@quenty/promise 10.6.0 → 10.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +8 -0
- package/package.json +3 -3
- package/src/Shared/Promise.lua +32 -33
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,14 @@
|
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
|
4
4
|
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
5
|
|
|
6
|
+
# [10.7.0](https://github.com/Quenty/NevermoreEngine/compare/@quenty/promise@10.6.0...@quenty/promise@10.7.0) (2024-10-06)
|
|
7
|
+
|
|
8
|
+
**Note:** Version bump only for package @quenty/promise
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
|
|
6
14
|
# [10.6.0](https://github.com/Quenty/NevermoreEngine/compare/@quenty/promise@10.5.0...@quenty/promise@10.6.0) (2024-10-04)
|
|
7
15
|
|
|
8
16
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@quenty/promise",
|
|
3
|
-
"version": "10.
|
|
3
|
+
"version": "10.7.0",
|
|
4
4
|
"description": "Promise implementation for Roblox",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"Roblox",
|
|
@@ -26,11 +26,11 @@
|
|
|
26
26
|
],
|
|
27
27
|
"dependencies": {
|
|
28
28
|
"@quenty/deferred": "^2.2.0",
|
|
29
|
-
"@quenty/loader": "^10.
|
|
29
|
+
"@quenty/loader": "^10.7.0",
|
|
30
30
|
"@quenty/maid": "^3.4.0"
|
|
31
31
|
},
|
|
32
32
|
"publishConfig": {
|
|
33
33
|
"access": "public"
|
|
34
34
|
},
|
|
35
|
-
"gitHead": "
|
|
35
|
+
"gitHead": "67c5dbf46f6f45213812f3f117419a5534936a0b"
|
|
36
36
|
}
|
package/src/Shared/Promise.lua
CHANGED
|
@@ -12,6 +12,7 @@ local HttpService = game:GetService("HttpService")
|
|
|
12
12
|
local ENABLE_TRACEBACK = false
|
|
13
13
|
local _emptyRejectedPromise = nil
|
|
14
14
|
local _emptyFulfilledPromise = nil
|
|
15
|
+
local EMPTY_PACKED_TUPLE = table.freeze({ n = 0; })
|
|
15
16
|
|
|
16
17
|
local Promise = {}
|
|
17
18
|
Promise.ClassName = "Promise"
|
|
@@ -138,7 +139,7 @@ function Promise.rejected(...)
|
|
|
138
139
|
end
|
|
139
140
|
|
|
140
141
|
local promise = Promise.new()
|
|
141
|
-
promise:_reject(
|
|
142
|
+
promise:_reject(table.pack(...))
|
|
142
143
|
return promise
|
|
143
144
|
end
|
|
144
145
|
|
|
@@ -178,7 +179,7 @@ end
|
|
|
178
179
|
]=]
|
|
179
180
|
function Promise:Wait()
|
|
180
181
|
if self._fulfilled then
|
|
181
|
-
return unpack(self._fulfilled, 1, self.
|
|
182
|
+
return table.unpack(self._fulfilled, 1, self._fulfilled.n)
|
|
182
183
|
elseif self._rejected then
|
|
183
184
|
return error(tostring(self._rejected[1]), 2)
|
|
184
185
|
else
|
|
@@ -195,7 +196,7 @@ function Promise:Wait()
|
|
|
195
196
|
if self._rejected then
|
|
196
197
|
return error(tostring(self._rejected[1]), 2)
|
|
197
198
|
else
|
|
198
|
-
return unpack(self._fulfilled, 1, self.
|
|
199
|
+
return table.unpack(self._fulfilled, 1, self._fulfilled.n)
|
|
199
200
|
end
|
|
200
201
|
end
|
|
201
202
|
end
|
|
@@ -210,9 +211,9 @@ end
|
|
|
210
211
|
]=]
|
|
211
212
|
function Promise:Yield()
|
|
212
213
|
if self._fulfilled then
|
|
213
|
-
return true, unpack(self._fulfilled, 1, self.
|
|
214
|
+
return true, table.unpack(self._fulfilled, 1, self._fulfilled.n)
|
|
214
215
|
elseif self._rejected then
|
|
215
|
-
return false, unpack(self._rejected, 1, self.
|
|
216
|
+
return false, table.unpack(self._rejected, 1, self._rejected.n)
|
|
216
217
|
else
|
|
217
218
|
local waitingCoroutine = coroutine.running()
|
|
218
219
|
|
|
@@ -225,9 +226,9 @@ function Promise:Yield()
|
|
|
225
226
|
coroutine.yield()
|
|
226
227
|
|
|
227
228
|
if self._fulfilled then
|
|
228
|
-
return true, unpack(self._fulfilled, 1, self.
|
|
229
|
+
return true, table.unpack(self._fulfilled, 1, self._fulfilled.n)
|
|
229
230
|
elseif self._rejected then
|
|
230
|
-
return false, unpack(self._rejected, 1, self.
|
|
231
|
+
return false, table.unpack(self._rejected, 1, self._rejected.n)
|
|
231
232
|
else
|
|
232
233
|
error("Bad state")
|
|
233
234
|
end
|
|
@@ -247,7 +248,7 @@ function Promise:Resolve(...)
|
|
|
247
248
|
|
|
248
249
|
local len = select("#", ...)
|
|
249
250
|
if len == 0 then
|
|
250
|
-
self:_fulfill(
|
|
251
|
+
self:_fulfill(EMPTY_PACKED_TUPLE)
|
|
251
252
|
elseif self == (...) then
|
|
252
253
|
self:Reject("TypeError: Resolved to self")
|
|
253
254
|
elseif Promise.isPromise(...) then
|
|
@@ -266,16 +267,16 @@ function Promise:Resolve(...)
|
|
|
266
267
|
function(...)
|
|
267
268
|
-- Still need to verify at this point that we're pending!
|
|
268
269
|
if self._pendingExecuteList then
|
|
269
|
-
self:_reject(
|
|
270
|
+
self:_reject(table.pack(...))
|
|
270
271
|
end
|
|
271
272
|
end,
|
|
272
273
|
nil
|
|
273
274
|
}
|
|
274
275
|
elseif promise2._rejected then -- rejected
|
|
275
276
|
promise2._unconsumedException = false
|
|
276
|
-
self:_reject(promise2._rejected
|
|
277
|
+
self:_reject(promise2._rejected)
|
|
277
278
|
elseif promise2._fulfilled then -- fulfilled
|
|
278
|
-
self:_fulfill(promise2._fulfilled
|
|
279
|
+
self:_fulfill(promise2._fulfilled)
|
|
279
280
|
else
|
|
280
281
|
error("[Promise.Resolve] - Bad promise2 state")
|
|
281
282
|
end
|
|
@@ -291,23 +292,21 @@ function Promise:Resolve(...)
|
|
|
291
292
|
-- TODO: Handle thenable promises!
|
|
292
293
|
-- Problem: Lua has :andThen() and also :Then() as two methods in promise
|
|
293
294
|
-- implementations.
|
|
294
|
-
self:_fulfill(
|
|
295
|
+
self:_fulfill(table.pack(...))
|
|
295
296
|
end
|
|
296
297
|
end
|
|
297
298
|
|
|
298
|
-
--[
|
|
299
|
+
--[[
|
|
299
300
|
Fulfills the promise with the value
|
|
300
301
|
@param values { T } -- Params to fulfil with
|
|
301
|
-
@param valuesLength number
|
|
302
302
|
@private
|
|
303
|
-
]
|
|
304
|
-
function Promise:_fulfill(values
|
|
303
|
+
]]
|
|
304
|
+
function Promise:_fulfill(values)
|
|
305
305
|
if not self._pendingExecuteList then
|
|
306
306
|
return
|
|
307
307
|
end
|
|
308
308
|
|
|
309
309
|
self._fulfilled = values
|
|
310
|
-
self._valuesLength = valuesLength
|
|
311
310
|
|
|
312
311
|
local list = self._pendingExecuteList
|
|
313
312
|
self._pendingExecuteList = nil
|
|
@@ -321,16 +320,15 @@ end
|
|
|
321
320
|
@param ... T -- Params to reject with
|
|
322
321
|
]=]
|
|
323
322
|
function Promise:Reject(...)
|
|
324
|
-
self:_reject(
|
|
323
|
+
self:_reject(table.pack(...))
|
|
325
324
|
end
|
|
326
325
|
|
|
327
|
-
function Promise:_reject(values
|
|
326
|
+
function Promise:_reject(values)
|
|
328
327
|
if not self._pendingExecuteList then
|
|
329
328
|
return
|
|
330
329
|
end
|
|
331
330
|
|
|
332
331
|
self._rejected = values
|
|
333
|
-
self._valuesLength = valuesLength
|
|
334
332
|
|
|
335
333
|
local list = self._pendingExecuteList
|
|
336
334
|
self._pendingExecuteList = nil
|
|
@@ -339,7 +337,7 @@ function Promise:_reject(values, valuesLength)
|
|
|
339
337
|
end
|
|
340
338
|
|
|
341
339
|
-- Check for uncaught exceptions
|
|
342
|
-
if self._unconsumedException and self.
|
|
340
|
+
if self._unconsumedException and self._rejected.n > 0 then
|
|
343
341
|
task.defer(function()
|
|
344
342
|
-- Yield to end of frame, giving control back to Roblox.
|
|
345
343
|
-- This is the equivalent of giving something back to a task manager.
|
|
@@ -461,11 +459,12 @@ function Promise:Catch(onRejected)
|
|
|
461
459
|
return self:Then(nil, onRejected)
|
|
462
460
|
end
|
|
463
461
|
|
|
462
|
+
|
|
464
463
|
--[=[
|
|
465
464
|
Rejects the current promise. Utility left for Maid task
|
|
466
465
|
]=]
|
|
467
466
|
function Promise:Destroy()
|
|
468
|
-
self:_reject(
|
|
467
|
+
self:_reject(EMPTY_PACKED_TUPLE)
|
|
469
468
|
end
|
|
470
469
|
|
|
471
470
|
--[=[
|
|
@@ -480,9 +479,9 @@ end
|
|
|
480
479
|
]=]
|
|
481
480
|
function Promise:GetResults()
|
|
482
481
|
if self._rejected then
|
|
483
|
-
return false, unpack(self._rejected, 1, self.
|
|
482
|
+
return false, table.unpack(self._rejected, 1, self._rejected.n)
|
|
484
483
|
elseif self._fulfilled then
|
|
485
|
-
return true, unpack(self._fulfilled, 1, self.
|
|
484
|
+
return true, table.unpack(self._fulfilled, 1, self._fulfilled.n)
|
|
486
485
|
else
|
|
487
486
|
error("Still pending")
|
|
488
487
|
end
|
|
@@ -492,7 +491,7 @@ function Promise:_getResolveReject()
|
|
|
492
491
|
return function(...)
|
|
493
492
|
self:Resolve(...)
|
|
494
493
|
end, function(...)
|
|
495
|
-
self:_reject(
|
|
494
|
+
self:_reject(table.pack(...))
|
|
496
495
|
end
|
|
497
496
|
end
|
|
498
497
|
|
|
@@ -510,10 +509,10 @@ function Promise:_executeThen(onFulfilled, onRejected, promise2)
|
|
|
510
509
|
-- If either onFulfilled or onRejected returns a value x, run
|
|
511
510
|
-- the Promise Resolution Procedure [[Resolve]](promise2, x).
|
|
512
511
|
if promise2 then
|
|
513
|
-
promise2:Resolve(onFulfilled(unpack(self._fulfilled, 1, self.
|
|
512
|
+
promise2:Resolve(onFulfilled(table.unpack(self._fulfilled, 1, self._fulfilled.n)))
|
|
514
513
|
return promise2
|
|
515
514
|
else
|
|
516
|
-
local results = table.pack(onFulfilled(unpack(self._fulfilled, 1, self.
|
|
515
|
+
local results = table.pack(onFulfilled(table.unpack(self._fulfilled, 1, self._fulfilled.n)))
|
|
517
516
|
if results.n == 0 then
|
|
518
517
|
return _emptyFulfilledPromise
|
|
519
518
|
elseif results.n == 1 and Promise.isPromise(results[1]) then
|
|
@@ -530,7 +529,7 @@ function Promise:_executeThen(onFulfilled, onRejected, promise2)
|
|
|
530
529
|
-- If onFulfilled is not a function and promise1 is fulfilled,
|
|
531
530
|
-- promise2 must be fulfilled with the same value as promise1.
|
|
532
531
|
if promise2 then
|
|
533
|
-
promise2:_fulfill(self._fulfilled
|
|
532
|
+
promise2:_fulfill(self._fulfilled)
|
|
534
533
|
return promise2
|
|
535
534
|
else
|
|
536
535
|
return self
|
|
@@ -541,10 +540,10 @@ function Promise:_executeThen(onFulfilled, onRejected, promise2)
|
|
|
541
540
|
-- If either onFulfilled or onRejected returns a value x, run
|
|
542
541
|
-- the Promise Resolution Procedure [[Resolve]](promise2, x).
|
|
543
542
|
if promise2 then
|
|
544
|
-
promise2:Resolve(onRejected(unpack(self._rejected, 1, self.
|
|
543
|
+
promise2:Resolve(onRejected(table.unpack(self._rejected, 1, self._rejected.n)))
|
|
545
544
|
return promise2
|
|
546
545
|
else
|
|
547
|
-
local results = table.pack(onRejected(unpack(self._rejected, 1, self.
|
|
546
|
+
local results = table.pack(onRejected(table.unpack(self._rejected, 1, self._rejected.n)))
|
|
548
547
|
if results.n == 0 then
|
|
549
548
|
return _emptyFulfilledPromise
|
|
550
549
|
elseif results.n == 1 and Promise.isPromise(results[1]) then
|
|
@@ -561,7 +560,7 @@ function Promise:_executeThen(onFulfilled, onRejected, promise2)
|
|
|
561
560
|
-- If onRejected is not a function and promise1 is rejected, promise2 must be
|
|
562
561
|
-- rejected with the same reason as promise1.
|
|
563
562
|
if promise2 then
|
|
564
|
-
promise2:_reject(self._rejected
|
|
563
|
+
promise2:_reject(self._rejected)
|
|
565
564
|
|
|
566
565
|
return promise2
|
|
567
566
|
else
|
|
@@ -575,9 +574,9 @@ end
|
|
|
575
574
|
|
|
576
575
|
-- Initialize promise values
|
|
577
576
|
_emptyFulfilledPromise = Promise.new()
|
|
578
|
-
_emptyFulfilledPromise:_fulfill(
|
|
577
|
+
_emptyFulfilledPromise:_fulfill(EMPTY_PACKED_TUPLE)
|
|
579
578
|
|
|
580
579
|
_emptyRejectedPromise = Promise.new()
|
|
581
|
-
_emptyRejectedPromise:_reject(
|
|
580
|
+
_emptyRejectedPromise:_reject(EMPTY_PACKED_TUPLE)
|
|
582
581
|
|
|
583
582
|
return Promise
|