@superutils/promise 1.1.3 → 1.1.5
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/dist/index.d.ts +73 -37
- package/dist/index.js +3 -5
- package/package.json +3 -3
package/dist/index.d.ts
CHANGED
|
@@ -81,17 +81,24 @@ type PromiseParams<T = unknown> = ConstructorParameters<typeof Promise<T>>;
|
|
|
81
81
|
type DeferredAsyncCallback<TArgs extends unknown[] | [] = []> = <TResult = unknown>(promise: Promise<TResult> | ((...args: TArgs) => Promise<TResult>)) => IPromisE<TResult>;
|
|
82
82
|
type DeferredAsyncGetPromise<T> = <TResult = T>() => Promise<TResult>;
|
|
83
83
|
/** Default options used by `PromisE.deferred` and related functions */
|
|
84
|
-
type DeferredAsyncDefaults = Pick<Required<DeferredAsyncOptions
|
|
84
|
+
type DeferredAsyncDefaults<ThisArg = unknown, Delay = unknown> = Pick<Required<DeferredAsyncOptions<ThisArg, Delay>>, 'delayMs' | 'resolveError' | 'resolveIgnored'>;
|
|
85
85
|
/** Options for `PromisE.deferred` and other related functions */
|
|
86
|
-
type DeferredAsyncOptions<ThisArg = unknown,
|
|
86
|
+
type DeferredAsyncOptions<ThisArg = unknown, Delay = unknown> = {
|
|
87
87
|
/**
|
|
88
88
|
* Delay in milliseconds, used for `debounce` and `throttle` modes. Use `0` for sequential execution.
|
|
89
89
|
*
|
|
90
90
|
* Use `0` to disable debounce/throttle and execute all operations sequentially.
|
|
91
91
|
*
|
|
92
|
-
* Default: `100` (or
|
|
92
|
+
* Default: `100` (or whatever is set in `PromisE.deferred.defaults.delayMs`)
|
|
93
|
+
*/
|
|
94
|
+
delayMs?: 0 | PositiveNumber<Delay>;
|
|
95
|
+
/**
|
|
96
|
+
* Whether to ignore (based on `resolveIgnored` settings) stale promises.
|
|
97
|
+
* In debouce/throttle mode, when an older promise is resolved after a newly resolved promise,
|
|
98
|
+
* the older one is considered stale.
|
|
99
|
+
*
|
|
100
|
+
* Default: `false`
|
|
93
101
|
*/
|
|
94
|
-
delayMs?: 0 | PositiveNumber<DelayMs>;
|
|
95
102
|
ignoreStale?: boolean;
|
|
96
103
|
/** Callback invoked whenever promise/function throws error */
|
|
97
104
|
onError?: (this: ThisArg, err: unknown) => ValueOrPromise<unknown>;
|
|
@@ -121,14 +128,15 @@ type DeferredAsyncOptions<ThisArg = unknown, DelayMs extends number = number> =
|
|
|
121
128
|
/** The value to be used as "thisArg" whenever any of the callbacks are invoked */
|
|
122
129
|
thisArg?: ThisArg;
|
|
123
130
|
} & (({
|
|
124
|
-
|
|
125
|
-
delayMs: PositiveNumber<DelayMs>;
|
|
131
|
+
delayMs: PositiveNumber<Delay>;
|
|
126
132
|
throttle: true;
|
|
127
133
|
} & Pick<ThrottleOptions, 'trailing'>) | ({
|
|
128
|
-
|
|
129
|
-
|
|
134
|
+
delayMs?: PositiveNumber<Delay>;
|
|
135
|
+
throttle?: false;
|
|
136
|
+
} & Pick<DeferredOptions, 'leading'>) | {
|
|
137
|
+
delayMs: 0;
|
|
130
138
|
throttle?: false;
|
|
131
|
-
}
|
|
139
|
+
});
|
|
132
140
|
/** Determines what to do when deferred promise/function fails */
|
|
133
141
|
declare enum ResolveError {
|
|
134
142
|
/** Neither resolve nor reject the failed */
|
|
@@ -354,34 +362,51 @@ type TimeoutOptions<Func extends string = 'all'> = {
|
|
|
354
362
|
* - debounced: when `delayMs > 0` and `throttle = false`
|
|
355
363
|
* - throttled: when `delayMs > 0` and `throttle = true`
|
|
356
364
|
*
|
|
357
|
-
*
|
|
358
|
-
* @example Explanation & example usage:
|
|
365
|
+
* @example Debounce calls
|
|
359
366
|
* ```typescript
|
|
360
|
-
* const example =
|
|
361
|
-
*
|
|
362
|
-
*
|
|
363
|
-
*
|
|
364
|
-
*
|
|
365
|
-
*
|
|
366
|
-
*
|
|
367
|
-
*
|
|
368
|
-
*
|
|
367
|
+
* const example = async (options = {}) => {
|
|
368
|
+
* const df = PromisE.deferred({
|
|
369
|
+
* delayMs: 100,
|
|
370
|
+
* resolveIgnored: ResolveIgnored.NEVER, // never resolve ignored calls
|
|
371
|
+
* ...options,
|
|
372
|
+
* })
|
|
373
|
+
* df(() => PromisE.delay(500)).then(console.log)
|
|
374
|
+
* df(() => PromisE.delay(1000)).then(console.log)
|
|
375
|
+
* df(() => PromisE.delay(5000)).then(console.log)
|
|
376
|
+
* // delay 2 seconds and invoke df() again
|
|
377
|
+
* await PromisE.delay(2000)
|
|
378
|
+
* df(() => PromisE.delay(200)).then(console.log)
|
|
369
379
|
* }
|
|
380
|
+
* example({ ignoreStale: false, throttle: false })
|
|
381
|
+
* // `200` and `1000` will be printed in the console
|
|
382
|
+
* example({ ignoreStale: true, throttle: false })
|
|
383
|
+
* // `200` will be printed in the console
|
|
384
|
+
* ```
|
|
370
385
|
*
|
|
371
|
-
*
|
|
372
|
-
*
|
|
373
|
-
*
|
|
386
|
+
* @example Throttle calls
|
|
387
|
+
* ```typescript
|
|
388
|
+
* const example = async (options = {}) => {
|
|
389
|
+
* const df = PromisE.deferred({
|
|
390
|
+
* delayMs: 100,
|
|
391
|
+
* resolveIgnored: ResolveIgnored.NEVER, // never resolve ignored calls
|
|
392
|
+
* ...options,
|
|
393
|
+
* })
|
|
394
|
+
* df(() => PromisE.delay(5000)).then(console.log)
|
|
395
|
+
* df(() => PromisE.delay(500)).then(console.log)
|
|
396
|
+
* df(() => PromisE.delay(1000)).then(console.log)
|
|
397
|
+
* // delay 2 seconds and invoke df() again
|
|
398
|
+
* await PromisE.delay(2000)
|
|
399
|
+
* df(() => PromisE.delay(200)).then(console.log)
|
|
400
|
+
* }
|
|
374
401
|
*
|
|
375
|
-
*
|
|
376
|
-
*
|
|
377
|
-
* // `5000` and `200` will be printed in the console
|
|
402
|
+
* example({ ignoreStale: true, throttle: true })
|
|
403
|
+
* // `200` will be printed in the console
|
|
378
404
|
*
|
|
379
|
-
*
|
|
380
|
-
*
|
|
381
|
-
* // `5000` will be printed in the console
|
|
405
|
+
* example({ ignoreStale: false, throttle: true })
|
|
406
|
+
* // `200` and `5000` will be printed in the console
|
|
382
407
|
* ```
|
|
383
408
|
*/
|
|
384
|
-
declare function deferred<T, ThisArg = unknown, Delay
|
|
409
|
+
declare function deferred<T = unknown, ThisArg = unknown, Delay = unknown>(options?: DeferredAsyncOptions<ThisArg, Delay>): DeferredAsyncCallback;
|
|
385
410
|
declare namespace deferred {
|
|
386
411
|
var defaults: {
|
|
387
412
|
/**
|
|
@@ -429,7 +454,7 @@ declare namespace deferred {
|
|
|
429
454
|
* // 200, 600, 1100
|
|
430
455
|
* ```
|
|
431
456
|
*/
|
|
432
|
-
declare function deferredCallback<TDefault, ThisArg, Delay
|
|
457
|
+
declare function deferredCallback<TDefault = unknown, ThisArg = unknown, Delay = unknown, CbArgs extends unknown[] = unknown[]>(callback: (...args: CbArgs) => TDefault | Promise<TDefault>, options?: DeferredAsyncOptions<ThisArg, Delay>): <TResult = TDefault>(...args: CbArgs) => IPromisE<TResult>;
|
|
433
458
|
|
|
434
459
|
/**
|
|
435
460
|
* Creates a promise that completes after given delay/duration.
|
|
@@ -444,16 +469,14 @@ declare function deferredCallback<TDefault, ThisArg, Delay extends number = numb
|
|
|
444
469
|
* Default: `delayMs` when resolved or timed out error when rejected
|
|
445
470
|
* @param asRejected (optional) if `true`, will reject the promise after the delay.
|
|
446
471
|
*
|
|
447
|
-
* @returns
|
|
472
|
+
* @returns promise
|
|
448
473
|
*
|
|
449
|
-
* @example
|
|
474
|
+
* @example Create a promise that resolves after specified delay.
|
|
450
475
|
* ```typescript
|
|
451
476
|
* import PromisE from '@superutils/promise'
|
|
452
477
|
*
|
|
453
|
-
*
|
|
454
|
-
*
|
|
455
|
-
* await PromisE.delay(3000)
|
|
456
|
-
* console.log('App ready')
|
|
478
|
+
* // Resolves after 1 second delay
|
|
479
|
+
* PromisE.delay(1000).then(console.log)
|
|
457
480
|
* ```
|
|
458
481
|
*
|
|
459
482
|
* @example Execute a function after delay.
|
|
@@ -463,6 +486,19 @@ declare function deferredCallback<TDefault, ThisArg, Delay extends number = numb
|
|
|
463
486
|
*
|
|
464
487
|
* PromisE.delay(1000, () => console.log('Prints after 1 second delay'))
|
|
465
488
|
* ```
|
|
489
|
+
*
|
|
490
|
+
* @example Delay before continuing execution
|
|
491
|
+
* ```typescript
|
|
492
|
+
* import PromisE from '@superutils/promise'
|
|
493
|
+
*
|
|
494
|
+
* const func = async () => {
|
|
495
|
+
* console.log('Waiting for app initialization or something else to be ready')
|
|
496
|
+
* // wait 3 seconds before proceeding
|
|
497
|
+
* await PromisE.delay(3000)
|
|
498
|
+
* console.log('App ready')
|
|
499
|
+
* }
|
|
500
|
+
* func()
|
|
501
|
+
* ```
|
|
466
502
|
*/
|
|
467
503
|
declare function delay<T = number, TReject extends boolean = boolean>(duration?: number, result?: T | (() => T), asRejected?: TReject): IPromisE_Delay<T>;
|
|
468
504
|
declare namespace delay {
|
package/dist/index.js
CHANGED
|
@@ -227,11 +227,9 @@ function deferred(options = {}) {
|
|
|
227
227
|
}
|
|
228
228
|
let items = [...queue.entries()];
|
|
229
229
|
if (throttle && options.trailing) {
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
);
|
|
234
|
-
} else if (!throttle && options.leading) {
|
|
230
|
+
const currentIndex = items.findIndex(([id]) => id === currentId);
|
|
231
|
+
items = items.slice(0, currentIndex);
|
|
232
|
+
} else if (!throttle) {
|
|
235
233
|
items = items.slice(0, -1);
|
|
236
234
|
}
|
|
237
235
|
handleIgnore(items, _prevQItem);
|
package/package.json
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
"url": "https://github.com/alien45/superutils/issues"
|
|
5
5
|
},
|
|
6
6
|
"dependencies": {
|
|
7
|
-
"@superutils/core": "^1.1.
|
|
7
|
+
"@superutils/core": "^1.1.5"
|
|
8
8
|
},
|
|
9
9
|
"description": "An extended Promise with extra features such as status tracking, deferred/throttled execution, timeout and retry mechanism.",
|
|
10
10
|
"files": [
|
|
@@ -23,7 +23,7 @@
|
|
|
23
23
|
"main": "dist/index.js",
|
|
24
24
|
"name": "@superutils/promise",
|
|
25
25
|
"peerDpendencies": {
|
|
26
|
-
"@superutils/core": "^1.1.
|
|
26
|
+
"@superutils/core": "^1.1.5"
|
|
27
27
|
},
|
|
28
28
|
"publishConfig": {
|
|
29
29
|
"access": "public"
|
|
@@ -43,5 +43,5 @@
|
|
|
43
43
|
"sideEffects": false,
|
|
44
44
|
"type": "module",
|
|
45
45
|
"types": "dist/index.d.ts",
|
|
46
|
-
"version": "1.1.
|
|
46
|
+
"version": "1.1.5"
|
|
47
47
|
}
|