@creejs/commons-retrier 2.1.11 → 2.1.13
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/cjs/index-dev.cjs +209 -4
- package/dist/cjs/index-dev.cjs.map +1 -1
- package/dist/cjs/index-min.cjs +1 -1
- package/dist/cjs/index-min.cjs.map +1 -1
- package/dist/esm/index-dev.js +209 -4
- package/dist/esm/index-dev.js.map +1 -1
- package/dist/esm/index-min.js +1 -1
- package/dist/esm/index-min.js.map +1 -1
- package/dist/umd/index.dev.js +209 -4
- package/dist/umd/index.dev.js.map +1 -1
- package/dist/umd/index.min.js +1 -1
- package/dist/umd/index.min.js.map +1 -1
- package/package.json +2 -2
- package/types/retrier.d.ts +109 -0
package/dist/cjs/index-dev.cjs
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
|
5
|
-
var t$1={constructorName:r$1,defaults:function(t,...r){if(null==t)throw new TypeError('"target" must not be null or undefined');for(const
|
|
5
|
+
var t$1={constructorName:r$1,defaults:function(t,...r){if(null==t)throw new TypeError('"target" must not be null or undefined');for(const e of r)if(null!=e)for(const r in e) void 0===t[r]&&(t[r]=e[r]);return t},extend:e$1,extends:e$1,equals:function(t,r){if(t===r)return true;if("function"==typeof t?.equals)return t.equals(r);if("function"==typeof r?.equals)return r.equals(t);return false},isBrowser:n$1,isNode:function(){return !n$1()}};function r$1(t){return t?.constructor?.name}function e$1(t,...r){if(null==t)throw new TypeError('"target" must not be null or undefined');for(const e of r)if(null!=e)for(const r in e)t[r]=e[r];return t}function n$1(){return "undefined"!=typeof window&&"undefined"!=typeof document}var o$1={isNumber:h$1,isNil:a$1};function u$1(t){return "function"==typeof t}function a$1(t){return null==t}function c$1(t){return !!h$1(t)&&t>0}function l$1(t){return !!h$1(t)&&t>=0}function h$1(t){return null!=t&&"number"==typeof t}function m$1(t){return null!=t&&"function"==typeof t.then}function b$1(t){return null!=t&&"string"==typeof t}var L$1={assertNumber:C,assertPositive:q,assertNotNegative:D,assertFunction:M,assertNotNil:W,assertString:J};function k(t,r){if(!Array.isArray(t))throw new Error(`${r?'"'+r+'" ':" "}Not Array: type=${typeof t} value=${JSON.stringify(t)}`)}function J(t,r){if(!b$1(t))throw new Error(`${r?'"'+r+'" ':" "}Not String: type=${typeof t} value=${JSON.stringify(t)}`)}function C(t,r){if(!h$1(t))throw new Error(`${r?'"'+r+'" ':" "}Not Number: type=${typeof t} value=${JSON.stringify(t)}`)}function q(t,r){if(!c$1(t))throw new Error(`${r?'"'+r+'" ':" "}Not Positive: ${t}`)}function D(t,r){if(!l$1(t))throw new Error(`${r?'"'+r+'" ':" "}Not "0 or Positive": ${t}`)}function M(t,r){if(!u$1(t))throw new Error(`${r?'"'+r+'" ':" "}Not Function: type=${typeof t} value=${JSON.stringify(t)}`)}function R(t,r){if(!m$1(t))throw new Error(`${r?'"'+r+'" ':" "}Not Promise: type=${typeof t} value=${JSON.stringify(t)}`)}function W(t,r){if(a$1(t))throw new Error((r?'"'+r+'" ':" ")+"Should Not Nil")}var Y={defer:Z,delay:function(t,r){h$1(t)?(r=t,t=Promise.resolve()):null==t&&null==r&&(r=1,t=Promise.resolve());null!=t&&R(t),C(r=r??1e3);const e=Z(),n=Date.now();return t.then((...t)=>{const o=Date.now()-n;o<r?setTimeout(()=>e.resolve(...t),r-o):e.resolve(...t);}).catch(t=>{const o=Date.now()-n;o<r?setTimeout(()=>e.reject(t),r-o):e.reject(t);}),e.promise},timeout:function(t,r,e){R(t),C(r=r??1);const n=Z(r,e),o=Date.now();return t.then((...t)=>{Date.now()-o<=r?n.resolve(...t):n.reject(new Error(e??`Promise Timeout: ${r}ms`));}).catch(t=>{!n.resolved&&!n.rejected&&n.reject(t);}),n.promise},allSettled:_$1,returnValuePromised:tt,series:async function(t){k(t);const r=[];for(const e of t)M(e),r.push(await e());return r},seriesAllSettled:async function(t){k(t);const r=[];for(const e of t){M(e);try{r.push({ok:!0,result:await e()});}catch(t){r.push({ok:false,result:t});}}return r},parallel:async function(t,r=5){if(k(t),C(r),r<=0)throw new Error(`Invalid maxParallel: ${r}, should > 0`);t.forEach(t=>M(t));const e=[];if(t.length<=r){const r=await Promise.all(t.map(t=>tt(t)));return e.push(...r),e}const n=[];for(const o of t)if(M(o),n.push(o),n.length>=r){const t=await Promise.all(n.map(t=>tt(t)));e.push(...t),n.length=0;}if(n.length>0&&n.length<r){const t=await Promise.all(n.map(t=>tt(t)));e.push(...t);}return e},parallelAllSettled:async function(t,r=5){if(k(t),C(r),r<=0)throw new Error(`Invalid maxParallel: ${r}, should > 0`);t.forEach(t=>M(t));const e=[];if(t.length<=r){const r=await _$1(t.map(t=>tt(t)));return e.push(...r),e}const n=[];for(const o of t)if(M(o),n.push(o),n.length>=r){const t=await _$1(n.map(t=>tt(t)));e.push(...t),n.length=0;}if(n.length>0&&n.length<r){const t=await _$1(n.map(t=>tt(t)));e.push(...t);}return e},wait:function(t){D(t);const r={};let e;return r.timerHandler=e=setTimeout(()=>{clearTimeout(e),r.resolve();},t),r.promise=new Promise((t,n)=>{r.resolve=r=>{null!=e&&clearTimeout(e),t(r);};}),r}};function Z(t=-1,r){C(t);const e={};let n;return t>=0&&(e.timerHandler=n=setTimeout(()=>{clearTimeout(n),e.timerCleared=true,e.reject(new Error(r??`Promise Timeout: ${t}ms`));},t)),e.promise=new Promise((t,r)=>{e.resolve=r=>{null!=n&&(clearTimeout(n),e.timerCleared=true),e.resolved=true,t(r);},e.reject=t=>{null!=n&&(clearTimeout(n),e.timerCleared=true),e.rejected=true,r(t);};}),e.promise.cancel=()=>{null!=n&&(clearTimeout(n),e.timerCleared=true),e.rejected=true,e.canceled=e.promise.canceled=true,e.reject(new Error("Cancelled"));},e}async function _$1(t){k(t);const r=await Promise.allSettled(t),e=[];for(const t of r)"fulfilled"===t.status&&e.push({ok:true,result:t.value}),"rejected"===t.status&&e.push({ok:false,result:t.reason});return e}function tt(t){try{const r=t();return m$1(r)?r:Promise.resolve(r)}catch(t){return Promise.reject(t)}}new TextDecoder;new TextEncoder;
|
|
6
6
|
|
|
7
7
|
// module vars
|
|
8
8
|
const DefaultMinInterval = 50;
|
|
@@ -154,7 +154,7 @@ class Policy {
|
|
|
154
154
|
}
|
|
155
155
|
}
|
|
156
156
|
|
|
157
|
-
var e={isFunction:t,isNil:s};function t(e){return "function"==typeof e}function s(e){return null==e}function n(e){return null!=e&&"string"==typeof e}var r={assertNumber:function(e,t){if(!function(e){return null!=e&&"number"==typeof e}(e))throw new Error(`${t?'"'+t+'" ':" "}Not Number: type=${typeof e} value=${JSON.stringify(e)}`)},assertFunction:function(e,s){if(!t(e))throw new Error(`${s?'"'+s+'" ':" "}Not Function: type=${typeof e} value=${JSON.stringify(e)}`)},assertNotNil:function(e,t){if(s(e))throw new Error((t?'"'+t+'" ':" ")+"Should Not Nil")},assertString:function(e,t){if(!n(e))throw new Error(`${t?'"'+t+'" ':" "}Not String: type=${typeof e} value=${JSON.stringify(e)}`)},assertStringOrSymbol:function(e,t){if(!n(e)&&!function(e){return null!=e&&"symbol"==typeof e}(e))throw new Error(`${t?'"'+t+'" ':" "}Not String or Symbol: type=${typeof e} value=${JSON.stringify(e)}`)}};new TextDecoder,new TextEncoder;const i="DOwner$#$",{assertFunction:l,assertNotNil:a}=r;class o{constructor(e,t,s=false){a(e,"event"),l(t,"callback"),this._event=e,this._callback=t,this._isOnce=!!s,this._owner=void 0;}set owner(e){this._owner=e;}get owner(){return this._owner===i?void 0:this._owner}get event(){return this._event}get isOnce(){return this._isOnce}isSameCallback(e){return this._callback===e}get callback(){return this._callback}invoke(...e){try{return this._callback(...e)}finally{if(this._isOnce)try{this._event._remove(this);}catch(e){console.warn(e);}}}listener(...e){return this.invoke(...e)}}const{isFunction:c,isNil:h}=e,{assertStringOrSymbol:u,assertFunction:_}=r;class f{static get DefaultOwner(){return i}constructor(e){u(e,"eventName"),this._name=e,this._callbacks=new Set,this._listeners=[],this._callback2Listeners=new Map,this._listener2Owner=new Map,this._owner2Listeners=new Map;}get name(){return this._name}isEmpty(){return 0===this._callbacks.size}rawListeners(){return [...this._listeners]}listenerCount(e){return null==e?this._listeners.length:this._callback2Listeners.get(e)?.size??0}callbacks(){return [...this.rawListeners().map(e=>e.callback)]}emit(...e){if(0===this._listeners.length)return false;for(const t of [...this._listeners])t.invoke(...e);return true}hasListener(e){return !!c(e)&&this._callbacks.has(e)}hasOwner(e){return !h(e)&&this._owner2Listeners.has(e)}addListener(e,t){return this._addListener(e,t,false,false)}prependListener(e,t){return this._addListener(e,t,false,true)}addOnceListener(e,t){return this._addListener(e,t,true,false)}prependOnceListener(e,t){return this._addListener(e,t,true,true)}_addListener(e,t,s,n){if(h(e))return false;_(e),this._callbacks.has(e)||this._callbacks.add(e),t=t??i;const r=new o(this,e,s);r.owner=t,n?this._listeners.unshift(r):this._listeners.push(r),this._listener2Owner.set(r,t);let l=this._callback2Listeners.get(e);null==l&&(l=new Set,this._callback2Listeners.set(e,l)),l.add(r);let a=this._owner2Listeners.get(t);return null==a&&(a=new Set,this._owner2Listeners.set(t,a)),a.add(r),true}removeListener(e){if(h(e))return false;if(!this._callbacks.has(e))return false;this._callbacks.delete(e);const t=this._callback2Listeners.get(e);if(null==t)return false;this._callback2Listeners.delete(e);for(const e of t){ -1!==this._listeners.indexOf(e)&&this._listeners.splice(this._listeners.indexOf(e),1);const t=this._listener2Owner.get(e);if(null==t)continue;this._listener2Owner.delete(e);const s=this._owner2Listeners.get(t);null!=s&&(s.delete(e),0===s.size&&this._owner2Listeners.delete(t));}return true}_remove(e){const t=this._listeners.indexOf(e);-1!==t&&this._listeners.splice(t,1);const{callback:s}=e,n=this._callback2Listeners.get(s);null!=n&&(n.delete(e),0===n.size&&(this._callback2Listeners.delete(s),this._callbacks.delete(s)));const r=this._listener2Owner.get(e);if(null==r)return;this._listener2Owner.delete(e);const i=this._owner2Listeners.get(r);null!=i&&(i.delete(e),0===i.size&&this._owner2Listeners.delete(r));}removeAllListeners(e){if(h(e))return this._callbacks.clear(),this._listeners.length=0,this._callback2Listeners.clear(),this._listener2Owner.clear(),this._owner2Listeners.clear(),this;const t=this._owner2Listeners.get(e);if(null==t)return this;this._owner2Listeners.delete(e);for(const e of t){ -1!==this._listeners.indexOf(e)&&this._listeners.splice(this._listeners.indexOf(e),1),this._listener2Owner.delete(e);const{callback:t}=e,s=this._callback2Listeners.get(t);null!=s&&(s.delete(e),0===s.size&&(this._callback2Listeners.delete(t),this._callbacks.delete(t)));}return this}}const{isNil:
|
|
157
|
+
var e={isFunction:t,isNil:s};function t(e){return "function"==typeof e}function s(e){return null==e}function n(e){return null!=e&&"string"==typeof e}var r={assertNumber:function(e,t){if(!function(e){return null!=e&&"number"==typeof e}(e))throw new Error(`${t?'"'+t+'" ':" "}Not Number: type=${typeof e} value=${JSON.stringify(e)}`)},assertFunction:function(e,s){if(!t(e))throw new Error(`${s?'"'+s+'" ':" "}Not Function: type=${typeof e} value=${JSON.stringify(e)}`)},assertNotNil:function(e,t){if(s(e))throw new Error((t?'"'+t+'" ':" ")+"Should Not Nil")},assertString:function(e,t){if(!n(e))throw new Error(`${t?'"'+t+'" ':" "}Not String: type=${typeof e} value=${JSON.stringify(e)}`)},assertStringOrSymbol:function(e,t){if(!n(e)&&!function(e){return null!=e&&"symbol"==typeof e}(e))throw new Error(`${t?'"'+t+'" ':" "}Not String or Symbol: type=${typeof e} value=${JSON.stringify(e)}`)}};new TextDecoder,new TextEncoder;const i="DOwner$#$",{assertFunction:l,assertNotNil:a}=r;class o{constructor(e,t,s=false){a(e,"event"),l(t,"callback"),this._event=e,this._callback=t,this._isOnce=!!s,this._owner=void 0;}set owner(e){this._owner=e;}get owner(){return this._owner===i?void 0:this._owner}get event(){return this._event}get isOnce(){return this._isOnce}isSameCallback(e){return this._callback===e}get callback(){return this._callback}invoke(...e){try{return this._callback(...e)}finally{if(this._isOnce)try{this._event._remove(this);}catch(e){console.warn(e);}}}listener(...e){return this.invoke(...e)}}const{isFunction:c,isNil:h}=e,{assertStringOrSymbol:u,assertFunction:_}=r;class f{static get DefaultOwner(){return i}constructor(e){u(e,"eventName"),this._name=e,this._callbacks=new Set,this._listeners=[],this._callback2Listeners=new Map,this._listener2Owner=new Map,this._owner2Listeners=new Map;}get name(){return this._name}isEmpty(){return 0===this._callbacks.size}rawListeners(){return [...this._listeners]}listenerCount(e){return null==e?this._listeners.length:this._callback2Listeners.get(e)?.size??0}callbacks(){return [...this.rawListeners().map(e=>e.callback)]}emit(...e){if(0===this._listeners.length)return false;for(const t of [...this._listeners])t.invoke(...e);return true}emitOwner(e,...t){if(0===this._listeners.length)return false;const s=this._owner2Listeners.get(e);if(null==s)return false;for(const e of [...s])e.invoke(...t);return true}hasListener(e){return !!c(e)&&this._callbacks.has(e)}hasOwner(e){return !h(e)&&this._owner2Listeners.has(e)}addListener(e,t){return this._addListener(e,t,false,false)}prependListener(e,t){return this._addListener(e,t,false,true)}addOnceListener(e,t){return this._addListener(e,t,true,false)}prependOnceListener(e,t){return this._addListener(e,t,true,true)}_addListener(e,t,s,n){if(h(e))return false;_(e),this._callbacks.has(e)||this._callbacks.add(e),t=t??i;const r=new o(this,e,s);r.owner=t,n?this._listeners.unshift(r):this._listeners.push(r),this._listener2Owner.set(r,t);let l=this._callback2Listeners.get(e);null==l&&(l=new Set,this._callback2Listeners.set(e,l)),l.add(r);let a=this._owner2Listeners.get(t);return null==a&&(a=new Set,this._owner2Listeners.set(t,a)),a.add(r),true}removeListener(e){if(h(e))return false;if(!this._callbacks.has(e))return false;this._callbacks.delete(e);const t=this._callback2Listeners.get(e);if(null==t)return false;this._callback2Listeners.delete(e);for(const e of t){ -1!==this._listeners.indexOf(e)&&this._listeners.splice(this._listeners.indexOf(e),1);const t=this._listener2Owner.get(e);if(null==t)continue;this._listener2Owner.delete(e);const s=this._owner2Listeners.get(t);null!=s&&(s.delete(e),0===s.size&&this._owner2Listeners.delete(t));}return true}_remove(e){const t=this._listeners.indexOf(e);-1!==t&&this._listeners.splice(t,1);const{callback:s}=e,n=this._callback2Listeners.get(s);null!=n&&(n.delete(e),0===n.size&&(this._callback2Listeners.delete(s),this._callbacks.delete(s)));const r=this._listener2Owner.get(e);if(null==r)return;this._listener2Owner.delete(e);const i=this._owner2Listeners.get(r);null!=i&&(i.delete(e),0===i.size&&this._owner2Listeners.delete(r));}removeAllListeners(e){if(h(e))return this._callbacks.clear(),this._listeners.length=0,this._callback2Listeners.clear(),this._listener2Owner.clear(),this._owner2Listeners.clear(),this;const t=this._owner2Listeners.get(e);if(null==t)return this;this._owner2Listeners.delete(e);for(const e of t){ -1!==this._listeners.indexOf(e)&&this._listeners.splice(this._listeners.indexOf(e),1),this._listener2Owner.delete(e);const{callback:t}=e,s=this._callback2Listeners.get(t);null!=s&&(s.delete(e),0===s.size&&(this._callback2Listeners.delete(t),this._callbacks.delete(t)));}return this}}const{isNil:m}=e,{assertString:d,assertFunction:w,assertNumber:L,assertStringOrSymbol:v,assertNotNil:g}=r,O=["on","once","addListener","prependListener","prependOnceListener","off","offAll","offOwner","removeAllListeners","removeListener","emit","emitOwner","setMaxListeners","getMaxListeners","hasOwner","listeners","listenerCount","eventNames","rawListeners"];let p=10;class b{static mixin(e){const t=new b;e.__emitter=t;for(const s of O){const n=t[s];e[s]=n.bind(t);}return e}static get defaultMaxListeners(){return p}static set defaultMaxListeners(e){L(e),p=e??10;}constructor(){this._name2Event=new Map,this._maxListeners=p;}addListener(e,t,s){return this.on(e,t,s)}prependListener(e,t,s){d(e),w(t),this._checkMaxListeners(e);return this._getOrCreateEvent(e).prependListener(t,s),this}prependOnceListener(e,t,s){d(e),w(t),this._checkMaxListeners(e);return this._getOrCreateEvent(e).prependOnceListener(t,s),this}emit(e,...t){const s=this._name2Event.get(e);return null!=s&&!s.isEmpty()&&(s.emit(...t),true)}emitOwner(e,t,...s){if(null==t)throw new Error('Missing "owner"');const n=this._name2Event.get(e);return null!=n&&!n.isEmpty()&&(n.emitOwner(t,...s),true)}eventNames(){return [...this._name2Event.keys()]}getMaxListeners(){return this._maxListeners}listenerCount(e,t){v(e,"eventName");const s=this._name2Event.get(e);return null==s||s.isEmpty()?0:s.listenerCount(t)}listeners(e){v(e,"eventName");const t=this._name2Event.get(e);return null==t||t.isEmpty()?[]:t.callbacks()}off(e,t){const s=this._name2Event.get(e);return null==s?this:(s.removeListener(t),s.isEmpty()?(this._name2Event.delete(e),this):this)}offAll(e,t){v(e,"eventName");const s=this._name2Event.get(e);return null==s?this:(s.removeAllListeners(t),s.isEmpty()?(this._name2Event.delete(e),this):this)}offOwner(e){g(e,"owner");const t=[...this._name2Event.values()];for(const s of t)s.removeAllListeners(e),s.isEmpty()&&this._name2Event.delete(s.name);return this}on(e,t,s){d(e),w(t),this._checkMaxListeners(e);return this._getOrCreateEvent(e).addListener(t,s),this}_checkMaxListeners(e){let t=0;0!==this._maxListeners&&this._maxListeners!==1/0&&(t=this.listenerCount(e))>=this._maxListeners&&console.warn(`maxlistenersexceededwarning: Possible EventEmitter memory leak detected. ${t} ${e} listeners added to [${this}]. Use emitter.setMaxListeners() to increase limit`);}once(e,t,s){d(e),w(t);return this._getOrCreateEvent(e).addOnceListener(t,s),this}rawListeners(e){return this._name2Event.get(e)?.rawListeners()||[]}removeAllListeners(e,t){return this.offAll(e,t)}removeListener(e,t){return this.off(e,t)}setMaxListeners(e){if(L(e),e<0)throw new RangeError("maxListeners must >=0");return this._maxListeners=e,this}_getOrCreateEvent(e){if(this._name2Event.has(e))return this._name2Event.get(e);const t=new f(e);return this._name2Event.set(e,t),t}hasOwner(e){if(m(e))return false;for(const t of this._name2Event.values())if(t.hasOwner(e))return true;return false}}
|
|
158
158
|
|
|
159
159
|
const Start = 'start'; // retry started
|
|
160
160
|
const Stop = 'stop'; // retry stopped
|
|
@@ -512,12 +512,201 @@ const TaskTimoutFlag = '!#@%$&^*';
|
|
|
512
512
|
* @extends EventEmitter
|
|
513
513
|
*/
|
|
514
514
|
class Retrier {
|
|
515
|
+
/**
|
|
516
|
+
* Creates a new Retrier instance with the specified name.
|
|
517
|
+
* @param {string} name - The name to assign to the retrier.
|
|
518
|
+
* @returns {Retrier} A new Retrier instance with the given name.
|
|
519
|
+
*/
|
|
520
|
+
static naming (name) {
|
|
521
|
+
const retrier = new Retrier();
|
|
522
|
+
retrier.name(name);
|
|
523
|
+
return retrier
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
/**
|
|
527
|
+
* Creates and returns a Retrier instance configured for infinite retries.
|
|
528
|
+
* @returns {Retrier} A Retrier instance with infinite retry behavior.
|
|
529
|
+
*/
|
|
530
|
+
static infinite () {
|
|
531
|
+
const retrier = new Retrier();
|
|
532
|
+
retrier.infinite();
|
|
533
|
+
return retrier
|
|
534
|
+
}
|
|
535
|
+
|
|
536
|
+
/**
|
|
537
|
+
* Creates a retrier configured to attempt an operation a specified number of times.
|
|
538
|
+
* @param {number} times - The maximum number of retry attempts.
|
|
539
|
+
* @returns {Retrier} A configured Retrier instance with the specified max retries.
|
|
540
|
+
*/
|
|
541
|
+
static times (times) {
|
|
542
|
+
const retrier = new Retrier();
|
|
543
|
+
retrier.times(times);
|
|
544
|
+
return retrier
|
|
545
|
+
}
|
|
546
|
+
|
|
547
|
+
/**
|
|
548
|
+
* Alias for times.
|
|
549
|
+
* @param {number} maxRetries - The maximum number of retry attempts.
|
|
550
|
+
* @returns {Retrier} A configured Retrier instance with the specified max retries.
|
|
551
|
+
*/
|
|
552
|
+
static maxRetries (maxRetries) {
|
|
553
|
+
const retrier = new Retrier();
|
|
554
|
+
retrier.maxRetries(maxRetries);
|
|
555
|
+
return retrier
|
|
556
|
+
}
|
|
557
|
+
|
|
558
|
+
/**
|
|
559
|
+
* Sets the minimum Interval for the retrier and returns the instance.
|
|
560
|
+
* @param {number} min - The minimum Interval in milliseconds.
|
|
561
|
+
* @returns {Retrier} The retrier instance with updated minimum Interval.
|
|
562
|
+
*/
|
|
563
|
+
static min (min) {
|
|
564
|
+
const retrier = new Retrier();
|
|
565
|
+
retrier.min(min);
|
|
566
|
+
return retrier
|
|
567
|
+
}
|
|
568
|
+
|
|
569
|
+
/**
|
|
570
|
+
* Sets the maximum Interval for the retrier and returns the instance.
|
|
571
|
+
* @param {number} max - The maximum Interval in milliseconds.
|
|
572
|
+
* @returns {Retrier} The retrier instance with updated maximum Interval.
|
|
573
|
+
*/
|
|
574
|
+
static max (max) {
|
|
575
|
+
const retrier = new Retrier();
|
|
576
|
+
retrier.max(max);
|
|
577
|
+
return retrier
|
|
578
|
+
}
|
|
579
|
+
|
|
580
|
+
/**
|
|
581
|
+
* Creates a retrier with the specified Interval range.
|
|
582
|
+
* @param {number} min - Minimum Interval.
|
|
583
|
+
* @param {number} max - Maximum Interval.
|
|
584
|
+
* @returns {Retrier} A new Retrier instance configured with specified Interval range.
|
|
585
|
+
*/
|
|
586
|
+
static range (min, max) {
|
|
587
|
+
const retrier = new Retrier();
|
|
588
|
+
retrier.range(min, max);
|
|
589
|
+
return retrier
|
|
590
|
+
}
|
|
591
|
+
|
|
592
|
+
/**
|
|
593
|
+
* Creates a retrier with a fixed interval between attempts.
|
|
594
|
+
* @param {number} fixedInterval - The fixed interval in milliseconds between retry attempts.
|
|
595
|
+
* @returns {Retrier} A new Retrier instance configured with the specified fixed interval.
|
|
596
|
+
*/
|
|
597
|
+
static fixedInterval (fixedInterval) {
|
|
598
|
+
const retrier = new Retrier();
|
|
599
|
+
retrier.fixedInterval(fixedInterval);
|
|
600
|
+
return retrier
|
|
601
|
+
}
|
|
602
|
+
|
|
603
|
+
/**
|
|
604
|
+
* Creates a retrier with a fixed backoff strategy.
|
|
605
|
+
* @param {number} fixedInterval - The fixed interval between retries in milliseconds.
|
|
606
|
+
* @param {number} [jitter=500] - The maximum jitter to add to the interval in milliseconds.
|
|
607
|
+
* @returns {Retrier} A retrier instance configured with fixed backoff.
|
|
608
|
+
*/
|
|
609
|
+
static fixedBackoff (fixedInterval, jitter = 500) {
|
|
610
|
+
const retrier = new Retrier();
|
|
611
|
+
retrier.fixedBackoff(fixedInterval, jitter);
|
|
612
|
+
return retrier
|
|
613
|
+
}
|
|
614
|
+
|
|
615
|
+
/**
|
|
616
|
+
* Creates a retrier with a fixed increase strategy.
|
|
617
|
+
* @param {number} increasement - The fixed amount to increase on each retry.
|
|
618
|
+
* @returns {Retrier} A retrier instance configured with fixed increase.
|
|
619
|
+
*/
|
|
620
|
+
static fixedIncrease (increasement) {
|
|
621
|
+
const retrier = new Retrier();
|
|
622
|
+
retrier.fixedIncrease(increasement);
|
|
623
|
+
return retrier
|
|
624
|
+
}
|
|
625
|
+
|
|
626
|
+
/**
|
|
627
|
+
* Creates a retrier with a fixed increase strategy.
|
|
628
|
+
* @param {number} increasement - The fixed amount to increase on each retry.
|
|
629
|
+
* @param {number} [jitter=500] - The maximum jitter to add to the interval in milliseconds.
|
|
630
|
+
* @returns {Retrier} A retrier instance configured with fixed increase.
|
|
631
|
+
*/
|
|
632
|
+
static linearBackoff (increasement, jitter = 500) {
|
|
633
|
+
const retrier = new Retrier();
|
|
634
|
+
retrier.linearBackoff(increasement, jitter);
|
|
635
|
+
return retrier
|
|
636
|
+
}
|
|
637
|
+
|
|
638
|
+
/**
|
|
639
|
+
* Creates a new Retrier instance with factor-increase strategy.
|
|
640
|
+
* @param {number} factor - The factor by which to increase the interval on each retry.
|
|
641
|
+
* @returns {Retrier} A new Retrier instance with factor-increase strategy.
|
|
642
|
+
*/
|
|
643
|
+
static factorIncrease (factor) {
|
|
644
|
+
const retrier = new Retrier();
|
|
645
|
+
retrier.factorIncrease(factor);
|
|
646
|
+
return retrier
|
|
647
|
+
}
|
|
648
|
+
|
|
649
|
+
/**
|
|
650
|
+
* Creates a new Retrier instance with exponential-backoff strategy.
|
|
651
|
+
* @param {number} [jitter=500]
|
|
652
|
+
* @returns {Retrier} A new Retrier instance
|
|
653
|
+
*/
|
|
654
|
+
static exponentialBackoff (jitter = 500) {
|
|
655
|
+
const retrier = new Retrier();
|
|
656
|
+
retrier.exponentialBackoff(jitter);
|
|
657
|
+
return retrier
|
|
658
|
+
}
|
|
659
|
+
|
|
660
|
+
/**
|
|
661
|
+
* Creates a Retrier instance with a shuttle-interval strategt.
|
|
662
|
+
* @param {number} stepLength - The interval step length of each change
|
|
663
|
+
* @returns {Retrier} A configured Retrier instance with shuttle-interval strategt.
|
|
664
|
+
*/
|
|
665
|
+
static shuttleInterval (stepLength) {
|
|
666
|
+
const retrier = new Retrier();
|
|
667
|
+
retrier.shuttleInterval(stepLength);
|
|
668
|
+
return retrier
|
|
669
|
+
}
|
|
670
|
+
|
|
671
|
+
/**
|
|
672
|
+
* Creates a Retrier instance with a total-opertion timeout.
|
|
673
|
+
* @param {number} timeout - The timeout value in milliseconds.
|
|
674
|
+
* @returns {Retrier} A Retrier instance configured with the given timeout.
|
|
675
|
+
*/
|
|
676
|
+
static timeout (timeout) {
|
|
677
|
+
const retrier = new Retrier();
|
|
678
|
+
retrier.timeout(timeout);
|
|
679
|
+
return retrier
|
|
680
|
+
}
|
|
681
|
+
|
|
682
|
+
/**
|
|
683
|
+
* Creates a retrier instance with a single-task timeout.
|
|
684
|
+
* @param {number} timeout - The timeout duration in milliseconds for the retrier task.
|
|
685
|
+
* @returns {Retrier} A Retrier instance configured with the given task timeout.
|
|
686
|
+
*/
|
|
687
|
+
static taskTimeout (timeout) {
|
|
688
|
+
const retrier = new Retrier();
|
|
689
|
+
retrier.taskTimeout(timeout);
|
|
690
|
+
return retrier
|
|
691
|
+
}
|
|
692
|
+
|
|
693
|
+
/**
|
|
694
|
+
* Creates a new Retrier instance, sets the task to be retried, and starts the retry process.
|
|
695
|
+
* @param {Function} task - The asynchronous task static to be retried.
|
|
696
|
+
* @returns {Promise<*>} A promise that resolves when the retry process completes.
|
|
697
|
+
*/
|
|
698
|
+
static start (task) {
|
|
699
|
+
const retrier = new Retrier();
|
|
700
|
+
retrier.task(task);
|
|
701
|
+
return retrier.start()
|
|
702
|
+
}
|
|
703
|
+
|
|
515
704
|
/**
|
|
516
705
|
* Creates a new Retrier instance with a fixed interval policy.
|
|
517
706
|
* @param {number} [fixedInterval=1000] - The fixed interval in milliseconds between retry attempts. Defaults to 1000ms if not provided.
|
|
518
707
|
*/
|
|
519
708
|
constructor (fixedInterval) {
|
|
520
|
-
|
|
709
|
+
b.mixin(this);
|
|
521
710
|
/**
|
|
522
711
|
* @type {Policy}
|
|
523
712
|
*/
|
|
@@ -926,7 +1115,10 @@ class Retrier {
|
|
|
926
1115
|
this._taskingFlag.reject(lastError ?? new Error(`Max Retries Exceeded, Retring ${this._currentRetries} times > max ${this._maxRetries}`));
|
|
927
1116
|
break
|
|
928
1117
|
}
|
|
929
|
-
|
|
1118
|
+
// use Waiter to introduce the ability to wakeup at any time
|
|
1119
|
+
this._sleepWaiter = Y.wait(nextDelay);
|
|
1120
|
+
await this._sleepWaiter.promise;
|
|
1121
|
+
// await PromiseUtils.delay(nextDelay)
|
|
930
1122
|
}
|
|
931
1123
|
this._taskingFlag.promise.finally(() => {
|
|
932
1124
|
this.resetRetryPolicy();
|
|
@@ -938,6 +1130,19 @@ class Retrier {
|
|
|
938
1130
|
return this._taskingFlag.promise
|
|
939
1131
|
}
|
|
940
1132
|
|
|
1133
|
+
/**
|
|
1134
|
+
* Force an execution immediately.
|
|
1135
|
+
*/
|
|
1136
|
+
wakeup () {
|
|
1137
|
+
if (!this.running) {
|
|
1138
|
+
this.start();
|
|
1139
|
+
return
|
|
1140
|
+
}
|
|
1141
|
+
if (this._sleepWaiter != null) {
|
|
1142
|
+
this._sleepWaiter.resolve();
|
|
1143
|
+
}
|
|
1144
|
+
}
|
|
1145
|
+
|
|
941
1146
|
/**
|
|
942
1147
|
* Stops the retrier with an optional reason. If already stopping, returns the existing break promise.
|
|
943
1148
|
* @param {Error} [reason] - Optional reason for stopping (defaults to 'Manually Stop' error).
|