-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpromise.lua
203 lines (172 loc) · 4.36 KB
/
promise.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
---------
-- promise library/module
-- Port of https://github.com/rhysbrettbowen/promise_impl/blob/master/promise.js
-- and https://github.com/rhysbrettbowen/Aplus
-- Source: https://github.com/Billiam/promise.lua
local State = {
PENDING = 'pending',
FULFILLED = 'fulfilled',
REJECTED = 'rejected',
}
local function callable_table(callback)
local mt = getmetatable(callback)
return type(mt) == 'table' and type(mt.__call) == 'function'
end
local function is_callable(value)
local t = type(value)
return t == 'function' or (t == 'table' and callable_table(value))
end
local transition, resolve, run
local prototype = {
is_promise = true,
state = State.PENDING
}
local mt = { __index = prototype }
local reject = function(promise, reason)
transition(promise, State.REJECTED, reason)
end
local fulfill = function(promise, value)
transition(promise, State.FULFILLED, value)
end
transition = function(promise, state, value)
if promise.state == state
or promise.state ~= State.PENDING
or ( state ~= State.FULFILLED and state ~= State.REJECTED )
then
return
end
promise.state = state
promise.value = value
run(promise)
end
function prototype:next(on_fulfilled, on_rejected)
local promise = Promise.new()
table.insert(self.queue, {
fulfill = is_callable(on_fulfilled) and on_fulfilled or nil,
reject = is_callable(on_rejected) and on_rejected or nil,
promise = promise
})
run(self)
return promise
end
resolve = function(promise, x)
if promise == x then
reject(promise, 'TypeError: cannot resolve a promise with itself')
return
end
local x_type = type(x)
if x_type ~= 'table' then
fulfill(promise, x)
return
end
-- x is a promise in the current implementation
if x.is_promise then
-- 2.3.2.1 if x is pending, resolve or reject this promise after completion
if x.state == State.PENDING then
x:next(
function(value)
resolve(promise, value)
end,
function(reason)
reject(promise, reason)
end
)
return
end
-- if x is not pending, transition promise to x's state and value
transition(promise, x.state, x.value)
return
end
local called = false
-- 2.3.3.1. Catches errors thrown by __index metatable
local success, reason = pcall(function()
local next = x.next
if is_callable(next) then
next(
x,
function(y)
if not called then
resolve(promise, y)
called = true
end
end,
function(r)
if not called then
reject(promise, r)
called = true
end
end
)
else
fulfill(promise, x)
end
end)
if not success then
if not called then
reject(promise, reason)
end
end
end
run = function(promise)
if promise.state == State.PENDING then return end
minetest.after(0, function()
-- drain promise.queue while allowing pushes from within callbacks
local q = promise.queue
local i = 0
while i < #q do
i = i + 1
local obj = q[i]
local success, result = pcall(function()
local success_fn = obj.fulfill or function(x) return x end
local failure_fn = obj.reject or function(x) error(x) end
local callback = promise.state == State.FULFILLED and success_fn or failure_fn
return callback(promise.value)
end)
if not success and obj.reject == nil then
-- replace reason
result = promise.value
end
if not success then
reject(obj.promise, result)
else
resolve(obj.promise, result)
end
end
for j = 1, i do
q[j] = nil
end
end)
end
function Promise.new(callback)
local instance = {
queue = {}
}
setmetatable(instance, mt)
if callback then
-- catch error in first callback function
local success, err = pcall(callback,
function(value)
resolve(instance, value)
end,
function(reason)
reject(instance, reason)
end
)
if not success then
return Promise.reject(err)
end
end
return instance
end
function prototype:catch(callback)
return self:next(nil, callback)
end
function prototype:finally(callback)
return self:next(callback, callback)
end
function prototype:resolve(value)
fulfill(self, value)
end
function prototype:reject(reason)
reject(self, reason)
end