@quenty/promise 3.2.1-canary.8533eea.0 → 3.3.1

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 CHANGED
@@ -3,12 +3,20 @@
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
- ## [3.2.1-canary.8533eea.0](https://github.com/Quenty/NevermoreEngine/compare/@quenty/promise@3.2.0...@quenty/promise@3.2.1-canary.8533eea.0) (2021-12-18)
6
+ ## [3.3.1](https://github.com/Quenty/NevermoreEngine/compare/@quenty/promise@3.3.0...@quenty/promise@3.3.1) (2021-12-30)
7
+
8
+ **Note:** Version bump only for package @quenty/promise
9
+
10
+
11
+
12
+
13
+
14
+ # [3.3.0](https://github.com/Quenty/NevermoreEngine/compare/@quenty/promise@3.2.0...@quenty/promise@3.3.0) (2021-12-18)
7
15
 
8
16
 
9
17
  ### Bug Fixes
10
18
 
11
- * Use Promies.spawn() since task.spawn() is probably cheaper now ([96d0732](https://github.com/Quenty/NevermoreEngine/commit/96d0732df7b89f6e98c89d03810a87a41a17277b))
19
+ * Use Promies.spawn() since task.spawn() is probably cheaper now ([6a069c2](https://github.com/Quenty/NevermoreEngine/commit/6a069c2a1c99ca34f53af747a969d5f5c4044e84))
12
20
 
13
21
 
14
22
 
package/README.md CHANGED
@@ -1,10 +1,10 @@
1
1
  ## Promise
2
2
  <div align="center">
3
- <a href="http://quenty.github.io/api/">
4
- <img src="https://img.shields.io/badge/docs-website-green.svg" alt="Documentation" />
3
+ <a href="http://quenty.github.io/NevermoreEngine/">
4
+ <img src="https://github.com/Quenty/NevermoreEngine/actions/workflows/docs.yml/badge.svg" alt="Documentation status" />
5
5
  </a>
6
6
  <a href="https://discord.gg/mhtGUS8">
7
- <img src="https://img.shields.io/badge/discord-nevermore-blue.svg" alt="Discord" />
7
+ <img src="https://img.shields.io/discord/385151591524597761?color=5865F2&label=discord&logo=discord&logoColor=white" alt="Discord" />
8
8
  </a>
9
9
  <a href="https://github.com/Quenty/NevermoreEngine/actions">
10
10
  <img src="https://github.com/Quenty/NevermoreEngine/actions/workflows/build.yml/badge.svg" alt="Build and release status" />
@@ -12,6 +12,8 @@
12
12
  </div>
13
13
  Promises, but without error handling as this screws with stack traces, using Roblox signals
14
14
 
15
+ <div align="center"><a href="https://quenty.github.io/NevermoreEngine/api/Promise">View docs →</a></div>
16
+
15
17
  ## Installation
16
18
  ```
17
19
  npm install @quenty/promise --save
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@quenty/promise",
3
- "version": "3.2.1-canary.8533eea.0",
3
+ "version": "3.3.1",
4
4
  "description": "Promise implementation for Roblox",
5
5
  "keywords": [
6
6
  "Roblox",
@@ -25,12 +25,12 @@
25
25
  "Quenty"
26
26
  ],
27
27
  "dependencies": {
28
- "@quenty/deferred": "2.0.1",
29
- "@quenty/loader": "3.1.1",
30
- "@quenty/maid": "2.0.1"
28
+ "@quenty/deferred": "^2.0.2",
29
+ "@quenty/loader": "^3.1.2",
30
+ "@quenty/maid": "^2.0.2"
31
31
  },
32
32
  "publishConfig": {
33
33
  "access": "public"
34
34
  },
35
- "gitHead": "8533eeade3bf6835c0295785c1c326b9abee3222"
35
+ "gitHead": "d146c77d0a8e452824de0ab0b4b03ba0370bcc1b"
36
36
  }
@@ -1,12 +1,12 @@
1
- --- Promises, but without error handling as this screws with stack traces, using Roblox signals
2
- -- @classmod Promise
3
- -- See: https://promisesaplus.com/
1
+ --[=[
2
+ Promises, but without error handling as this screws with stack traces, using Roblox signals
4
3
 
5
- local RunService = game:GetService("RunService")
4
+ See: https://promisesaplus.com/
6
5
 
7
- local function isPromise(value)
8
- return type(value) == "table" and value.ClassName == "Promise"
9
- end
6
+ @class Promise
7
+ ]=]
8
+
9
+ local RunService = game:GetService("RunService")
10
10
 
11
11
  -- Turns out debug.traceback() is slow
12
12
  local ENABLE_TRACEBACK = false
@@ -17,13 +17,27 @@ local Promise = {}
17
17
  Promise.ClassName = "Promise"
18
18
  Promise.__index = Promise
19
19
 
20
- --- Determines whether a value is a promise or not
21
- -- @function isPromise
22
- Promise.isPromise = isPromise
20
+ --[=[
21
+ Determines whether a value is a promise or not.
22
+
23
+ @param value any
24
+ @return boolean
25
+ ]=]
26
+ function Promise.isPromise(value)
27
+ return type(value) == "table" and value.ClassName == "Promise"
28
+ end
23
29
 
24
- --- Construct a new promise
25
- -- @constructor Promise.new()
26
- -- @treturn Promise
30
+ --[=[
31
+ Constructs a new promise.
32
+
33
+ ::warning
34
+ Do not yield within this func callback, as it will yield on the
35
+ main thread. This is a performance optimization.
36
+ ::
37
+
38
+ @param func (resolve: (...) -> (), reject: (...) -> ()) -> ()?
39
+ @return Promise<T>
40
+ ]=]
27
41
  function Promise.new(func)
28
42
  local self = setmetatable({
29
43
  _pendingExecuteList = {};
@@ -38,7 +52,12 @@ function Promise.new(func)
38
52
  return self
39
53
  end
40
54
 
41
- --- Initializes a new promise with the given function in a deferred wrapper
55
+ --[=[
56
+ Initializes a new promise with the given function in a deferred wrapper.
57
+
58
+ @param func (resolve: (...) -> (), reject: (...) -> ()) -> ()?
59
+ @return Promise<T>
60
+ ]=]
42
61
  function Promise.spawn(func)
43
62
  local self = Promise.new()
44
63
 
@@ -47,6 +66,12 @@ function Promise.spawn(func)
47
66
  return self
48
67
  end
49
68
 
69
+ --[=[
70
+ Initializes a new promise with the given function in a deferred wrapper.
71
+
72
+ @param func (resolve: (...) -> (), reject: (...) -> ()) -> ()?
73
+ @return Promise<T>
74
+ ]=]
50
75
  function Promise.defer(func)
51
76
  local self = Promise.new()
52
77
 
@@ -56,12 +81,18 @@ function Promise.defer(func)
56
81
  return self
57
82
  end
58
83
 
84
+ --[=[
85
+ Returns a resolved promise with the following values
86
+
87
+ @param ... Values to resolve to
88
+ @return Promise<T>
89
+ ]=]
59
90
  function Promise.resolved(...)
60
91
  local n = select("#", ...)
61
92
  if n == 0 then
62
93
  -- Reuse promise here to save on calls to Promise.resolved()
63
94
  return _emptyFulfilledPromise
64
- elseif n == 1 and isPromise(...) then
95
+ elseif n == 1 and Promise.isPromise(...) then
65
96
  local promise = (...)
66
97
 
67
98
  -- Resolving to promise that is already resolved. Just return the promise!
@@ -75,6 +106,12 @@ function Promise.resolved(...)
75
106
  return promise
76
107
  end
77
108
 
109
+ --[=[
110
+ Returns a rejected promise with the following values
111
+
112
+ @param ... Values to reject to
113
+ @return Promise<T>
114
+ ]=]
78
115
  function Promise.rejected(...)
79
116
  local n = select("#", ...)
80
117
  if n == 0 then
@@ -87,21 +124,40 @@ function Promise.rejected(...)
87
124
  return promise
88
125
  end
89
126
 
90
- --- Returns whether or not the promise is pending
91
- -- @treturn bool True if pending, false otherwise
127
+ --[=[
128
+ Returns whether or not the promise is pending
129
+
130
+ @return bool -- True if pending, false otherwise
131
+ ]=]
92
132
  function Promise:IsPending()
93
133
  return self._pendingExecuteList ~= nil
94
134
  end
95
135
 
136
+ --[=[
137
+ Returns whether or not the promise is fulfilled
138
+
139
+ @return bool -- True if fulfilled
140
+ ]=]
96
141
  function Promise:IsFulfilled()
97
142
  return self._fulfilled ~= nil
98
143
  end
99
144
 
145
+ --[=[
146
+ Returns whether or not the promise is rejected
147
+
148
+ @return bool -- True if rejected
149
+ ]=]
100
150
  function Promise:IsRejected()
101
151
  return self._rejected ~= nil
102
152
  end
103
153
 
104
- --- Yield until the promise is complete
154
+ --[=[
155
+ Yields until the promise is complete, and errors if an error
156
+ exists, otherwise returns the fulfilled results.
157
+
158
+ @yields
159
+ @return T
160
+ ]=]
105
161
  function Promise:Wait()
106
162
  if self._fulfilled then
107
163
  return unpack(self._fulfilled, 1, self._valuesLength)
@@ -127,6 +183,14 @@ function Promise:Wait()
127
183
  end
128
184
  end
129
185
 
186
+
187
+ --[=[
188
+ Yields until the promise is complete, then returns a boolean indicating
189
+ the result, followed by the values from the promise.
190
+
191
+ @yields
192
+ @return boolean, T
193
+ ]=]
130
194
  function Promise:Yield()
131
195
  if self._fulfilled then
132
196
  return true, unpack(self._fulfilled, 1, self._valuesLength)
@@ -153,9 +217,11 @@ function Promise:Yield()
153
217
  end
154
218
 
155
219
 
156
- --- Promise resolution procedure
157
- -- Resolves a promise
158
- -- @return self
220
+ --[=[
221
+ Promise resolution procedure, resolves the given values
222
+
223
+ @param ... T
224
+ ]=]
159
225
  function Promise:Resolve(...)
160
226
  if not self._pendingExecuteList then
161
227
  return
@@ -166,7 +232,7 @@ function Promise:Resolve(...)
166
232
  self:_fulfill({}, 0)
167
233
  elseif self == (...) then
168
234
  self:Reject("TypeError: Resolved to self")
169
- elseif isPromise(...) then
235
+ elseif Promise.isPromise(...) then
170
236
  if len > 1 then
171
237
  local message = ("When resolving a promise, extra arguments are discarded! See:\n\n%s")
172
238
  :format(self._source)
@@ -213,9 +279,12 @@ function Promise:Resolve(...)
213
279
  end
214
280
  end
215
281
 
216
- --- Fulfills the promise with the value
217
- -- @param ... Params to _fulfill with
218
- -- @return self
282
+ --[=[
283
+ Fulfills the promise with the value
284
+ @param values { T } -- Params to fulfil with
285
+ @param valuesLength number
286
+ @private
287
+ ]=]
219
288
  function Promise:_fulfill(values, valuesLength)
220
289
  if not self._pendingExecuteList then
221
290
  return
@@ -231,9 +300,10 @@ function Promise:_fulfill(values, valuesLength)
231
300
  end
232
301
  end
233
302
 
234
- --- Rejects the promise with the value given
235
- -- @param ... Params to reject with
236
- -- @return self
303
+ --[=[
304
+ Rejects the promise with the values given
305
+ @param ... T -- Params to reject with
306
+ ]=]
237
307
  function Promise:Reject(...)
238
308
  self:_reject({...}, select("#", ...))
239
309
  end
@@ -272,18 +342,26 @@ function Promise:_reject(values, valuesLength)
272
342
  end
273
343
  end
274
344
 
275
- --- Handlers if/when promise is fulfilled/rejected. It takes up to two arguments, callback functions
276
- -- for the success and failure cases of the Promise. May return the same promise if certain behavior
277
- -- is met.
278
- -- NOTE: We do not comply with 2.2.4 (onFulfilled or onRejected must not be called until the execution context stack
279
- -- contains only platform code). This means promises may stack overflow, however, it also makes promises a lot cheaper
280
- -- @tparam[opt=nil] function onFulfilled Called if/when fulfilled with parameters
281
- -- If/when promise is fulfilled, all respective onFulfilled callbacks must execute in the order of their
282
- -- originating calls to then.
283
- -- @tparam[opt=nil] function onRejected Called if/when rejected with parameters
284
- -- If/when promise is rejected, all respective onRejected callbacks must execute in the order of their
285
- -- originating calls to then.
286
- -- @treturn Promise
345
+ --[=[
346
+ Handlers if/when promise is fulfilled/rejected. It takes up to two arguments, callback functions
347
+ for the success and failure cases of the Promise. May return the same promise if certain behavior
348
+ is met.
349
+
350
+ :::info
351
+ We do not comply with 2.2.4 (onFulfilled or onRejected must not be called until the execution context stack
352
+ contains only platform code). This means promises may stack overflow, however, it also makes promises a lot cheaper
353
+ :::
354
+
355
+ If/when promise is rejected, all respective onRejected callbacks must execute in the order of their
356
+ originating calls to then.
357
+
358
+ If/when promise is fulfilled, all respective onFulfilled callbacks must execute in the order of their
359
+ originating calls to then.
360
+
361
+ @param onFulfilled function -- Called if/when fulfilled with parameters
362
+ @param onRejected function -- Called if/when rejected with parameters
363
+ @return Promise<T>
364
+ ]=]
287
365
  function Promise:Then(onFulfilled, onRejected)
288
366
  if type(onRejected) == "function" then
289
367
  self._unconsumedException = false
@@ -298,9 +376,16 @@ function Promise:Then(onFulfilled, onRejected)
298
376
  end
299
377
  end
300
378
 
301
- -- Like then, but the value passed down the chain is the resolved value of the promise, not
302
- -- the value returned from onFulfilled or onRejected
303
- -- Will still yield for the result if a promise is returned, but will discard the result.
379
+ --[=[
380
+ Like then, but the value passed down the chain is the resolved value of the promise, not
381
+ the value returned from onFulfilled or onRejected
382
+
383
+ Will still yield for the result if a promise is returned, but will discard the result.
384
+
385
+ @param onFulfilled function
386
+ @param onRejected function
387
+ @return Promise<T> -- Returns self
388
+ ]=]
304
389
  function Promise:Tap(onFulfilled, onRejected)
305
390
  -- Run immediately like then, but we return something safer!
306
391
  local result = self:Then(onFulfilled, onRejected)
@@ -327,23 +412,36 @@ function Promise:Tap(onFulfilled, onRejected)
327
412
  end
328
413
  end
329
414
 
415
+ --[=[
416
+ Executes upon pending stop
417
+
418
+ @param func function
419
+ @return Promise<T>
420
+ ]=]
330
421
  function Promise:Finally(func)
331
422
  return self:Then(func, func)
332
423
  end
333
424
 
334
- --- Catch errors from the promise
335
- -- @treturn Promise
425
+ --[=[
426
+ Catch errors from the promise
427
+
428
+ @param onRejected function
429
+ @return Promise<T>
430
+ ]=]
336
431
  function Promise:Catch(onRejected)
337
432
  return self:Then(nil, onRejected)
338
433
  end
339
434
 
340
- --- Rejects the current promise.
341
- -- Utility left for Maid task
342
- -- @treturn nil
435
+ --[=[
436
+ Rejects the current promise. Utility left for Maid task
437
+ ]=]
343
438
  function Promise:Destroy()
344
439
  self:_reject({}, 0)
345
440
  end
346
441
 
442
+ --[=[
443
+ Returns the results from the promise
444
+ ]=]
347
445
  function Promise:GetResults()
348
446
  if self._rejected then
349
447
  return false, unpack(self._rejected, 1, self._valuesLength)
@@ -375,7 +473,7 @@ function Promise:_executeThen(onFulfilled, onRejected, promise2)
375
473
  local results = table.pack(onFulfilled(unpack(self._fulfilled, 1, self._valuesLength)))
376
474
  if results.n == 0 then
377
475
  return _emptyFulfilledPromise
378
- elseif results.n == 1 and isPromise(results[1]) then
476
+ elseif results.n == 1 and Promise.isPromise(results[1]) then
379
477
  return results[1]
380
478
  else
381
479
  local promise = Promise.new()
@@ -406,7 +504,7 @@ function Promise:_executeThen(onFulfilled, onRejected, promise2)
406
504
  local results = table.pack(onRejected(unpack(self._rejected, 1, self._valuesLength)))
407
505
  if results.n == 0 then
408
506
  return _emptyFulfilledPromise
409
- elseif results.n == 1 and isPromise(results[1]) then
507
+ elseif results.n == 1 and Promise.isPromise(results[1]) then
410
508
  return results[1]
411
509
  else
412
510
  local promise = Promise.new()
@@ -1,5 +1,7 @@
1
- --- Utility methods for promise
2
- -- @module PromiseUtils
1
+ --[=[
2
+ Utility methods for promise
3
+ @class PromiseUtils
4
+ ]=]
3
5
 
4
6
  local require = require(script.Parent.loader).load(script)
5
7
 
@@ -7,10 +9,11 @@ local Promise = require("Promise")
7
9
 
8
10
  local PromiseUtils = {}
9
11
 
10
- --- Returns the value of the first promise resolved
11
- -- @constructor First
12
- -- @tparam Array(Promise) promises
13
- -- @treturn Promise Promise that resolves with first result
12
+ --[=[
13
+ Returns the value of the first promise resolved
14
+ @param promises { Promise<T> }
15
+ @return Promise<T> -- Promise that resolves with first result
16
+ ]=]
14
17
  function PromiseUtils.any(promises)
15
18
  local returnPromise = Promise.new()
16
19
 
@@ -29,10 +32,13 @@ function PromiseUtils.any(promises)
29
32
  return returnPromise
30
33
  end
31
34
 
32
- --- Executes all promises. If any fails, the result will be rejected. However, it yields until
33
- -- every promise is complete
34
- -- @constructor First
35
- -- @treturn Promise
35
+ --[=[
36
+ Executes all promises. If any fails, the result will be rejected. However, it yields until
37
+ every promise is complete.
38
+
39
+ @param promises { Promise<T> }
40
+ @return Promise<T>
41
+ ]=]
36
42
  function PromiseUtils.all(promises)
37
43
  if #promises == 0 then
38
44
  return Promise.resolved()
@@ -62,6 +68,13 @@ function PromiseUtils.all(promises)
62
68
  return returnPromise
63
69
  end
64
70
 
71
+ --[=[
72
+ Inverts the result of a promise, turning a resolved promise
73
+ into a rejected one, and a rejected one into a resolved one.
74
+
75
+ @param promise Promise<T>
76
+ @return Promise<T>
77
+ ]=]
65
78
  function PromiseUtils.invert(promise)
66
79
  if promise:IsPending() then
67
80
  return promise:Then(function(...)
@@ -79,6 +92,12 @@ function PromiseUtils.invert(promise)
79
92
  end
80
93
  end
81
94
 
95
+ --[=[
96
+ Creates a promise from a signal
97
+
98
+ @param signal Signal<T>
99
+ @return Promise<T>
100
+ ]=]
82
101
  function PromiseUtils.fromSignal(signal)
83
102
  local promise = Promise.new()
84
103
  local conn
@@ -95,6 +114,14 @@ function PromiseUtils.fromSignal(signal)
95
114
  return promise
96
115
  end
97
116
 
117
+ --[=[
118
+ Creates a new promise from the given promise that will
119
+ reject after the given `timeoutTime`
120
+
121
+ @param timeoutTime number
122
+ @param fromPromise Promise<T>
123
+ @return Promise<T>
124
+ ]=]
98
125
  function PromiseUtils.timeout(timeoutTime, fromPromise)
99
126
  assert(type(timeoutTime) == "number", "Bad timeoutTime")
100
127
  assert(fromPromise, "Bad fromPromise")
@@ -107,12 +134,11 @@ function PromiseUtils.timeout(timeoutTime, fromPromise)
107
134
 
108
135
  promise:Resolve(fromPromise)
109
136
 
110
- delay(timeoutTime, function()
137
+ task.delay(timeoutTime, function()
111
138
  promise:Reject()
112
139
  end)
113
140
 
114
141
  return promise
115
-
116
142
  end
117
143
 
118
144
  return PromiseUtils
@@ -1,5 +1,7 @@
1
- --- Tracks pending promises
2
- -- @classmod PendingPromiseTracker
1
+ --[=[
2
+ Tracks pending promises
3
+ @class PendingPromiseTracker
4
+ ]=]
3
5
 
4
6
  local PendingPromiseTracker = {}
5
7
  PendingPromiseTracker.ClassName = "PendingPromiseTracker"
@@ -1,6 +1,6 @@
1
- ---
2
- -- @module PromiseInstanceUtils
3
- -- @author Quenty
1
+ --[=[
2
+ @class PromiseInstanceUtils
3
+ ]=]
4
4
 
5
5
  local require = require(script.Parent.loader).load(script)
6
6
 
@@ -9,6 +9,10 @@ local Maid = require("Maid")
9
9
 
10
10
  local PromiseInstanceUtils = {}
11
11
 
12
+ --[=[
13
+ @param instance Instance
14
+ @return Promise
15
+ ]=]
12
16
  function PromiseInstanceUtils.promiseRemoved(instance)
13
17
  assert(instance:IsDescendantOf(game))
14
18
 
@@ -1,11 +1,22 @@
1
- --- Warps the WaitForChild API with a promise
2
- -- @module promiseChild
1
+ --[=[
2
+ Warps the WaitForChild API with a promise
3
+ @class promiseChild
4
+ ]=]
3
5
 
4
6
  local require = require(script.Parent.loader).load(script)
5
7
 
6
8
  local Promise = require("Promise")
7
9
 
8
- --- Wraps the :WaitForChild API with a promise
10
+ --[=[
11
+ Wraps the :WaitForChild API with a promise
12
+
13
+ @function promiseChild
14
+ @param parent Instance
15
+ @param name string
16
+ @param timeOut number?
17
+ @return Promise<Instance>
18
+ @within promiseChild
19
+ ]=]
9
20
  return function(parent, name, timeOut)
10
21
  local result = parent:FindFirstChild(name)
11
22
  if result then
@@ -1,5 +1,7 @@
1
- --- Promises a property value
2
- -- @module promisePropertyValue
1
+ --[=[
2
+ Promises a property value
3
+ @class promisePropertyValue
4
+ ]=]
3
5
 
4
6
  local require = require(script.Parent.loader).load(script)
5
7
 
@@ -1,5 +1,8 @@
1
- --- Wraps the wait()/delay() API in a promise
2
- -- @module promiseWait
1
+ --[=[
2
+ Wraps the wait()/delay() API in a promise
3
+
4
+ @class promiseWait
5
+ ]=]
3
6
 
4
7
  local require = require(script.Parent.loader).load(script)
5
8