poll-until-promise
Advanced tools
Comparing version
@@ -1,2 +0,153 @@ | ||
!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){var e;return this._applyPromiseHandlers(),function(t){if("function"!=typeof t)throw new Error(i)}(t),this._executeFn=(e=t,async()=>await e()),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})}_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());this._executeFn().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 | ||
"use strict"; | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
}); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.waitFor = exports.PollUntil = void 0; | ||
const ERRORS = { | ||
NOT_FUNCTION: 'Your executor is not a function. functions and promises are valid.', | ||
FAILED_TO_WAIT: 'Failed to wait', | ||
}; | ||
function promisify(fn) { | ||
return () => __awaiter(this, void 0, void 0, function* () { | ||
const result = yield fn(); | ||
return result; | ||
}); | ||
} | ||
function validateExecution(executeFn) { | ||
if (typeof executeFn !== 'function') { | ||
throw new Error(ERRORS.NOT_FUNCTION); | ||
} | ||
} | ||
class PollUntil { | ||
constructor({ PromiseModule = global.Promise, setTimeoutFunction, interval = 100, timeout = 1000, stopOnFailure = false, verbose = false, backoffFactor = 1, message = '', } = {}) { | ||
this._PromiseModule = PromiseModule; | ||
this._setTimeoutFunction = setTimeoutFunction; | ||
this._interval = interval; | ||
this._timeout = timeout; | ||
this._stopOnFailure = stopOnFailure; | ||
this._isWaiting = false; | ||
this._isResolved = false; | ||
this._verbose = verbose; | ||
this._userMessage = message; | ||
this.originalStacktraceError = new Error(); | ||
this._Console = console; | ||
this._backoffFactor = backoffFactor; | ||
this.start = +Date.now(); | ||
} | ||
tryEvery(interval) { | ||
this._interval = interval; | ||
return this; | ||
} | ||
stopAfter(timeout) { | ||
this._timeout = timeout; | ||
return this; | ||
} | ||
execute(executeFn) { | ||
this._applyPromiseHandlers(); | ||
validateExecution(executeFn); | ||
this._executeFn = promisify(executeFn); | ||
this.start = Date.now(); | ||
this._isWaiting = true; | ||
this._log('starting to execute'); | ||
this._runFunction(); | ||
return this.promise; | ||
} | ||
getPromise() { | ||
return this.promise; | ||
} | ||
isResolved() { | ||
return this._isResolved; | ||
} | ||
isWaiting() { | ||
return this._isWaiting; | ||
} | ||
stopOnFailure(stop) { | ||
this._stopOnFailure = stop; | ||
return this; | ||
} | ||
_applyPromiseHandlers() { | ||
this.promise = new this._PromiseModule((resolve, reject) => { | ||
this.resolve = resolve; | ||
this.reject = reject; | ||
}); | ||
} | ||
_timeFromStart() { | ||
return Date.now() - this.start; | ||
} | ||
_shouldStopTrying() { | ||
return this._timeFromStart() > this._timeout; | ||
} | ||
_executeAgain() { | ||
this._log('executing again'); | ||
this._interval *= this._backoffFactor; | ||
if (typeof this._setTimeoutFunction === 'function') { | ||
this._setTimeoutFunction(this._runFunction.bind(this), this._interval); | ||
} | ||
else { | ||
setTimeout(this._runFunction.bind(this), this._interval); | ||
} | ||
} | ||
_failedToWait() { | ||
let waitErrorText = `${ERRORS.FAILED_TO_WAIT} after ${this._timeFromStart()}ms`; | ||
if (this._userMessage) | ||
waitErrorText = `${waitErrorText}: ${this._userMessage}`; | ||
if (this._lastError) { | ||
this._lastError.message = `${waitErrorText}\n${this._lastError.message}`; | ||
const originalStack = this.originalStacktraceError.stack; | ||
if (originalStack) { | ||
this._lastError.stack += originalStack.substring(originalStack.indexOf('\n') + 1); | ||
} | ||
} | ||
else { | ||
this._lastError = this.originalStacktraceError; | ||
this._lastError.message = waitErrorText; | ||
} | ||
this._log(this._lastError); | ||
return this._lastError; | ||
} | ||
_runFunction() { | ||
var _a; | ||
if (this._shouldStopTrying()) { | ||
this._isWaiting = false; | ||
(_a = this.reject) === null || _a === void 0 ? void 0 : _a.call(this, this._failedToWait()); | ||
return; | ||
} | ||
this._executeFn() | ||
.then((result) => { | ||
var _a; | ||
if (result === false) { | ||
this._log(`then execute again with result: ${result}`); | ||
this._executeAgain(); | ||
return; | ||
} | ||
(_a = this.resolve) === null || _a === void 0 ? void 0 : _a.call(this, result); | ||
this._isWaiting = false; | ||
this._isResolved = true; | ||
this._log(`then done waiting with result: ${result}`); | ||
}) | ||
.catch((err) => { | ||
var _a; | ||
if (this._stopOnFailure) { | ||
this._log(`stopped on failure with err: ${err}`); | ||
return (_a = this.reject) === null || _a === void 0 ? void 0 : _a.call(this, err); | ||
} | ||
this._lastError = err; | ||
this._log(`catch with err: ${err}`); | ||
return this._executeAgain(); | ||
}); | ||
} | ||
_log(message) { | ||
if (this._verbose && this._Console && this._Console.log) | ||
this._Console.log(message); | ||
} | ||
} | ||
exports.PollUntil = PollUntil; | ||
const waitFor = (waitForFunction, options) => new PollUntil(options).execute(waitForFunction); | ||
exports.waitFor = waitFor; |
{ | ||
"name": "poll-until-promise", | ||
"version": "3.6.1", | ||
"version": "4.0.1", | ||
"description": "Try repeatedly for a promise to be resolved", | ||
"main": "lib/poll-until-promise.js", | ||
"main": "lib/index.js", | ||
"types": "lib/index.d.ts", | ||
"directories": { | ||
@@ -18,9 +19,12 @@ "test": "test" | ||
"scripts": { | ||
"build": "cross-env WEBPACK_ENV=build webpack", | ||
"lint": "cross-env eslint src/", | ||
"lint:fix": "cross-env eslint src/ --fix", | ||
"dev": "cross-env WEBPACK_ENV=dev webpack --progress --colors --watch", | ||
"test": "cross-env NODE_ENV=test jest", | ||
"test:coverage": "cross-env jest --coverage && cat ./coverage/lcov.info | coveralls", | ||
"release": "npm run lint && npm run test:coverage && npm run build" | ||
"build": "tsc", | ||
"lint": "eslint .", | ||
"test": "jest", | ||
"test:coverage": "jest --coverage && cat ./coverage/lcov.info | coveralls", | ||
"prepare": "npm run build", | ||
"prepublishOnly": "npm test && npm run lint", | ||
"release": "npm run lint && npm run test:coverage && npm run build", | ||
"preversion": "npm run lint", | ||
"version": "npm run lint && git add -A src", | ||
"postversion": "git push && git push --tags" | ||
}, | ||
@@ -46,17 +50,22 @@ "contributors": [ | ||
"license": "ISC", | ||
"dependencies": {}, | ||
"devDependencies": { | ||
"@babel/core": "7.8.7", | ||
"@babel/preset-env": "7.8.7", | ||
"coveralls": "^3.0.9", | ||
"cross-env": "7.0.2", | ||
"babel-loader": "8.0.6", | ||
"eslint": "6.8.0", | ||
"eslint-config-airbnb-base": "14.1.0", | ||
"eslint-loader": "3.0.3", | ||
"eslint-plugin-import": "2.20.1", | ||
"jest": "25.1.0", | ||
"nyc": "15.0.0", | ||
"webpack": "4.42.0", | ||
"webpack-cli": "^3.3.11" | ||
"@babel/core": "^7.16.5", | ||
"@babel/preset-env": "^7.16.5", | ||
"@types/jest": "^27.0.3", | ||
"@typescript-eslint/eslint-plugin": "^5.8.0", | ||
"@typescript-eslint/parser": "^5.8.0", | ||
"coveralls": "^3.1.1", | ||
"cross-env": "^7.0.3", | ||
"eslint": "^8.5.0", | ||
"eslint-config-airbnb-base": "^15.0.0", | ||
"eslint-config-airbnb-typescript": "^16.1.0", | ||
"eslint-plugin-import": "^2.25.3", | ||
"eslint-webpack-plugin": "^3.1.1", | ||
"jest": "^27.4.5", | ||
"nyc": "^15.1.0", | ||
"ts-jest": "^27.1.2", | ||
"typescript": "^4.5.4", | ||
"webpack": "^5.65.0", | ||
"webpack-cli": "^4.9.1", | ||
"ts-loader": "^9.2.6" | ||
}, | ||
@@ -63,0 +72,0 @@ "nyc": { |
const path = require('path'); | ||
const libraryWindowName = 'PollUntil'; | ||
const libraryName = 'poll-until-promise'; | ||
const plugins = []; | ||
const outputFile = `${libraryName}.js`; | ||
const outputFile = `index.js`; | ||
const config = { | ||
mode: 'production', | ||
entry: path.resolve('./index.js'), | ||
entry: path.resolve('./src/index.ts'), | ||
devtool: 'source-map', | ||
@@ -22,10 +20,13 @@ output: { | ||
module: { | ||
rules: [{ | ||
test: /\.js$/, | ||
exclude: /(node_modules)/, | ||
use: { | ||
loader: 'babel-loader', | ||
rules: [ | ||
{ | ||
test: /\.tsx?$/, | ||
use: 'ts-loader', | ||
exclude: /node_modules/, | ||
}, | ||
}], | ||
], | ||
}, | ||
resolve: { | ||
extensions: ['.tsx', '.ts', '.js'], | ||
}, | ||
plugins, | ||
@@ -32,0 +33,0 @@ }; |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
55173
42.29%18
50%874
67.43%19
46.15%1
Infinity%