-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpromise.lua
635 lines (631 loc) · 15.7 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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
local _module_0 = nil
--[[
MIT License
Copyright (c) 2023-2024 Retro
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
--]]
local Error, TypeError, RuntimeError
do
local _obj_0 = include("error.lua")
Error, TypeError, RuntimeError = _obj_0.Error, _obj_0.TypeError, _obj_0.RuntimeError
end
local isfunction, istable, isstring, getmetatable, pcall, xpcall, error, ipairs = _G.isfunction, _G.istable, _G.isstring, _G.getmetatable, _G.pcall, _G.xpcall, _G.error, _G.ipairs
local format, match = string.format, string.match
local create, resume, yield, running = coroutine.create, coroutine.resume, coroutine.yield, coroutine.running
local remove = table.remove
local setTimeout = timer.Simple
local iscallable
iscallable = function(obj)
if isfunction(obj) then
return true
end
local meta = getmetatable(obj)
return meta and meta.__call and true or false
end
local getThenable
getThenable = function(obj)
local thenFn = obj.Then or obj.next
return iscallable(thenFn) and thenFn
end
local getAwaitable
getAwaitable = function(obj)
local awaitable = obj.Await or obj.await
return iscallable(awaitable) and awaitable
end
local once
once = function()
local wasCalled = false
return function(wrappedFn)
return function(...)
if wasCalled then
return ...
end
wasCalled = true
return wrappedFn(...)
end
end
end
local HTTPError
do
local _class_0
local _parent_0 = Error
local _base_0 = { }
for _key_0, _val_0 in pairs(_parent_0.__base) do
if _base_0[_key_0] == nil and _key_0:match("^__") and not (_key_0 == "__index" and _val_0 == _parent_0.__base) then
_base_0[_key_0] = _val_0
end
end
if _base_0.__index == nil then
_base_0.__index = _base_0
end
setmetatable(_base_0, _parent_0.__base)
_class_0 = setmetatable({
__init = function(self, ...)
return _class_0.__parent.__init(self, ...)
end,
__base = _base_0,
__name = "HTTPError",
__parent = _parent_0
}, {
__index = function(cls, name)
local val = rawget(_base_0, name)
if val == nil then
local parent = rawget(cls, "__parent")
if parent then
return parent[name]
end
else
return val
end
end,
__call = function(cls, ...)
local _self_0 = setmetatable({ }, _base_0)
cls.__init(_self_0, ...)
return _self_0
end
})
_base_0.__class = _class_0
if _parent_0.__inherited then
_parent_0.__inherited(_parent_0, _class_0)
end
HTTPError = _class_0
end
local PromiseError
do
local _class_0
local _parent_0 = Error
local _base_0 = { }
for _key_0, _val_0 in pairs(_parent_0.__base) do
if _base_0[_key_0] == nil and _key_0:match("^__") and not (_key_0 == "__index" and _val_0 == _parent_0.__base) then
_base_0[_key_0] = _val_0
end
end
if _base_0.__index == nil then
_base_0.__index = _base_0
end
setmetatable(_base_0, _parent_0.__base)
_class_0 = setmetatable({
__init = function(self, ...)
return _class_0.__parent.__init(self, ...)
end,
__base = _base_0,
__name = "PromiseError",
__parent = _parent_0
}, {
__index = function(cls, name)
local val = rawget(_base_0, name)
if val == nil then
local parent = rawget(cls, "__parent")
if parent then
return parent[name]
end
else
return val
end
end,
__call = function(cls, ...)
local _self_0 = setmetatable({ }, _base_0)
cls.__init(_self_0, ...)
return _self_0
end
})
_base_0.__class = _class_0
if _parent_0.__inherited then
_parent_0.__inherited(_parent_0, _class_0)
end
PromiseError = _class_0
end
local Promise
do
local _class_0
local STATE_PENDING, STATE_FULFILLED, STATE_REJECTED, IsPromise, finalizePromiseUnsafe, finalizePromise, finalize, fulfill, resolveThenable, resolve, reject, Resolve, Reject, Then, Catch, Finally, CALL_STACK, transformError, SafeAwait, Await
local _base_0 = {
__tostring = function(self)
local _exp_0 = self.state
if STATE_PENDING == _exp_0 then
return format("Promise %p { <state>: \"pending\" }", self)
elseif STATE_FULFILLED == _exp_0 then
return format("Promise %p { <state>: \"fulfilled\", <value>: %s }", self, self.value)
elseif STATE_REJECTED == _exp_0 then
return format("Promise %p { <state>: \"rejected\", <reason>: %s }", self, self.reason)
else
return format("Promise %p { <state>: \"invalid\" }", self)
end
end
}
if _base_0.__index == nil then
_base_0.__index = _base_0
end
_class_0 = setmetatable({
__init = function(self, executor)
self.state = STATE_PENDING
self.resolving = false
if iscallable(executor) then
self.resolving = true
local once_wrapper = once()
local onFulfill = once_wrapper(function(value)
return resolve(self, value)
end)
local onReject = once_wrapper(function(reason)
return reject(self, reason)
end)
return xpcall(executor, function(err)
return onReject(err)
end, onFulfill, onReject)
end
end,
__base = _base_0,
__name = "Promise"
}, {
__index = _base_0,
__call = function(cls, ...)
local _self_0 = setmetatable({ }, _base_0)
cls.__init(_self_0, ...)
return _self_0
end
})
_base_0.__class = _class_0
local self = _class_0;
self.VERSION = "2.1.0"
self.AUTHOR = "Retro"
self.URL = "https://github.com/dankmolot/gm_promise"
STATE_PENDING = 1
STATE_FULFILLED = 2
STATE_REJECTED = 3
IsPromise = function(obj)
return istable(obj) and obj.__class == Promise
end
finalizePromiseUnsafe = function(self, p)
local onFulfilled, onRejected, onFinally = p.onFulfilled, p.onRejected, p.onFinally
do
local _exp_0 = self.state
if STATE_FULFILLED == _exp_0 then
if onFulfilled then
resolve(p, onFulfilled(self.value))
else
resolve(p, self.value)
end
elseif STATE_REJECTED == _exp_0 then
if onRejected then
resolve(p, onRejected(self.reason))
else
reject(p, self.reason)
end
end
end
if onFinally then
onFinally()
end
return
end
finalizePromise = function(self, p)
local success, err = pcall(finalizePromiseUnsafe, self, p)
if not success then
return reject(p, err)
end
end
finalize = function(self)
if self.state == STATE_PENDING then
return
end
return setTimeout(0, function()
local queue = self.queue
local index = 1
while queue and queue[index] do
finalizePromise(self, queue[index])
queue[index] = nil
index = index + 1
end
end)
end
fulfill = function(self, value)
self.state = STATE_FULFILLED
self.value = value
return finalize(self)
end
resolveThenable = function(self, obj, thenable)
local once_wrapper = once()
local onFulfill = once_wrapper(function(value)
return resolve(self, value)
end)
local onReject = once_wrapper(function(reason)
return reject(self, reason)
end)
local ok, err = pcall(thenable, obj, onFulfill, onReject)
if not ok then
return onReject(err)
end
end
resolve = function(self, value)
if self.state ~= STATE_PENDING then
return
end
if value == self then
return reject(self, TypeError("Cannot resolve a promise with itself"))
end
self.resolving = true
self.onFulfilled = nil
self.onRejected = nil
self.onFinally = nil
if IsPromise(value) then
if value.state == STATE_PENDING then
if not value.queue then
value.queue = { }
end
local _obj_0 = value.queue
_obj_0[#_obj_0 + 1] = self
else
return finalizePromise(value, self)
end
elseif istable(value) then
local ok, thenable = pcall(getThenable, value)
if not ok then
return reject(self, thenable)
end
if thenable then
return resolveThenable(self, value, thenable)
else
return fulfill(self, value)
end
else
return fulfill(self, value)
end
end
reject = function(self, reason)
if self.state ~= STATE_PENDING then
return
end
self.state = STATE_REJECTED
self.reason = reason
return finalize(self)
end
Resolve = function(self, value)
if not self.resolving then
return resolve(self, value)
end
end
Reject = function(self, reason)
if not self.resolving then
return reject(self, reason)
end
end
self.Resolve = function(value)
local p = Promise()
Resolve(p, value)
return p
end
self.Reject = function(reason)
local p = Promise()
Reject(p, reason)
return p
end
Then = function(self, onFulfilled, onRejected, onFinally)
local p = Promise()
if iscallable(onFulfilled) then
p.onFulfilled = onFulfilled
end
if iscallable(onRejected) then
p.onRejected = onRejected
end
if iscallable(onFinally) then
p.onFinally = onFinally
end
if not self.queue then
self.queue = { }
end
do
local _obj_0 = self.queue
_obj_0[#_obj_0 + 1] = p
end
finalize(self)
return p
end
Catch = function(self, onRejected)
return Then(self, nil, onRejected)
end
Finally = function(self, onFinally)
return Then(self, nil, nil, onFinally)
end
CALL_STACK = { }
transformError = function(err)
if isstring(err) then
local file, line, message = match(err, "^([A-Za-z0-9%-_/.]+):(%d+): (.*)")
if file and line then
err = RuntimeError(message, file, line, 4)
end
end
if Error.is(err) then
local stack = err.stack
local length = #stack
if length >= 2 and stack[length - 1].name == "xpcall" then
stack[length - 1] = nil
stack[length] = nil
end
if #stack == 0 then
err.fileName = nil
err.lineNumber = nil
end
local lastStack = CALL_STACK[#CALL_STACK]
if lastStack then
if #stack > 0 and not stack[#stack].name then
stack[#stack].name = lastStack[1].name
end
-- Append previous stack to the current stack
for _index_0 = 2, #lastStack do
local entry = lastStack[_index_0]
stack[#stack + 1] = entry
end
end
local first = stack[1]
if first then
self.fileName = self.fileName or first.short_src
self.lineNumber = self.lineNumber or first.currentline
end
end
return err
end
self.Async = function(fn)
return function(...)
local p = Promise()
CALL_STACK[#CALL_STACK + 1] = Error.captureStack()
local co = create(function(...)
local success, result = xpcall(fn, transformError, ...)
if success then
Resolve(p, result)
else
Reject(p, result)
end
CALL_STACK[#CALL_STACK] = nil
end)
resume(co, ...)
return p
end
end
SafeAwait = function(p)
local co = running()
if not co then
return false, PromiseError("Cannot await in main thread")
end
local _success, _value, waiting, stack
local finish
finish = function(success, value)
if waiting then
CALL_STACK[#CALL_STACK + 1] = stack
return resume(co, success, value)
else
_success, _value = success, value
end
end
local wait
wait = function()
if _success ~= nil then
return _success, _value
end
waiting = true
stack = remove(CALL_STACK)
return yield()
end
local once_wrapper = once()
local onResolve = once_wrapper(function(value)
return finish(true, value)
end)
local onReject = once_wrapper(function(reason)
return finish(false, reason)
end)
if IsPromise(p) then
local _exp_0 = p.state
if STATE_FULFILLED == _exp_0 then
return true, p.value
elseif STATE_REJECTED == _exp_0 then
return false, p.reason
else
Then(p, onResolve, onReject)
return wait()
end
end
local thenable = istable(p) and getThenable(p)
if iscallable(thenable) then
pcall(thenable, p, onResolve, onReject)
return wait()
end
return true, p
end
Await = function(p)
local awaitable = istable(p) and getAwaitable(p)
if awaitable and not IsPromise(p) then
return awaitable(p)
end
local ok, result = SafeAwait(p)
if ok then
return result
else
return error(result)
end
end
self.All = function(promises)
local p = Promise()
local count = #promises
local values = { }
if count == 0 then
Resolve(p, values)
end
for i, promise in ipairs(promises) do
if IsPromise(promise) then
promise:Then(function(value)
values[i] = value
count = count - 1
if count == 0 then
return Resolve(p, values)
end
end, function(reason)
return Reject(p, reason)
end)
else
values[i] = promise
count = count - 1
if count == 0 then
Resolve(p, values)
end
end
end
return p
end
self.AllSettled = function(promises)
local p = Promise()
local count = #promises
local values = { }
if count == 0 then
Resolve(p, values)
end
for i, promise in ipairs(promises) do
promise:Then(function(value)
values[i] = {
status = "fulfilled",
value = value
}
count = count - 1
if count == 0 then
return Resolve(p, values)
end
end, function(reason)
values[i] = {
status = "rejected",
reason = reason
}
if count == 0 then
return Resolve(p, values)
end
end)
end
return p
end
self.Any = function(promises)
local p = Promise()
local count = #promises
local reasons = { }
if count == 0 then
Reject(p, PromiseError("No promises to resolve"))
end
for i, promise in ipairs(promises) do
promise:Then(function(value)
return Resolve(p, value)
end, function(reason)
reasons[i] = reason
count = count - 1
if count == 0 then
return Resolve(p, values)
end
end)
end
return p
end
self.Race = function(promises)
local p = Promise()
for _index_0 = 1, #promises do
local promise = promises[_index_0]
promise:Then(function(value)
return Resolve(p, value)
end, function(reason)
return Reject(p, reason)
end)
end
return p
end
self.Delay = function(time, value)
local p = Promise()
setTimeout(time, function()
return Resolve(p, value)
end)
return p
end
self.HTTP = function(options)
local p = Promise()
options.success = function(code, body, headers)
Resolve(p, {
code = code,
body = body,
headers = headers
})
return
end
options.failed = function(err)
Reject(p, HTTPError(err))
return
end
do
local ok = HTTP(options)
if not ok then
Reject(p, HTTPError("failed to make http request"))
end
end
return p
end
self.__base.STATE_PENDING = STATE_PENDING
self.__base.STATE_FULFILLED = STATE_FULFILLED
self.__base.STATE_REJECTED = STATE_REJECTED
self.__base.IsPromise = IsPromise
self.__base.Resolve = Resolve
self.__base.resolve = Resolve
self.__base.Reject = Reject
self.__base.reject = Reject
self.__base.Then = Then
self.__base["then"] = Then
self.__base.next = Then
self.__base.andThen = Then
self.__base.Catch = Catch
self.__base.catch = Catch
self.__base.Finally = Finally
self.__base.finally = Finally
self.__base.SafeAwait = SafeAwait
self.__base.safeAwait = SafeAwait
self.__base.Await = Await
self.__base.await = Await
self.resolve = self.Resolve
self.reject = self.Reject
self.async = self.Async
self.all = self.All
self.allSettled = self.AllSettled
self.any = self.Any
self.race = self.Race
self.delay = self.Delay
self.http = self.HTTP
self.PromiseError = PromiseError
self.HTTPError = HTTPError
Promise = _class_0
end
_module_0 = Promise
return _module_0