-
Notifications
You must be signed in to change notification settings - Fork 0
/
Co.lua
223 lines (196 loc) · 5.21 KB
/
Co.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
-----------------------------------------------------------------------------
-- ES6 co lib in lua 5.1
-- Author: [email protected]
-- Copyright (c) 2015.11
--
-- This is a lib porting from Co v4 in JavaScript
-- It has some different before.
-- to see https://github.com/tj/co
-- Useage:
-- co(coroutine.create(function()
-- local v1 = coroutine.yield(Promise.resolve(123))
-- local v2 = coroutine.yield({
-- a = Promise.resolve(234),
-- b = Promise.resolve(456),
-- })
-- console.log(v1)
-- console.log(v2)
-- end)):catch(function(err)
-- print(err)
-- end)
-----------------------------------------------------------------------------
local Promise = Promise or require 'Promise'
local unpack = unpack or table.unpack
local isArray = table.isArray or function(tab)
if (type(tab) ~= "table") then
return false
end
local length = #tab
for k, v in pairs(tab) do
if ((type(k) ~= "number") or (k > length)) then
return false
end
end
return true
end
function tryCatch(cb)
return xpcall(cb, function(e)
return setStackTraceback and
(e .. '\n' .. debug.traceback())
or (e)
end)
end
----------------------------------------------------------------------
function new(gen, ...)
return Promise.new(function(resolve, reject)
if (type(gen) == 'function') then gen = coroutine.create(gen) end
if (type(gen) ~= 'thread') then return resolve(gen) end
local onResolved, onRejected, next
onResolved = function(res)
local done, ret
local coStatus = true
local xpcallRes, xpcallErr = tryCatch(function()
coStatus, ret = coroutine.resume(gen, res)
end)
if (not xpcallRes) then
return reject(xpcallErr)
end
if (not coStatus) then
return reject(ret)
end
done = (coroutine.status(gen) == 'dead')
next(done, ret)
end
onRejected = function(err)
local done, ret
local coStatus = true
local xpcallRes, xpcallErr = tryCatch(function()
coStatus, ret = coroutine.resume(gen, error(err))
end)
if (not xpcallRes) then
return reject(xpcallErr)
end
if (not coStatus) then
return reject(xpcallErr)
end
done = (coroutine.status(gen) == 'dead')
next(done, ret)
end
next = function(done, ret)
if (done) then
return resolve(ret)
end
local value = toPromise(ret)
if (value and (isPromise(value))) then
return value.andThen(onResolved, onRejected)
end
return onResolved(value)
-- onRejected(error('You may only yield a function, promise, generator, array, or object, '
-- .. 'but the following object was passed: "' .. type(ret) .. '"'))
end
onResolved();
end)
end
-- Convert a `yield`ed value into a promise.
--
-- @param {Mixed} obj
-- @return {Promise}
-- @api private
function toPromise(obj)
if (not obj) then return obj end
if (isPromise(obj)) then return obj end
if (isCoroutine(obj)) then return new(obj) end
if (type(obj) == 'function') then return thunkToPromise(obj) end
if (isArray(obj)) then
return arrayToPromise(obj)
elseif (type(obj) == 'table') then
return objectToPromise(obj)
end
return obj
end
-- Check if `obj` is a promise.
--
-- @param {Object} obj
-- @return {Boolean}
-- @api private
function isPromise(obj)
if ((type(obj) == 'table') and (type(obj.andThen) == 'function')) then
return true
end
return false
end
-- Check if `obj` is a generator.
--
-- @param {Mixed} obj
-- @return {Boolean}
-- @api private
function isCoroutine(obj)
if (type(obj) == 'thread') then
return true
end
return false
end
-- Convert a thunk to a promise.
--
-- @param {Function}
-- @return {Promise}
-- @api private
function thunkToPromise(fn)
return Promise.new(function(resolve, reject)
fn(function(err, res)
if (err) then return reject(err) end
if (#res > 2) then
res = { res[2] }
end
resolve(res)
end)
end)
end
-- Convert an array of "yieldables" to a promise.
-- Uses `Promise.all()` internally.
--
-- @param {Array} obj
-- @return {Promise}
-- @api private
function arrayToPromise(obj)
local newArr = {}
for k, v in ipairs(obj) do
table.insert(newArr, toPromise(v))
end
return Promise.all(newArr);
end
-- Convert an object of "yieldables" to a promise.
-- Uses `Promise.all()` internally.
--
-- @param {Object} obj
-- @return {Promise}
-- @api private
function objectToPromise(obj)
local results = {}
local promises = {}
local function defer(promise, key)
results[key] = nil
table.insert(promises, promise.andThen(function(res)
results[key] = res
end))
end
for key, value in pairs(obj) do
local promise = toPromise(value)
if (promise and isPromise(promise)) then
defer(promise, key)
else
results[key] = obj[key]
end
end
return Promise.all(promises).andThen(function()
return results
end)
end
return setmetatable({
new = new,
Promise = Promise,
}, {
__call = function(_, ...)
return new(...)
end
})