@handy-common-utils/promise-utils 1.6.0 → 1.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.
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.promiseState = exports.synchronised = exports.synchronized = exports.timeoutReject = exports.timeoutResolve = exports.delayedReject = exports.delayedResolve = exports.inParallel = exports.withConcurrency = exports.withRetry = exports.repeat = exports.PromiseUtils = exports.PromiseState = exports.EXPONENTIAL_SEQUENCE = exports.FIBONACCI_SEQUENCE = void 0;
3
+ exports.runPeriodically = exports.promiseState = exports.synchronised = exports.synchronized = exports.timeoutReject = exports.timeoutResolve = exports.cancellableDelayedReject = exports.cancellableDelayedResolve = exports.delayedReject = exports.delayedResolve = exports.inParallel = exports.withConcurrency = exports.withRetry = exports.repeat = exports.PromiseUtils = exports.PromiseState = exports.EXPONENTIAL_SEQUENCE = exports.FIBONACCI_SEQUENCE = void 0;
4
4
  /**
5
5
  * Array of 25 Fibonacci numbers starting from 1 up to 317811.
6
6
  * It can be used to form your own backoff interval array.
@@ -205,31 +205,120 @@ class PromiseUtils {
205
205
  await Promise.all(promises);
206
206
  return jobResults;
207
207
  }
208
+ /**
209
+ * Creates a cancellable timer that will resolve after a specified number of milliseconds.
210
+ *
211
+ * The returned object contains:
212
+ * - `stop()` to cancel the scheduled resolution (if called before the timer fires). Calling
213
+ * `stop()` prevents the promise from being settled by this timer.
214
+ * - `promise` which will resolve with the supplied `result` (or the value returned by the
215
+ * `result` function) after `ms` milliseconds unless `stop()` is called first.
216
+ *
217
+ * Note: If the `result` is a function that returns a Promise, the returned `promise` will
218
+ * resolve with that Promise's resolution (i.e. it behaves like resolving with a PromiseLike).
219
+ *
220
+ * @param ms The number of milliseconds after which the scheduled resolution will occur.
221
+ * @param result The result to be resolved by the Promise, or a function that supplies the result.
222
+ * @returns An object with `stop()` and `promise`.
223
+ */
224
+ static cancellableDelayedResolve(ms, result) {
225
+ let stopped = false;
226
+ let timer;
227
+ const promise = new Promise(resolve => {
228
+ timer = setTimeout(() => {
229
+ timer = undefined;
230
+ if (stopped)
231
+ return;
232
+ resolve(typeof result === 'function' ? result() : result);
233
+ }, ms);
234
+ });
235
+ const stop = () => {
236
+ if (stopped)
237
+ return;
238
+ stopped = true;
239
+ if (timer !== undefined) {
240
+ clearTimeout(timer);
241
+ timer = undefined;
242
+ }
243
+ };
244
+ return { stop, promise };
245
+ }
208
246
  /**
209
247
  * Creates a Promise that resolves after a specified number of milliseconds.
210
248
  *
249
+ * The `result` argument may be:
250
+ * - a value to resolve with,
251
+ * - a PromiseLike whose resolution will be adopted by the returned Promise, or
252
+ * - a function which is invoked when the timer fires and may return a value or a PromiseLike.
253
+ *
254
+ * If `result` is a function, it is called when the timer elapses; if it returns a Promise,
255
+ * the returned Promise will adopt that Promise's outcome.
256
+ *
211
257
  * @param ms The number of milliseconds after which the created Promise will resolve.
212
258
  * @param result The result to be resolved by the Promise, or a function that supplies the result.
213
- * @returns A new Promise that resolves with the specified result after the specified delay.
259
+ * @returns A Promise that resolves with the specified result after the specified delay.
214
260
  */
215
261
  static delayedResolve(ms, result) {
216
- // eslint-disable-next-line no-promise-executor-return
217
- return new Promise(resolve => setTimeout(() => resolve(typeof result === 'function' ? result() : result), ms));
262
+ return PromiseUtils.cancellableDelayedResolve(ms, result).promise;
263
+ }
264
+ /**
265
+ * Creates a cancellable timer that will reject after a specified number of milliseconds.
266
+ *
267
+ * The returned object contains:
268
+ * - `stop()` to cancel the scheduled rejection (if called before the timer fires). Calling
269
+ * `stop()` prevents the promise from being settled by this timer.
270
+ * - `promise` which will reject with the supplied `reason` (or the value returned by the
271
+ * `reason` function) after `ms` milliseconds unless `stop()` is called first.
272
+ *
273
+ * If the `reason` is a PromiseLike that rejects, its rejection value will be used as the rejection reason.
274
+ *
275
+ * @param ms The number of milliseconds after which the scheduled rejection will occur.
276
+ * @param reason The reason for the rejection, or a function that supplies the reason.
277
+ * @returns An object with `stop()` and `promise`.
278
+ */
279
+ static cancellableDelayedReject(ms, reason) {
280
+ let stopped = false;
281
+ let timer;
282
+ const promise = new Promise((_resolve, reject) => {
283
+ timer = setTimeout(() => {
284
+ timer = undefined;
285
+ if (stopped)
286
+ return;
287
+ const r = typeof reason === 'function' ? reason() : reason;
288
+ // Resolve the possibly-PromiseLike `r` and reject the outer promise with either
289
+ // the resolved value or the rejection reason of `r`.
290
+ Promise.resolve(r).then(reject, reject);
291
+ }, ms);
292
+ });
293
+ const stop = () => {
294
+ if (stopped)
295
+ return;
296
+ stopped = true;
297
+ if (timer !== undefined) {
298
+ clearTimeout(timer);
299
+ timer = undefined;
300
+ }
301
+ };
302
+ return { stop, promise };
218
303
  }
219
304
  /**
220
305
  * Creates a Promise that rejects after a specified number of milliseconds.
221
306
  *
307
+ * The `reason` argument may be:
308
+ * - a value to reject with,
309
+ * - a PromiseLike whose rejection will be adopted by the returned Promise, or
310
+ * - a function which is invoked when the timer fires and may return a value or a PromiseLike.
311
+ *
312
+ * If `reason` is a function, it is called when the timer elapses; if it returns a Promise,
313
+ * the returned Promise will reject with that Promise's rejection reason (or reject with the
314
+ * returned value if it resolves).
315
+ *
222
316
  * @param ms The number of milliseconds after which the created Promise will reject.
223
317
  * @param reason The reason for the rejection, or a function that supplies the reason.
224
- * If the reason is a rejected Promise, the outcome of it will be the rejection reason of the returned Promise.
225
- * @returns A new Promise that rejects with the specified reason after the specified delay.
318
+ * @returns A Promise that rejects with the specified reason after the specified delay.
226
319
  */
227
320
  static delayedReject(ms, reason) {
228
- // eslint-disable-next-line no-promise-executor-return
229
- return new Promise((_resolve, reject) => setTimeout(() => {
230
- const r = typeof reason === 'function' ? reason() : reason;
231
- Promise.resolve(r).catch(error => error).then(r => reject(r));
232
- }, ms));
321
+ return PromiseUtils.cancellableDelayedReject(ms, reason).promise;
233
322
  }
234
323
  /**
235
324
  * Applies a timeout to a Promise or a function that returns a Promise.
@@ -334,50 +423,282 @@ class PromiseUtils {
334
423
  static async synchronised(lock, operation) {
335
424
  return PromiseUtils.synchronized(lock, operation);
336
425
  }
426
+ /**
427
+ * Runs an operation periodically with configurable intervals and stopping conditions.
428
+ *
429
+ * - `interval` may be a single number (ms), an array of numbers, or a function
430
+ * that receives the iteration number (starting at 1) and returns the next
431
+ * interval in milliseconds or `undefined` to stop.
432
+ * - If the interval array runs out of elements or the function returns `undefined`
433
+ * (or a negative value), no further invocations will be scheduled.
434
+ *
435
+ * Options:
436
+ * - `maxExecutions` stop after N runs (inclusive).
437
+ * - `maxDurationMs` stop after elapsed ms since the first scheduled start.
438
+ * - `schedule` controls how the interval is measured:
439
+ * - `'delayAfterEnd'`: wait the interval after the previous operation completes
440
+ * before scheduling the next one (equivalent to a fixed delay between ends).
441
+ * - `'delayBetweenStarts'`: keep start times on a regular schedule (interval measured
442
+ * between the starts of successive operations).
443
+ * The default schedule is `'delayBetweenStarts'`.
444
+ *
445
+ * Returns an object with `stop()` to cancel further executions and `done` which
446
+ * resolves when the periodic runner stops. If the provided `operation` throws or
447
+ * rejects, the `done` promise will reject with that error so callers can handle it.
448
+ *
449
+ * Note: The first invocation of `operation` is scheduled after the first interval
450
+ * elapses (i.e. this function does NOT call `operation` immediately). If you need
451
+ * an immediate run, invoke `operation(1)` yourself before calling `runPeriodically`.
452
+ *
453
+ * @template T The operation return type (ignored by the runner; used for typing).
454
+ * @param operation Function to run each iteration. Receives the iteration index (1-based).
455
+ * @param interval Number | number[] | ((iteration: number) => number|undefined) defining waits.
456
+ * @param options Optional configuration.
457
+ * @returns An object containing `stop()` to cancel further executions and `done` Promise
458
+ * which resolves when the periodic runner stops (or rejects if the operation errors).
459
+ */
460
+ static runPeriodically(operation, interval, options) {
461
+ let stopped = false;
462
+ let timer;
463
+ let waitResolve;
464
+ const stop = () => {
465
+ stopped = true;
466
+ if (timer !== undefined) {
467
+ clearTimeout(timer);
468
+ timer = undefined;
469
+ }
470
+ if (waitResolve) {
471
+ // resolve the pending wait so the runner can notice `stopped` and exit
472
+ const r = waitResolve;
473
+ waitResolve = undefined;
474
+ r();
475
+ }
476
+ };
477
+ const getInterval = (iteration) => {
478
+ if (Array.isArray(interval)) {
479
+ return interval[iteration - 1];
480
+ }
481
+ if (typeof interval === 'function') {
482
+ return interval(iteration);
483
+ }
484
+ return interval;
485
+ };
486
+ const done = (async () => {
487
+ var _a;
488
+ const startTime = Date.now();
489
+ let iteration = 0;
490
+ // lastStart tracks the start time of the previous iteration (used by delayBetweenStarts)
491
+ let lastStart = Date.now();
492
+ while (!stopped) {
493
+ const nextIteration = iteration + 1;
494
+ const nextInterval = getInterval(nextIteration);
495
+ if (nextInterval == null || nextInterval < 0)
496
+ break;
497
+ const schedule = (_a = options === null || options === void 0 ? void 0 : options.schedule) !== null && _a !== void 0 ? _a : 'delayBetweenStarts';
498
+ const waitMs = schedule === 'delayBetweenStarts'
499
+ ? Math.max(0, lastStart + nextInterval - Date.now())
500
+ : nextInterval;
501
+ // wait (cancelable via stop() which clears the timeout and resolves the wait)
502
+ await new Promise(resolve => {
503
+ waitResolve = resolve;
504
+ timer = setTimeout(() => { timer = undefined; waitResolve = undefined; resolve(); }, waitMs);
505
+ });
506
+ waitResolve = undefined;
507
+ if (stopped)
508
+ break;
509
+ iteration = nextIteration;
510
+ lastStart = Date.now();
511
+ // let errors propagate to the done promise so caller can decide handling
512
+ await operation(iteration);
513
+ if ((options === null || options === void 0 ? void 0 : options.maxExecutions) && iteration >= options.maxExecutions)
514
+ break;
515
+ if ((options === null || options === void 0 ? void 0 : options.maxDurationMs) && (Date.now() - startTime) >= options.maxDurationMs)
516
+ break;
517
+ }
518
+ })();
519
+ return { stop, done };
520
+ }
337
521
  }
338
522
  exports.PromiseUtils = PromiseUtils;
339
523
  PromiseUtils.synchronizationLocks = new Map();
340
524
  /**
341
- * See {@link PromiseUtils.repeat} for full documentation.
525
+ * Executes an operation repeatedly and collects all the results.
526
+ * This function is very useful for many scenarios, such like client-side pagination.
527
+ *
528
+ * @param operation A function that takes a parameter as input and returns a result. Typically, the parameter has optional fields to control paging.
529
+ * @param nextParameter A function for calculating the next parameter from the operation result. Normally, this parameter controls paging. This function should return null when no further invocation of the operation function is desired. If further invocation is desired, the return value of this function can be a Promise or a non-Promise value.
530
+ * @param collect A function for merging the operation result into the collection.
531
+ * @param initialCollection The initial collection, which will be the first argument passed to the first invocation of the collect function.
532
+ * @param initialParameter The parameter for the first operation.
533
+ * @returns A promise that resolves to a collection of all the results returned by the operation function.
342
534
  */
343
535
  exports.repeat = PromiseUtils.repeat;
344
536
  /**
345
- * See {@link PromiseUtils.withRetry} for full documentation.
537
+ * Repeatedly performs an operation until a specified criteria is met.
538
+ *
539
+ * @param operation A function that outputs a Promise result. Typically, the operation does not use its arguments.
540
+ * @param backoff An array of retry backoff periods (in milliseconds) or a function for calculating them.
541
+ * @param shouldRetry A predicate function for deciding whether another call to the operation should occur.
542
+ * @returns A promise of the operation result, potentially with retries applied.
346
543
  */
347
544
  exports.withRetry = PromiseUtils.withRetry;
348
545
  /**
349
- * See {@link PromiseUtils.withConcurrency} for full documentation.
546
+ * Executes multiple jobs/operations with a specified level of concurrency.
547
+ *
548
+ * @param concurrency The number of jobs/operations to run concurrently.
549
+ * @param jobs The job data to be processed. This function can handle an infinite or unknown number of elements safely.
550
+ * @param operation The function that processes job data asynchronously.
551
+ * @returns A promise that resolves to an array containing the results from the operation function.
552
+ * The results in the returned array are in the same order as the corresponding elements in the jobs array.
350
553
  */
351
554
  exports.withConcurrency = PromiseUtils.withConcurrency;
352
555
  /**
353
- * See {@link PromiseUtils.inParallel} for full documentation.
556
+ * Executes multiple jobs/operations in parallel. By default, all operations are executed regardless of any failures.
557
+ * In most cases, using withConcurrency might be more convenient.
558
+ *
559
+ * By default, this function does not throw or reject an error when any job/operation fails.
560
+ * Errors from operations are returned alongside results in the returned array.
561
+ * This function only resolves when all jobs/operations are settled (either resolved or rejected).
562
+ *
563
+ * If options.abortOnError is set to true, this function throws (or rejects with) an error immediately when any job/operation fails.
564
+ * In this mode, some jobs/operations may not be executed if one fails.
565
+ *
566
+ * @param parallelism The number of jobs/operations to run concurrently.
567
+ * @param jobs The job data to be processed. This function can safely handle an infinite or unknown number of elements.
568
+ * @param operation The function that processes job data asynchronously.
569
+ * @param options Options to control the function's behavior.
570
+ * @param options.abortOnError If true, the function aborts and throws an error on the first failed operation.
571
+ * @returns A promise that resolves to an array containing the results of the operations.
572
+ * Each element is either a fulfilled result or a rejected error/reason.
573
+ * The results or errors in the returned array are in the same order as the corresponding elements in the jobs array.
354
574
  */
355
575
  exports.inParallel = PromiseUtils.inParallel;
356
576
  /**
357
- * See {@link PromiseUtils.delayedResolve} for full documentation.
577
+ * Creates a Promise that resolves after a specified number of milliseconds.
578
+ *
579
+ * The result argument may be:
580
+ * - a value to resolve with,
581
+ * - a PromiseLike whose resolution will be adopted by the returned Promise, or
582
+ * - a function which is invoked when the timer fires and may return a value or a PromiseLike.
583
+ *
584
+ * If result is a function, it is called when the timer elapses; if it returns a Promise,
585
+ * the returned Promise will adopt that Promise's outcome.
586
+ *
587
+ * @param ms The number of milliseconds after which the created Promise will resolve.
588
+ * @param result The result to be resolved by the Promise, or a function that supplies the result.
589
+ * @returns A Promise that resolves with the specified result after the specified delay.
358
590
  */
359
591
  exports.delayedResolve = PromiseUtils.delayedResolve;
360
592
  /**
361
- * See {@link PromiseUtils.delayedReject} for full documentation.
593
+ * Creates a Promise that rejects after a specified number of milliseconds.
594
+ *
595
+ * The reason argument may be:
596
+ * - a value to reject with,
597
+ * - a PromiseLike whose rejection will be adopted by the returned Promise, or
598
+ * - a function which is invoked when the timer fires and may return a value or a PromiseLike.
599
+ *
600
+ * If reason is a function, it is called when the timer elapses; if it returns a Promise,
601
+ * the returned Promise will reject with that Promise's rejection reason (or reject with the
602
+ * returned value if it resolves).
603
+ *
604
+ * @param ms The number of milliseconds after which the created Promise will reject.
605
+ * @param reason The reason for the rejection, or a function that supplies the reason.
606
+ * @returns A Promise that rejects with the specified reason after the specified delay.
362
607
  */
363
608
  exports.delayedReject = PromiseUtils.delayedReject;
364
609
  /**
365
- * See {@link PromiseUtils.timeoutResolve} for full documentation.
610
+ * Creates a cancellable timer that will resolve after a specified number of milliseconds.
611
+ *
612
+ * The returned object contains:
613
+ * - stop() to cancel the scheduled resolution (if called before the timer fires). Calling
614
+ * stop() prevents the promise from being settled by this timer.
615
+ * - promise which will resolve with the supplied result (or the value returned by the
616
+ * result function) after ms milliseconds unless stop() is called first.
617
+ *
618
+ * If the result is a PromiseLike, its resolution value will be used as the resolved value.
619
+ *
620
+ * @param ms The number of milliseconds after which the scheduled resolution will occur.
621
+ * @param result The result to be resolved by the Promise, or a function that supplies the result.
622
+ * @returns An object with stop() and promise.
623
+ */
624
+ exports.cancellableDelayedResolve = PromiseUtils.cancellableDelayedResolve;
625
+ /**
626
+ * Creates a cancellable timer that will reject after a specified number of milliseconds.
627
+ *
628
+ * The returned object contains:
629
+ * - stop() to cancel the scheduled rejection (if called before the timer fires). Calling
630
+ * stop() prevents the promise from being settled by this timer.
631
+ * - promise which will reject with the supplied reason (or the value returned by the
632
+ * reason function) after ms milliseconds unless stop() is called first.
633
+ *
634
+ * If the reason is a PromiseLike that rejects, its rejection value will be used as the rejection reason.
635
+ *
636
+ * @param ms The number of milliseconds after which the scheduled rejection will occur.
637
+ * @param reason The reason for the rejection, or a function that supplies the reason.
638
+ * @returns An object with stop() and promise.
639
+ */
640
+ exports.cancellableDelayedReject = PromiseUtils.cancellableDelayedReject;
641
+ /**
642
+ * Applies a timeout to a Promise or a function that returns a Promise.
643
+ * If the timeout occurs, the returned Promise resolves to the specified result.
644
+ * If the timeout does not occur, the returned Promise resolves or rejects based on the outcome of the original Promise.
645
+ * If the result parameter is a function and the timeout does not occur, the function will not be called.
646
+ * Note: The rejection of the operation parameter is not handled by this function.
647
+ * You may want to handle it outside this function to avoid warnings like "(node:4330) PromiseRejectionHandledWarning: Promise rejection was handled asynchronously."
648
+ *
649
+ * @param operation The original Promise or a function that returns a Promise to which the timeout will be applied.
650
+ * @param ms The number of milliseconds for the timeout.
651
+ * @param result The result to resolve with if the timeout occurs, or a function that supplies the result.
652
+ * @returns A new Promise that resolves to the specified result if the timeout occurs.
366
653
  */
367
654
  exports.timeoutResolve = PromiseUtils.timeoutResolve;
368
655
  /**
369
- * See {@link PromiseUtils.timeoutReject} for full documentation.
656
+ * Applies a timeout to a Promise or a function that returns a Promise.
657
+ * If the timeout occurs, the returned Promise rejects with the specified reason.
658
+ * If the timeout does not occur, the returned Promise resolves or rejects based on the outcome of the original Promise.
659
+ * If the rejectReason parameter is a function and the timeout does not occur, the function will not be called.
660
+ * Note: The rejection of the operation parameter is not handled by this function. You may want to handle it outside this function to avoid warnings like "(node:4330) PromiseRejectionHandledWarning: Promise rejection was handled asynchronously."
661
+ *
662
+ * @param operation The original Promise or a function that returns a Promise to which the timeout will be applied.
663
+ * @param ms The number of milliseconds for the timeout.
664
+ * @param rejectReason The reason to reject with if the timeout occurs, or a function that supplies the reason.
665
+ * @returns A new Promise that rejects with the specified reason if the timeout occurs.
370
666
  */
371
667
  exports.timeoutReject = PromiseUtils.timeoutReject;
372
668
  /**
373
- * See {@link PromiseUtils.synchronized} for full documentation.
669
+ * Provides mutual exclusion similar to synchronized in Java.
670
+ * Ensures no concurrent execution of any operation function associated with the same lock.
671
+ * The operation function has access to the state (when synchronized is called),
672
+ * settledState (when the operation function is called),
673
+ * and result (either the fulfilled result or the rejected reason) of the previous operation.
674
+ * If there is no previous invocation, state, settledState, and result will all be undefined.
675
+ *
676
+ * @param lock The object (such as a string, a number, or this in a class) used to identify the lock.
677
+ * @param operation The function that performs the computation and returns a Promise.
678
+ * @returns The result of the operation function.
374
679
  */
375
680
  exports.synchronized = PromiseUtils.synchronized;
376
681
  /**
377
- * See {@link PromiseUtils.synchronised} for full documentation.
682
+ * This is just another spelling of synchronized.
683
+ * @param lock The object (such as a string, a number, or this in a class) used to identify the lock.
684
+ * @param operation The function that performs the computation and returns a Promise.
685
+ * @returns The result of the operation function.
378
686
  */
379
687
  exports.synchronised = PromiseUtils.synchronised;
380
688
  /**
381
- * See {@link PromiseUtils.promiseState} for full documentation.
689
+ * Retrieves the state of the specified Promise.
690
+ * Note: The returned value is a Promise that resolves immediately.
691
+ *
692
+ * @param p The Promise whose state is to be determined.
693
+ * @returns A Promise that resolves immediately with the state of the input Promise.
382
694
  */
383
695
  exports.promiseState = PromiseUtils.promiseState;
696
+ /**
697
+ * Runs an operation periodically with configurable intervals and stopping conditions.
698
+ *
699
+ * @param operation The operation to run periodically.
700
+ * @param interval The interval (ms), array of intervals, or function returning interval per iteration.
701
+ * @param options Options for maxExecutions, maxDurationMs, and schedule type.
702
+ * @returns An object with stop() and done Promise which resolves when the periodic runner stops (or rejects if the operation errors).
703
+ */
704
+ exports.runPeriodically = PromiseUtils.runPeriodically;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@handy-common-utils/promise-utils",
3
- "version": "1.6.0",
3
+ "version": "1.7.0",
4
4
  "description": "Promise related utilities",
5
5
  "scripts": {
6
6
  "pretest": "eslint . --ext .ts",