poll-until-promise
Advanced tools
Comparing version
@@ -1,2 +0,2 @@ | ||
!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define("PollUntil",[],e):"object"==typeof exports?exports.PollUntil=e():t.PollUntil=e()}(this,(function(){return function(t){var e={};function i(r){if(e[r])return e[r].exports;var s=e[r]={i:r,l:!1,exports:{}};return t[r].call(s.exports,s,s.exports,i),s.l=!0,s.exports}return i.m=t,i.c=e,i.d=function(t,e,r){i.o(t,e)||Object.defineProperty(t,e,{enumerable:!0,get:r})},i.r=function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},i.t=function(t,e){if(1&e&&(t=i(t)),8&e)return t;if(4&e&&"object"==typeof t&&t&&t.__esModule)return t;var r=Object.create(null);if(i.r(r),Object.defineProperty(r,"default",{enumerable:!0,value:t}),2&e&&"string"!=typeof t)for(var s in t)i.d(r,s,function(e){return t[e]}.bind(null,s));return r},i.n=function(t){var e=t&&t.__esModule?function(){return t.default}:function(){return t};return i.d(e,"a",e),e},i.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},i.p="",i(i.s=0)}([function(t,e,i){t.exports=i(1)},function(t,e,i){(function(e){const i="Your executor is not a function. functions and promises are valid.",r="Failed to wait";class s{constructor({Promise:t=e.Promise,setTimeout:i,interval:r=100,timeout:s=1e3,stopOnFailure:o=!1,verbose:n=!1,message:u}={}){this._PromiseModule=t,this._setTimeoutModule=i,this._interval=r,this._timeout=s,this._stopOnFailure=o,this._isWaiting=!1,this._isResolved=!1,this._verbose=n,this._userMessage=u,this.originalStacktraceError=new Error,this._Console=console}tryEvery(t){return this._interval=t,this}stopAfter(t){return this._timeout=t,this}execute(t){return this._executeFn=t,this._applyPromiseHandlers(),this._validateExecution(),this.start=Date.now(),this._isWaiting=!0,this._log("starting to execute"),this._runFunction(),this.promise}getPromise(){return this.promise}isResolved(){return this._isResolved}isWaiting(){return this._isWaiting}stopOnFailure(t){return this._stopOnFailure=t,this}_applyPromiseHandlers(){this.promise=new this._PromiseModule((t,e)=>{this.resolve=t,this.reject=e})}_validateExecution(){if("function"!=typeof this._executeFn)throw new Error(i)}_timeFromStart(){return Date.now()-this.start}_shouldStopTrying(){return this._timeFromStart()>this._timeout}_executeAgain(){this._log("executing again"),"function"==typeof this._setTimeoutModule?this._setTimeoutModule(this._runFunction.bind(this),this._interval):setTimeout(this._runFunction.bind(this),this._interval)}_failedToWait(){let t=`${r} after ${this._timeFromStart()}ms`;if(this._userMessage&&(t=`${t}: ${this._userMessage}`),this._lastError){this._lastError.message=`${t}\n${this._lastError.message}`;const e=this.originalStacktraceError.stack;this._lastError.stack+=e.substring(e.indexOf("\n")+1)}else this._lastError=this.originalStacktraceError,this._lastError.message=t;return this._log(this._lastError),this._lastError}_runFunction(){if(this._shouldStopTrying())return this._isWaiting=!1,void this.reject(this._failedToWait());let t=this._executeFn();"object"==typeof t&&"function"==typeof t.then||(t=new this._PromiseModule(e=>e(t))),t.then(t=>{if(!1===t)return this._log(`then execute again with result: ${t}`),void this._executeAgain();this.resolve(t),this._isWaiting=!1,this._isResolved=!0,this._log(`then done waiting with result: ${t}`)}).catch(t=>this._stopOnFailure?(this._log(`stopped on failure with err: ${t}`),this.reject(t)):(this._lastError=t,this._log(`catch with err: ${t}`),this._executeAgain()))}_log(t){this._verbose&&this._Console&&this._Console.log&&this._Console.log(t)}}t.exports={PollUntil:s,waitFor:(t,e)=>new s(e).execute(t)}}).call(this,i(2))},function(t,e){var i;i=function(){return this}();try{i=i||new Function("return this")()}catch(t){"object"==typeof window&&(i=window)}t.exports=i}])})); | ||
!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define("PollUntil",[],e):"object"==typeof exports?exports.PollUntil=e():t.PollUntil=e()}(this,(function(){return function(t){var e={};function i(r){if(e[r])return e[r].exports;var s=e[r]={i:r,l:!1,exports:{}};return t[r].call(s.exports,s,s.exports,i),s.l=!0,s.exports}return i.m=t,i.c=e,i.d=function(t,e,r){i.o(t,e)||Object.defineProperty(t,e,{enumerable:!0,get:r})},i.r=function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},i.t=function(t,e){if(1&e&&(t=i(t)),8&e)return t;if(4&e&&"object"==typeof t&&t&&t.__esModule)return t;var r=Object.create(null);if(i.r(r),Object.defineProperty(r,"default",{enumerable:!0,value:t}),2&e&&"string"!=typeof t)for(var s in t)i.d(r,s,function(e){return t[e]}.bind(null,s));return r},i.n=function(t){var e=t&&t.__esModule?function(){return t.default}:function(){return t};return i.d(e,"a",e),e},i.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},i.p="",i(i.s=0)}([function(t,e,i){t.exports=i(1)},function(t,e,i){(function(e){const i="Your executor is not a function. functions and promises are valid.",r="Failed to wait";class s{constructor({Promise:t=e.Promise,setTimeout:i,interval:r=100,timeout:s=1e3,stopOnFailure:o=!1,verbose:n=!1,backoffFactor:u=1,message:a}={}){this._PromiseModule=t,this._setTimeoutModule=i,this._interval=r,this._timeout=s,this._stopOnFailure=o,this._isWaiting=!1,this._isResolved=!1,this._verbose=n,this._userMessage=a,this.originalStacktraceError=new Error,this._Console=console,this._backoffFactor=u}tryEvery(t){return this._interval=t,this}stopAfter(t){return this._timeout=t,this}execute(t){return this._executeFn=t,this._applyPromiseHandlers(),this._validateExecution(),this.start=Date.now(),this._isWaiting=!0,this._log("starting to execute"),this._runFunction(),this.promise}getPromise(){return this.promise}isResolved(){return this._isResolved}isWaiting(){return this._isWaiting}stopOnFailure(t){return this._stopOnFailure=t,this}_applyPromiseHandlers(){this.promise=new this._PromiseModule((t,e)=>{this.resolve=t,this.reject=e})}_validateExecution(){if("function"!=typeof this._executeFn)throw new Error(i)}_timeFromStart(){return Date.now()-this.start}_shouldStopTrying(){return this._timeFromStart()>this._timeout}_executeAgain(){this._log("executing again"),this._interval*=this._backoffFactor,"function"==typeof this._setTimeoutModule?this._setTimeoutModule(this._runFunction.bind(this),this._interval):setTimeout(this._runFunction.bind(this),this._interval)}_failedToWait(){let t=`${r} after ${this._timeFromStart()}ms`;if(this._userMessage&&(t=`${t}: ${this._userMessage}`),this._lastError){this._lastError.message=`${t}\n${this._lastError.message}`;const e=this.originalStacktraceError.stack;this._lastError.stack+=e.substring(e.indexOf("\n")+1)}else this._lastError=this.originalStacktraceError,this._lastError.message=t;return this._log(this._lastError),this._lastError}_runFunction(){if(this._shouldStopTrying())return this._isWaiting=!1,void this.reject(this._failedToWait());let t=this._executeFn();"object"==typeof t&&"function"==typeof t.then||(t=new this._PromiseModule(e=>e(t))),t.then(t=>{if(!1===t)return this._log(`then execute again with result: ${t}`),void this._executeAgain();this.resolve(t),this._isWaiting=!1,this._isResolved=!0,this._log(`then done waiting with result: ${t}`)}).catch(t=>this._stopOnFailure?(this._log(`stopped on failure with err: ${t}`),this.reject(t)):(this._lastError=t,this._log(`catch with err: ${t}`),this._executeAgain()))}_log(t){this._verbose&&this._Console&&this._Console.log&&this._Console.log(t)}}t.exports={PollUntil:s,waitFor:(t,e)=>new s(e).execute(t)}}).call(this,i(2))},function(t,e){var i;i=function(){return this}();try{i=i||new Function("return this")()}catch(t){"object"==typeof window&&(i=window)}t.exports=i}])})); | ||
//# sourceMappingURL=poll-until-promise.js.map |
{ | ||
"name": "poll-until-promise", | ||
"version": "3.5.0", | ||
"version": "3.6.0", | ||
"description": "Try repeatedly for a promise to be resolved", | ||
@@ -5,0 +5,0 @@ "main": "lib/poll-until-promise.js", |
@@ -71,2 +71,3 @@ [![Build Status][travis-image]][travis-url] | ||
interval: 100, | ||
backoffFactor: 1, // Exponential interval increase. Defaults to 1, which means no backoff | ||
timeout: 1000, | ||
@@ -73,0 +74,0 @@ stopOnFailure: false, // Ignores promise rejections |
@@ -15,2 +15,3 @@ const ERRORS = { | ||
verbose = false, | ||
backoffFactor = 1, | ||
message, | ||
@@ -30,2 +31,3 @@ } = {}) { | ||
this._Console = console; | ||
this._backoffFactor = backoffFactor; | ||
} | ||
@@ -99,2 +101,3 @@ | ||
this._log('executing again'); | ||
this._interval *= this._backoffFactor; | ||
if (typeof this._setTimeoutModule === 'function') { | ||
@@ -101,0 +104,0 @@ this._setTimeoutModule(this._runFunction.bind(this), this._interval); |
@@ -282,18 +282,16 @@ const { PollUntil, waitFor } = require('../src/poll-until-promise'); | ||
...options, | ||
message: 'waiting for something' | ||
} | ||
message: 'waiting for something', | ||
}; | ||
const options2 = { | ||
...options, | ||
message: 'waiting for another thing' | ||
} | ||
message: 'waiting for another thing', | ||
}; | ||
try { | ||
await waitFor(() => { | ||
return waitFor(async () => { | ||
function alon() { | ||
throw new Error('some error message'); | ||
} | ||
await waitFor(() => waitFor(async () => { | ||
function alon() { | ||
throw new Error('some error message'); | ||
} | ||
alon() | ||
}, options2) | ||
}, options1) | ||
alon(); | ||
}, options2), options1); | ||
} catch (e) { | ||
@@ -306,10 +304,10 @@ expect(e.message).toMatch(/Failed to wait after \d+ms: waiting for something\nFailed to wait after \d+ms: waiting for another thing/); | ||
it('wait for should show the user message on failure', async (done) => { | ||
options.message = 'waiting for something' | ||
options.message = 'waiting for something'; | ||
try { | ||
await waitFor(async () => { | ||
throw new Error('some error message'); | ||
}, options) | ||
}, options); | ||
} catch (e) { | ||
expect(e.message).toMatch(/^Failed to wait after \d+ms: waiting for something\nsome error message$/); | ||
done() | ||
done(); | ||
} | ||
@@ -319,14 +317,12 @@ }); | ||
it('wait for should save the original stacktrace', async (done) => { | ||
options.message = 'waiting for something' | ||
options.message = 'waiting for something'; | ||
try { | ||
async function customFunction() { | ||
await waitFor(() => { | ||
return false | ||
}, options) | ||
await waitFor(() => false, options); | ||
} | ||
await customFunction() | ||
await customFunction(); | ||
} catch (e) { | ||
expect(e.message).toMatch(/^Failed to wait after \d+ms: waiting for something$/); | ||
expect(e.stack).toMatch(/customFunction/); | ||
done() | ||
done(); | ||
} | ||
@@ -336,3 +332,3 @@ }); | ||
it('should show stack if thrown inside a function', async (done) => { | ||
let counter = 100 | ||
let counter = 100; | ||
try { | ||
@@ -345,12 +341,12 @@ function functionA() { | ||
} else { | ||
console.log('all good') | ||
console.log('all good'); | ||
} | ||
}, { timeout: 20, interval: 2, verbose: true }) | ||
}, { timeout: 20, interval: 2, verbose: true }); | ||
} | ||
async function functionB() { | ||
await functionA() | ||
await functionA(); | ||
} | ||
await functionB() | ||
await functionB(); | ||
} catch (e) { | ||
@@ -360,5 +356,25 @@ expect(e.message).toMatch(/try again/); | ||
expect(e.stack).toMatch(/functionB/); | ||
done() | ||
done(); | ||
} | ||
}); | ||
it('should backoff if factor defined', async (done) => { | ||
const baseInterval = 100; | ||
const backoffFactor = 2; | ||
shouldHaltPromiseResolve = true; | ||
tryingAttemptsRemaining = 1; | ||
const pollUntil = new PollUntil({ backoffFactor }); | ||
const mockPromise = jest.fn(() => someRandPromise(0)); | ||
pollUntil | ||
.tryEvery(baseInterval) | ||
.execute(mockPromise) | ||
.then(() => { | ||
expect(pollUntil._interval).toEqual(baseInterval * backoffFactor); | ||
done(); | ||
}); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
38762
2.51%509
3.04%130
0.78%