promise-poller
Advanced tools
Comparing version
@@ -7,2 +7,3 @@ module.exports = { | ||
}, | ||
"plugins": ["prettier"], | ||
"extends": "eslint:recommended", | ||
@@ -16,4 +17,5 @@ "parserOptions": { | ||
"semi": [2, "always"], | ||
"no-console": 0 | ||
"no-console": 0, | ||
"prettier/prettier": "error" | ||
} | ||
}; |
@@ -1,2 +0,2 @@ | ||
'use strict'; | ||
"use strict"; | ||
@@ -8,18 +8,11 @@ Object.defineProperty(exports, "__esModule", { | ||
var _bluebird = require('bluebird'); | ||
var _bluebird = _interopRequireDefault(require("bluebird")); | ||
var _bluebird2 = _interopRequireDefault(_bluebird); | ||
var _debug = _interopRequireDefault(require("debug")); | ||
var _debug = require('debug'); | ||
var _strategies = _interopRequireDefault(require("./strategies")); | ||
var _debug2 = _interopRequireDefault(_debug); | ||
var _strategies = require('./strategies'); | ||
var _strategies2 = _interopRequireDefault(_strategies); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
var debug = (0, _debug2.default)('promisePoller'); | ||
var debug = (0, _debug.default)('promisePoller'); | ||
var DEFAULTS = { | ||
@@ -32,3 +25,2 @@ strategy: 'fixed-interval', | ||
}; | ||
var pollerCount = 0; | ||
@@ -46,10 +38,11 @@ | ||
}); | ||
options.name = options.name || 'Poller-' + pollerCount++; | ||
debug('Creating a promise poller "' + options.name + '" with interval=' + options.interval + ', retries=' + options.retries); | ||
options.name = options.name || "Poller-".concat(pollerCount++); | ||
debug("Creating a promise poller \"".concat(options.name, "\" with interval=").concat(options.interval, ", retries=").concat(options.retries)); | ||
if (!_strategies2.default[options.strategy]) { | ||
throw new Error('Invalid strategy "' + options.strategy + '". Valid strategies are ' + Object.keys(_strategies2.default)); | ||
if (!_strategies.default[options.strategy]) { | ||
throw new Error("Invalid strategy \"".concat(options.strategy, "\". Valid strategies are ").concat(Object.keys(_strategies.default))); | ||
} | ||
var strategy = _strategies2.default[options.strategy]; | ||
debug('(' + options.name + ') Using strategy "' + options.strategy + '".'); | ||
var strategy = _strategies.default[options.strategy]; | ||
debug("(".concat(options.name, ") Using strategy \"").concat(options.strategy, "\".")); | ||
var strategyDefaults = strategy.defaults; | ||
@@ -59,9 +52,7 @@ Object.keys(strategyDefaults).forEach(function (option) { | ||
}); | ||
debug('(' + options.name + ') Options:'); | ||
debug("(".concat(options.name, ") Options:")); | ||
Object.keys(options).forEach(function (option) { | ||
debug(' "' + option + '": ' + options[option]); | ||
debug(" \"".concat(option, "\": ").concat(options[option])); | ||
}); | ||
return new _bluebird2.default(function (resolve, reject) { | ||
return new _bluebird.default(function (resolve, reject) { | ||
var polling = true; | ||
@@ -73,5 +64,5 @@ var retriesRemaining = options.retries; | ||
if (options.masterTimeout) { | ||
debug('(' + options.name + ') Using master timeout of ' + options.masterTimeout + ' ms.'); | ||
debug("(".concat(options.name, ") Using master timeout of ").concat(options.masterTimeout, " ms.")); | ||
timeoutId = setTimeout(function () { | ||
debug('(' + options.name + ') Master timeout reached. Rejecting master promise.'); | ||
debug("(".concat(options.name, ") Master timeout reached. Rejecting master promise.")); | ||
polling = false; | ||
@@ -86,4 +77,4 @@ reject('master timeout'); | ||
if (task === false) { | ||
task = _bluebird2.default.reject('Cancelled'); | ||
debug('(' + options.name + ') Task function returned false, canceling.'); | ||
task = _bluebird.default.reject('Cancelled'); | ||
debug("(".concat(options.name, ") Task function returned false, canceling.")); | ||
reject(rejections); | ||
@@ -93,3 +84,3 @@ polling = false; | ||
var taskPromise = _bluebird2.default.resolve(task); | ||
var taskPromise = _bluebird.default.resolve(task); | ||
@@ -101,9 +92,10 @@ if (options.timeout) { | ||
taskPromise.then(function (result) { | ||
debug('(' + options.name + ') Poll succeeded. Resolving master promise.'); | ||
debug("(".concat(options.name, ") Poll succeeded. Resolving master promise.")); | ||
if (options.shouldContinue(null, result)) { | ||
debug('(' + options.name + ') shouldContinue returned true. Retrying.'); | ||
debug("(".concat(options.name, ") shouldContinue returned true. Retrying.")); | ||
var nextInterval = strategy.getNextInterval(options.retries - retriesRemaining, options); | ||
debug('(' + options.name + ') Waiting ' + nextInterval + 'ms to try again.'); | ||
_bluebird2.default.delay(nextInterval).then(poll); | ||
debug("(".concat(options.name, ") Waiting ").concat(nextInterval, "ms to try again.")); | ||
_bluebird.default.delay(nextInterval).then(poll); | ||
} else { | ||
@@ -113,2 +105,3 @@ if (timeoutId !== null) { | ||
} | ||
resolve(result); | ||
@@ -118,2 +111,3 @@ } | ||
rejections.push(err); | ||
if (typeof options.progressCallback === 'function') { | ||
@@ -124,11 +118,10 @@ options.progressCallback(retriesRemaining, err); | ||
if (! --retriesRemaining || !options.shouldContinue(err)) { | ||
debug('(' + options.name + ') Maximum retries reached. Rejecting master promise.'); | ||
debug("(".concat(options.name, ") Maximum retries reached. Rejecting master promise.")); | ||
reject(rejections); | ||
} else if (polling) { | ||
debug('(' + options.name + ') Poll failed. ' + retriesRemaining + ' retries remaining.'); | ||
debug("(".concat(options.name, ") Poll failed. ").concat(retriesRemaining, " retries remaining.")); | ||
var nextInterval = strategy.getNextInterval(options.retries - retriesRemaining, options); | ||
debug("(".concat(options.name, ") Waiting ").concat(nextInterval, "ms to try again.")); | ||
debug('(' + options.name + ') Waiting ' + nextInterval + 'ms to try again.'); | ||
_bluebird2.default.delay(nextInterval).then(poll); | ||
_bluebird.default.delay(nextInterval).then(poll); | ||
} | ||
@@ -135,0 +128,0 @@ }); |
@@ -1,2 +0,2 @@ | ||
'use strict'; | ||
"use strict"; | ||
@@ -12,3 +12,2 @@ var strategies = { | ||
}, | ||
'linear-backoff': { | ||
@@ -23,3 +22,2 @@ defaults: { | ||
}, | ||
'exponential-backoff': { | ||
@@ -35,3 +33,2 @@ defaults: { | ||
}; | ||
module.exports = strategies; |
@@ -1,20 +0,16 @@ | ||
'use strict'; | ||
"use strict"; | ||
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; | ||
var _promisePoller = _interopRequireDefault(require("../lib/promise-poller")); | ||
var _promisePoller = require('../lib/promise-poller'); | ||
var _bluebird = _interopRequireDefault(require("bluebird")); | ||
var _promisePoller2 = _interopRequireDefault(_promisePoller); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
var _bluebird = require('bluebird'); | ||
function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } | ||
var _bluebird2 = _interopRequireDefault(_bluebird); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
describe('Promise Poller', function () { | ||
it('returns a promise', function () { | ||
var poller = (0, _promisePoller2.default)({ | ||
var poller = (0, _promisePoller.default)({ | ||
taskFn: function taskFn() { | ||
return _bluebird2.default.resolve('yay'); | ||
return _bluebird.default.resolve('yay'); | ||
} | ||
@@ -25,7 +21,6 @@ }); | ||
}); | ||
it('resolves the master promise when the poll succeeds', function (done) { | ||
(0, _promisePoller2.default)({ | ||
(0, _promisePoller.default)({ | ||
taskFn: function taskFn() { | ||
return _bluebird2.default.resolve('yay'); | ||
return _bluebird.default.resolve('yay'); | ||
}, | ||
@@ -41,7 +36,6 @@ interval: 500, | ||
}); | ||
it('rejects the master promise when the poll fails', function (done) { | ||
(0, _promisePoller2.default)({ | ||
(0, _promisePoller.default)({ | ||
taskFn: function taskFn() { | ||
return _bluebird2.default.reject('derp'); | ||
return _bluebird.default.reject('derp'); | ||
}, | ||
@@ -56,8 +50,7 @@ interval: 500, | ||
}); | ||
it('rejects the master promise with an array of rejections', function (done) { | ||
var counter = 0; | ||
(0, _promisePoller2.default)({ | ||
(0, _promisePoller.default)({ | ||
taskFn: function taskFn() { | ||
return _bluebird2.default.reject(++counter); | ||
return _bluebird.default.reject(++counter); | ||
}, | ||
@@ -73,6 +66,5 @@ interval: 500, | ||
}); | ||
it('fails the poll if the timeout is exceeded', function (done) { | ||
var taskFn = function taskFn() { | ||
return new _bluebird2.default(function (resolve) { | ||
return new _bluebird.default(function (resolve) { | ||
setTimeout(function () { | ||
@@ -84,3 +76,3 @@ return resolve('derp'); | ||
(0, _promisePoller2.default)({ | ||
(0, _promisePoller.default)({ | ||
taskFn: taskFn, | ||
@@ -98,3 +90,2 @@ timeout: 1000, | ||
}); | ||
it('rejects the master promise if the master timeout is exceeded', function (done) { | ||
@@ -104,3 +95,3 @@ var numPolls = 0; | ||
var taskFn = function taskFn() { | ||
return new _bluebird2.default(function (resolve, reject) { | ||
return new _bluebird.default(function (resolve, reject) { | ||
numPolls += 1; | ||
@@ -113,3 +104,3 @@ setTimeout(function () { | ||
(0, _promisePoller2.default)({ | ||
(0, _promisePoller.default)({ | ||
taskFn: taskFn, | ||
@@ -126,16 +117,18 @@ masterTimeout: 500, | ||
}); | ||
it('waits the given interval between attempts', function (done) { | ||
var last = 0; | ||
var now = void 0; | ||
var now; | ||
var taskFn = function taskFn() { | ||
now = Date.now(); | ||
if (last) { | ||
expect(now - last).not.toBeLessThan(500); | ||
} | ||
last = now; | ||
return _bluebird2.default.reject('derp'); | ||
return _bluebird.default.reject('derp'); | ||
}; | ||
(0, _promisePoller2.default)({ | ||
(0, _promisePoller.default)({ | ||
taskFn: taskFn, | ||
@@ -146,11 +139,11 @@ interval: 500, | ||
}); | ||
it('uses the default retries of 5 if not specified', function (done) { | ||
var counter = 0; | ||
var taskFn = function taskFn() { | ||
counter++; | ||
return _bluebird2.default.reject('derp'); | ||
return _bluebird.default.reject('derp'); | ||
}; | ||
(0, _promisePoller2.default)({ | ||
(0, _promisePoller.default)({ | ||
taskFn: taskFn, | ||
@@ -163,11 +156,11 @@ interval: 500 | ||
}); | ||
it('tries <retries> times before giving up', function (done) { | ||
var counter = 0; | ||
var taskFn = function taskFn() { | ||
counter++; | ||
return _bluebird2.default.reject('derp'); | ||
return _bluebird.default.reject('derp'); | ||
}; | ||
(0, _promisePoller2.default)({ | ||
(0, _promisePoller.default)({ | ||
taskFn: taskFn, | ||
@@ -181,16 +174,18 @@ interval: 500, | ||
}); | ||
it('uses the default interval of 500 if not specified', function (done) { | ||
var last = 0; | ||
var now = void 0; | ||
var now; | ||
var taskFn = function taskFn() { | ||
now = Date.now(); | ||
if (last) { | ||
expect(now - last).not.toBeLessThan(500); | ||
} | ||
last = now; | ||
return _bluebird2.default.reject('derp'); | ||
return _bluebird.default.reject('derp'); | ||
}; | ||
(0, _promisePoller2.default)({ | ||
(0, _promisePoller.default)({ | ||
taskFn: taskFn, | ||
@@ -200,12 +195,12 @@ retries: 3 | ||
}); | ||
it('throws an exception if no taskFn was specified', function () { | ||
var fn = function fn() { | ||
return (0, _promisePoller2.default)(); | ||
return (0, _promisePoller.default)(); | ||
}; | ||
expect(fn).toThrowError(/No taskFn/); | ||
}); | ||
it('calls the progress callback with each failure', function (done) { | ||
var count = 0; | ||
var callback = function callback(retriesRemaining, error) { | ||
@@ -216,5 +211,6 @@ expect(error).toEqual('derp'); | ||
}; | ||
(0, _promisePoller2.default)({ | ||
(0, _promisePoller.default)({ | ||
taskFn: function taskFn() { | ||
return _bluebird2.default.reject('derp'); | ||
return _bluebird.default.reject('derp'); | ||
}, | ||
@@ -229,5 +225,4 @@ interval: 500, | ||
}); | ||
it('wraps a non-promise task function return in Promise.resolve', function (done) { | ||
(0, _promisePoller2.default)({ | ||
(0, _promisePoller.default)({ | ||
taskFn: function taskFn() { | ||
@@ -243,5 +238,4 @@ return 'foobar'; | ||
}); | ||
it('fails the poll if an exception is thrown in the task function', function (done) { | ||
(0, _promisePoller2.default)({ | ||
(0, _promisePoller.default)({ | ||
taskFn: function taskFn() { | ||
@@ -257,5 +251,5 @@ throw new Error('oops'); | ||
}); | ||
it('rejects the master promise if false is returned from the task function', function (done) { | ||
var counter = 0; | ||
var taskFn = function taskFn() { | ||
@@ -265,7 +259,7 @@ if (++counter === 1) { | ||
} else { | ||
return _bluebird2.default.reject('derp'); | ||
return _bluebird.default.reject('derp'); | ||
} | ||
}; | ||
(0, _promisePoller2.default)({ | ||
(0, _promisePoller.default)({ | ||
taskFn: taskFn, | ||
@@ -280,3 +274,2 @@ interval: 500, | ||
}); | ||
it('clears the master timeout if the master promise resolves', function (done) { | ||
@@ -288,5 +281,5 @@ var globalObj = jasmine.getGlobal(); | ||
spyOn(globalObj, 'clearTimeout'); | ||
(0, _promisePoller2.default)({ | ||
(0, _promisePoller.default)({ | ||
taskFn: function taskFn() { | ||
return _bluebird2.default.resolve('foobar'); | ||
return _bluebird.default.resolve('foobar'); | ||
}, | ||
@@ -301,10 +294,10 @@ masterTimeout: 10000 | ||
}); | ||
it('bails out when shouldContinue returns false', function (done) { | ||
var counter = 0; | ||
var taskFn = function taskFn() { | ||
return _bluebird2.default.reject(++counter); | ||
return _bluebird.default.reject(++counter); | ||
}; | ||
(0, _promisePoller2.default)({ | ||
(0, _promisePoller.default)({ | ||
taskFn: taskFn, | ||
@@ -321,10 +314,10 @@ shouldContinue: function shouldContinue(err) { | ||
}); | ||
it('continues to poll on success if shouldContinue returns true', function (done) { | ||
var counter = 0; | ||
var taskFn = function taskFn() { | ||
return _bluebird2.default.resolve(++counter); | ||
return _bluebird.default.resolve(++counter); | ||
}; | ||
(0, _promisePoller2.default)({ | ||
(0, _promisePoller.default)({ | ||
taskFn: taskFn, | ||
@@ -331,0 +324,0 @@ shouldContinue: function shouldContinue(err, result) { |
@@ -1,7 +0,5 @@ | ||
'use strict'; | ||
"use strict"; | ||
var _strategies = require('../lib/strategies'); | ||
var _strategies = _interopRequireDefault(require("../lib/strategies")); | ||
var _strategies2 = _interopRequireDefault(_strategies); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
@@ -15,10 +13,8 @@ | ||
}; | ||
var expectedIntervals = [1000, 1000, 1000, 1000, 1000]; | ||
expectedIntervals.forEach(function (interval, index) { | ||
expect(_strategies2.default['fixed-interval'].getNextInterval(index, options)).toEqual(interval); | ||
expect(_strategies.default['fixed-interval'].getNextInterval(index, options)).toEqual(interval); | ||
}); | ||
}); | ||
}); | ||
describe('linear backoff strategy', function () { | ||
@@ -30,10 +26,8 @@ it('increases the interval linearly', function () { | ||
}; | ||
var expectedIntervals = [1000, 1500, 2000, 2500, 3000]; | ||
expectedIntervals.forEach(function (interval, index) { | ||
expect(_strategies2.default['linear-backoff'].getNextInterval(index, options)).toEqual(interval); | ||
expect(_strategies.default['linear-backoff'].getNextInterval(index, options)).toEqual(interval); | ||
}); | ||
}); | ||
}); | ||
describe('exponential backoff strategy', function () { | ||
@@ -43,2 +37,3 @@ it('uses exponential backoff with jitter', function () { | ||
var expectedIntervals = [1000, 1400, 2800, 6600, 10000]; | ||
Math.random = function () { | ||
@@ -52,5 +47,4 @@ return randoms.shift(); | ||
}; | ||
expectedIntervals.forEach(function (interval, index) { | ||
expect(_strategies2.default['exponential-backoff'].getNextInterval(index, options)).toEqual(interval); | ||
expect(_strategies.default['exponential-backoff'].getNextInterval(index, options)).toEqual(interval); | ||
}); | ||
@@ -57,0 +51,0 @@ }); |
@@ -5,8 +5,3 @@ { | ||
"**/*[sS]pec.js" | ||
], | ||
"helpers": [ | ||
"helpers/**/*.js" | ||
], | ||
"stopSpecOnExpectationFailure": false, | ||
"random": false | ||
] | ||
} |
{ | ||
"name": "promise-poller", | ||
"version": "1.6.0", | ||
"version": "1.7.0", | ||
"description": "A basic poller built on top of promises", | ||
"main": "dist/lib/promise-poller.js", | ||
"scripts": { | ||
"prepublish": "npm run build", | ||
"build": "npm run lint && npm test", | ||
"compile": "babel -d dist src", | ||
"prepublishOnly": "npm run build", | ||
"build": "babel -d dist src", | ||
"clean": "rm -rf dist", | ||
"lint": "eslint src", | ||
"test": "npm run compile && jasmine JASMINE_CONFIG_PATH=jasmine.json" | ||
"test": "npm run build && jasmine JASMINE_CONFIG_PATH=jasmine.json", | ||
"prettify": "prettier --write src/**/*js" | ||
}, | ||
@@ -34,13 +34,28 @@ "repository": { | ||
"devDependencies": { | ||
"babel-cli": "^6.11.4", | ||
"babel-eslint": "^7.0.0", | ||
"babel-preset-es2015": "^6.5.0", | ||
"eslint": "^3.7.1", | ||
"jasmine": "^2.4.1", | ||
"jasmine-core": "^2.4.1" | ||
"@babel/cli": "^7.1.5", | ||
"@babel/core": "^7.1.6", | ||
"@babel/preset-env": "^7.1.6", | ||
"@babel/register": "^7.0.0", | ||
"eslint": "^5.9.0", | ||
"eslint-plugin-prettier": "^3.0.0", | ||
"husky": "^1.1.4", | ||
"jasmine": "^3.3.0", | ||
"lint-staged": "^8.0.5", | ||
"prettier": "^1.15.2" | ||
}, | ||
"dependencies": { | ||
"bluebird": "^3.3.1", | ||
"debug": "^3.1.0" | ||
"bluebird": "^3.5.3", | ||
"debug": "^4.1.0" | ||
}, | ||
"husky": { | ||
"hooks": { | ||
"pre-commit": "lint-staged" | ||
} | ||
}, | ||
"lint-staged": { | ||
"src/**/*.js": [ | ||
"prettier --write", | ||
"git add" | ||
] | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
27408
0.55%10
66.67%475
-1.66%+ Added
- Removed
Updated
Updated