@jcoreio/poll
Advanced tools
Comparing version 2.3.2 to 2.4.0
124
index.js
"use strict"; | ||
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); | ||
Object.defineProperty(exports, "__esModule", { | ||
@@ -9,15 +8,10 @@ value: true | ||
exports["default"] = void 0; | ||
var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator")); | ||
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator")); | ||
function poll(fn, interval) { | ||
var fail, pass; | ||
var attemptNumber = 0; | ||
var until = function until(error, result) { | ||
return !error; | ||
}; | ||
var timeout; | ||
@@ -27,10 +21,7 @@ var timeoutId; | ||
var wrapError = true; | ||
if (!Number.isFinite(interval) || interval < 0) { | ||
throw new Error("invalid interval: ".concat(interval)); | ||
} | ||
var promise = new Promise(function (resolve, reject) { | ||
var startTime = Date.now(); | ||
fail = function fail(error) { | ||
@@ -40,3 +31,2 @@ if (timeoutId != null) clearTimeout(timeoutId); | ||
}; | ||
pass = function pass(value) { | ||
@@ -46,7 +36,5 @@ if (timeoutId != null) clearTimeout(timeoutId); | ||
}; | ||
function _attempt() { | ||
return _attempt2.apply(this, arguments); | ||
} | ||
function _attempt2() { | ||
@@ -56,61 +44,50 @@ _attempt2 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee() { | ||
return _regenerator["default"].wrap(function _callee$(_context) { | ||
while (1) { | ||
switch (_context.prev = _context.next) { | ||
case 0: | ||
now = Date.now(); | ||
_context.prev = 1; | ||
_context.next = 4; | ||
return fn({ | ||
attemptNumber: attemptNumber++, | ||
elapsedTime: now - startTime, | ||
fail: fail, | ||
pass: pass | ||
}); | ||
case 4: | ||
result = _context.sent; | ||
_context.next = 10; | ||
while (1) switch (_context.prev = _context.next) { | ||
case 0: | ||
now = Date.now(); | ||
_context.prev = 1; | ||
_context.next = 4; | ||
return fn({ | ||
attemptNumber: attemptNumber++, | ||
elapsedTime: now - startTime, | ||
fail: fail, | ||
pass: pass | ||
}); | ||
case 4: | ||
result = _context.sent; | ||
_context.next = 10; | ||
break; | ||
case 7: | ||
_context.prev = 7; | ||
_context.t0 = _context["catch"](1); | ||
lastError = error = _context.t0; | ||
case 10: | ||
_context.next = 12; | ||
return until(error, result); | ||
case 12: | ||
if (!_context.sent) { | ||
_context.next = 17; | ||
break; | ||
case 7: | ||
_context.prev = 7; | ||
_context.t0 = _context["catch"](1); | ||
lastError = error = _context.t0; | ||
case 10: | ||
_context.next = 12; | ||
return until(error, result); | ||
case 12: | ||
if (!_context.sent) { | ||
_context.next = 17; | ||
break; | ||
} | ||
if (timeoutId != null) clearTimeout(timeoutId); | ||
if (error) reject(error);else resolve(result); | ||
_context.next = 19; | ||
break; | ||
case 17: | ||
nextTime = now + interval; | ||
if (timeout != null && nextTime - startTime > timeout) { | ||
message = 'timed out waiting for polling to succeed'; | ||
if (wrapError) { | ||
if (lastError) message += "; last error: ".concat(lastError.stack); | ||
reject(new Error(message)); | ||
} else { | ||
reject(lastError || new Error(message)); | ||
} | ||
} | ||
if (timeoutId != null) clearTimeout(timeoutId); | ||
if (error) reject(error);else resolve(result); | ||
_context.next = 19; | ||
break; | ||
case 17: | ||
nextTime = now + interval; | ||
if (timeout != null && nextTime - startTime > timeout) { | ||
message = 'timed out waiting for polling to succeed'; | ||
if (wrapError) { | ||
if (lastError) message += "; last error: ".concat(lastError.stack); | ||
reject(new Error(message)); | ||
} else { | ||
delay = Math.max(0, nextTime - Date.now()); | ||
timeoutId = setTimeout(attempt, delay); | ||
reject(lastError || new Error(message)); | ||
} | ||
case 19: | ||
case "end": | ||
return _context.stop(); | ||
} | ||
} else { | ||
delay = Math.max(0, nextTime - Date.now()); | ||
timeoutId = setTimeout(attempt, delay); | ||
} | ||
case 19: | ||
case "end": | ||
return _context.stop(); | ||
} | ||
@@ -121,14 +98,10 @@ }, _callee, null, [[1, 7]]); | ||
} | ||
var attempt = function attempt() { | ||
return _attempt()["catch"](function () {}); | ||
}; | ||
attempt(); | ||
}); | ||
promise.cancel = function () { | ||
return fail(new Error('polling canceled')); | ||
}; | ||
promise.until = function (condition) { | ||
@@ -138,3 +111,2 @@ until = condition; | ||
}; | ||
promise.timeout = function (ms) { | ||
@@ -144,3 +116,2 @@ timeout = ms; | ||
}; | ||
promise.noWrapError = function () { | ||
@@ -150,9 +121,6 @@ wrapError = false; | ||
}; | ||
return promise; | ||
} | ||
poll["default"] = poll; | ||
var _default = poll; | ||
exports["default"] = _default; | ||
var _default = exports["default"] = poll; | ||
module.exports = exports.default; |
{ | ||
"name": "@jcoreio/poll", | ||
"version": "2.3.2", | ||
"version": "2.4.0", | ||
"description": "handy promised-based polling API", | ||
"scripts": { | ||
"prepush": "npm test" | ||
"@jedwards1211/js-toolchain": { | ||
"mochaArgs": [ | ||
"test/**.js", | ||
"--exit" | ||
] | ||
}, | ||
"config": {}, | ||
"repository": { | ||
@@ -27,14 +29,12 @@ "type": "git", | ||
"sideEffects": false, | ||
"exports": { | ||
"./package.json": "./package.json", | ||
".": { | ||
"require": "./index.js", | ||
"import": "./index.mjs" | ||
} | ||
"main": "index.js", | ||
"module": "index.mjs", | ||
"types": "index.d.ts", | ||
"engines": { | ||
"node": ">=16" | ||
}, | ||
"main": "./index.js", | ||
"module": "./index.mjs", | ||
"packageManager": "pnpm@8.3.1", | ||
"dependencies": { | ||
"@babel/runtime": "^7.12.0" | ||
"@babel/runtime": "^7.18.6" | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
16300
8
206
Updated@babel/runtime@^7.18.6