@creejs/commons-lang 2.0.0 → 2.0.2

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.
@@ -1,317 +0,0 @@
1
- // @ts-nocheck
2
- 'use strict'
3
-
4
- /**
5
- * @module PromiseUtils
6
- * @description Promise utility functions for enhanced promise handling, including timeout, delay, parallel execution, and series execution.
7
- */
8
-
9
- const { assertNumber, assertPromise, assertArray, assertFunction } = require('./type-assert')
10
- const { isPromise, isNumber } = require('./type-utils')
11
- /**
12
- * Creates a "Deferred" Object with Timeout support
13
- * 1. timeout=-1, it means no timeout check
14
- * @param {number} [timeout=-1] - Timeout duration in milliseconds
15
- * @param {string} [timeoutMessage]
16
- * @returns {{promise: Promise<*>, reject: function, resolve: function}}
17
- */
18
- function defer (timeout = -1, timeoutMessage) {
19
- assertNumber(timeout)
20
- const rtnVal = {}
21
-
22
- let timerHandler
23
- if (timeout >= 0) {
24
- rtnVal.timerHandler = timerHandler = setTimeout(() => {
25
- clearTimeout(timerHandler) // must clear it
26
- rtnVal.timerCleared = true // easy to check in test case
27
- rtnVal.reject(new Error(timeoutMessage ?? `Promise Timeout: ${timeout}ms`))
28
- }, timeout)
29
- rtnVal.timerHandler = timerHandler
30
- }
31
-
32
- rtnVal.promise = new Promise((resolve, reject) => {
33
- rtnVal.resolve = (...args) => {
34
- if (timerHandler != null) {
35
- clearTimeout(timerHandler) // must clear it
36
- rtnVal.timerCleared = true // easy to check in test case
37
- }
38
- rtnVal.resolved = true
39
- resolve(...args)
40
- }
41
-
42
- rtnVal.reject = (err) => {
43
- if (timerHandler != null) {
44
- clearTimeout(timerHandler) // must clear it
45
- rtnVal.timerCleared = true // easy to check in test case
46
- }
47
- rtnVal.rejected = true
48
- reject(err)
49
- }
50
- })
51
- rtnVal.promise.cancel = () => {
52
- if (timerHandler != null) {
53
- clearTimeout(timerHandler) // must clear it
54
- rtnVal.timerCleared = true // easy to check in test case
55
- }
56
- rtnVal.rejected = true // easy to check in test case
57
- rtnVal.canceled = rtnVal.promise.canceled = true // easy to check in test case
58
- rtnVal.reject(new Error('Cancelled'))
59
- }
60
- return rtnVal
61
- }
62
-
63
- /**
64
- * Creates a timeout wrapper around a promise that rejects if the promise doesn't resolve within the given time.
65
- * @param {Promise<*>} promise - The promise to wrap with a timeout
66
- * @param {number} [time=1] - Timeout duration in milliseconds
67
- * @param {string} [message=`Promise Timeout: ${time}ms`] - Custom rejection message
68
- * @returns {Promise<*>} A new promise that either resolves with the original promise's value, rejects with original promise's error or timeout error
69
- * @throws {TypeError} If input is not a promise or timeout is not a number
70
- */
71
- function timeout (promise, time, message) {
72
- assertPromise(promise)
73
-
74
- time = time ?? 1
75
- assertNumber(time)
76
-
77
- const deferred = PromiseUtils.defer(time, message)
78
- const startTs = Date.now()
79
- promise.then((...args) => { // original promise settled, but timeout
80
- const elapsed = Date.now() - startTs
81
- if (elapsed <= time) {
82
- deferred.resolve(...args)
83
- } else {
84
- deferred.reject(new Error(message ?? `Promise Timeout: ${time}ms`))
85
- }
86
- }).catch((err) => {
87
- // prevent double reject
88
- !deferred.resolved && !deferred.rejected && deferred.reject(err)
89
- })
90
- return deferred.promise
91
- }
92
-
93
- /**
94
- * Excutes All promises in parallel and returns an array of results.
95
- *
96
- * Why:
97
- * 1. Promise.allSettled() returns
98
- * * { status: 'fulfilled', value: any } when promise fulfills
99
- * * { status: 'rejected', reason: any } when promise rejects
100
- * 2. It's NOT convenient to use Promise.allSettled() to get the results of all promises.
101
- * * the data structure is not consistent when fullfilled or rejected
102
- * * have to check "string" type of status to know sucess or failure
103
- * @param {Promise} promises
104
- * @returns {Array<{ok: boolean, result: any}>}
105
- */
106
- async function allSettled (promises) {
107
- assertArray(promises)
108
- const results = await Promise.allSettled(promises)
109
- const rtnVal = []
110
- for (const result of results) {
111
- if (result.status === 'fulfilled') {
112
- rtnVal.push({ ok: true, result: result.value })
113
- }
114
- if (result.status === 'rejected') {
115
- rtnVal.push({ ok: false, result: result.reason })
116
- }
117
- }
118
- return rtnVal
119
- }
120
-
121
- /**
122
- * Execute the task Function, and ensure it returns a Promise.
123
- * @param {function} task
124
- * @returns {Promise<*>}
125
- */
126
- function returnValuePromised (task) {
127
- try {
128
- const taskRtnVal = task()
129
- if (isPromise(taskRtnVal)) {
130
- return taskRtnVal
131
- }
132
- return Promise.resolve(taskRtnVal)
133
- } catch (e) {
134
- return Promise.reject(e)
135
- }
136
- }
137
-
138
- /**
139
- * Delays a promise by a specified time.
140
- * 1. delay(), wait 1ms
141
- * 2. delay(1000), wait 1000ms
142
- * 3. delay(promise), after promise settled, wait 1000ms
143
- * 4. delay(promise, 2000), after promise settled, wait 2000ms
144
- *
145
- * @param {Promise<*>|number|undefined} [promise] - The input promise to delay
146
- * @param {number|undefined} [ms] - Minimum delay in milliseconds (default: 1)
147
- * @returns {Promise} A new promise that settles after the delay period
148
- */
149
- function delay (promise, ms) {
150
- if (isNumber(promise)) {
151
- ms = promise
152
- promise = Promise.resolve()
153
- } else if (promise == null && ms == null) {
154
- ms = 1
155
- promise = Promise.resolve()
156
- }
157
- promise != null && assertPromise(promise)
158
- ms = ms ?? 1000
159
- assertNumber(ms)
160
- const deferred = PromiseUtils.defer()
161
- const startTs = Date.now()
162
- promise
163
- .then((...args) => {
164
- const escaped = Date.now() - startTs
165
- if (escaped < ms) {
166
- setTimeout(() => deferred.resolve(...args), ms - escaped)
167
- } else {
168
- deferred.resolve(...args)
169
- }
170
- })
171
- .catch((err) => {
172
- const escaped = Date.now() - startTs
173
- if (escaped < ms) {
174
- setTimeout(() => deferred.reject(err), ms - escaped)
175
- } else {
176
- deferred.reject(err)
177
- }
178
- })
179
- return deferred.promise
180
- }
181
-
182
- /**
183
- * Fast-Fail mode to execute Tasks(functions) in series (one after another) and returns their results in order.
184
- * 1. function are executed one by one
185
- * 2. Fast Fail: if any tasks fail, the whole chain is rejected with the first error
186
- * 3. if an element is not function, rejects the whole chain with Error(Not Function)
187
- * @param {Function[]} promises
188
- * @returns {Promise<Array>} Promise that resolves with an array of results in the same order as input tasks
189
- */
190
- async function series (tasks) {
191
- assertArray(tasks)
192
- const results = []
193
- for (const task of tasks) {
194
- assertFunction(task)
195
- results.push(await task())
196
- }
197
- return results
198
- }
199
-
200
- /**
201
- * AllSettled Mode to execute Tasks(functions) in series (one after another) and returns their results in order.
202
- * 1. tasks are executed one by one
203
- * 2. Each result is an object with `ok` (boolean) and `result` (resolved value or error).
204
- * 3. if a task is not Function, rejects the whole chain with Error(Not Function)
205
- * @param {Function[]} tasks
206
- * @returns {Promise<Array<{ok: boolean, result: *}>>}
207
- */
208
- async function seriesAllSettled (tasks) {
209
- assertArray(tasks)
210
- const results = []
211
- for (const task of tasks) {
212
- assertFunction(task)
213
- try {
214
- results.push({ ok: true, result: await task() })
215
- } catch (err) {
216
- results.push({ ok: false, result: err })
217
- }
218
- }
219
- return results
220
- }
221
-
222
- /**
223
- * FastFail Mode to Execute tasks in parallel with a maximum concurrency limit
224
- * 1. tasks are executed in parallel with a maximum concurrency limit
225
- * 2. rejects whole chain with the first error, when first task fails
226
- * @param {Function[]} tasks
227
- * @param {number} [maxParallel=5]
228
- * @returns {Promise<Array>} Array of resolved values from all promises
229
- * @throws {TypeError} If input is not an array of function or maxParallel is not a number
230
- */
231
- async function parallel (tasks, maxParallel = 5) {
232
- assertArray(tasks)
233
- assertNumber(maxParallel)
234
- if (maxParallel <= 0) {
235
- throw new Error(`Invalid maxParallel: ${maxParallel}, should > 0`)
236
- }
237
- tasks.forEach((task) => assertFunction(task))
238
- const rtnVal = []
239
- // once for all, run all tasks
240
- if (tasks.length <= maxParallel) {
241
- const resultsForBatch = await Promise.all(tasks.map(task => PromiseUtils.returnValuePromised(task)))
242
- rtnVal.push(...resultsForBatch)
243
- return rtnVal
244
- }
245
- // run group by MaxParallel
246
- const tasksToRun = []
247
- for (const task of tasks) {
248
- assertFunction(task)
249
- tasksToRun.push(task)
250
- if (tasksToRun.length >= maxParallel) {
251
- const resultsForBatch = await Promise.all(tasksToRun.map(task => PromiseUtils.returnValuePromised(task)))
252
- rtnVal.push(...resultsForBatch)
253
- tasksToRun.length = 0
254
- }
255
- }
256
- // Run all rested
257
- if (tasksToRun.length > 0 && tasksToRun.length < maxParallel) {
258
- const resultsForBatch = await Promise.all(tasksToRun.map(task => PromiseUtils.returnValuePromised(task)))
259
- rtnVal.push(...resultsForBatch)
260
- }
261
- return rtnVal
262
- }
263
-
264
- /**
265
- * AllSettled Mode to execute tasks in parallel with a maximum concurrency limit
266
- * 1. tasks are executed in parallel with a maximum concurrency limit
267
- * 2. all tasks will be executed, even some of them failed.
268
- * @param {Function[]} tasks
269
- * @param {number} [maxParallel=5] - Maximum number of tasks to run in parallel
270
- * @returns {Promise<Array>} Array of resolved values from all promises
271
- * @throws {TypeError} If input is not an array of function or maxParallel is not a number
272
- */
273
- async function parallelAllSettled (tasks, maxParallel = 5) {
274
- assertArray(tasks)
275
- assertNumber(maxParallel)
276
- if (maxParallel <= 0) {
277
- throw new Error(`Invalid maxParallel: ${maxParallel}, should > 0`)
278
- }
279
- tasks.forEach((task) => assertFunction(task))
280
- const rtnVal = []
281
- // once for all, run all promises
282
- if (tasks.length <= maxParallel) {
283
- const resultsForBatch = await PromiseUtils.allSettled(tasks.map(task => PromiseUtils.returnValuePromised(task)))
284
- rtnVal.push(...resultsForBatch)
285
- return rtnVal
286
- }
287
- // run group by MaxParallel
288
- const tasksToRun = []
289
- for (const task of tasks) {
290
- assertFunction(task)
291
- tasksToRun.push(task)
292
- if (tasksToRun.length >= maxParallel) {
293
- const resultsForBatch = await PromiseUtils.allSettled(tasksToRun.map(task => PromiseUtils.returnValuePromised(task)))
294
- rtnVal.push(...resultsForBatch)
295
- tasksToRun.length = 0
296
- }
297
- }
298
- // Run all rested
299
- if (tasksToRun.length > 0 && tasksToRun.length < maxParallel) {
300
- const resultsForBatch = await PromiseUtils.allSettled(tasksToRun.map(task => PromiseUtils.returnValuePromised(task)))
301
- rtnVal.push(...resultsForBatch)
302
- }
303
- return rtnVal
304
- }
305
- const PromiseUtils = {
306
- defer,
307
- delay,
308
- timeout,
309
- allSettled,
310
- returnValuePromised,
311
- series,
312
- seriesAllSettled,
313
- parallel,
314
- parallelAllSettled
315
- }
316
-
317
- module.exports = PromiseUtils