bottleneck
Advanced tools
Comparing version 2.14.1 to 2.15.0
@@ -332,2 +332,19 @@ declare module "bottleneck" { | ||
} | ||
class Events { | ||
constructor(object: Object); | ||
/** | ||
* Returns the number of limiters for the event name | ||
* @param name - The event name. | ||
*/ | ||
listenerCount(name: string): number; | ||
/** | ||
* Returns a promise with the first non-null/non-undefined result from a listener | ||
* @param name - The event name. | ||
* @param args - The arguments to pass to the event listeners. | ||
*/ | ||
trigger(name: string, ...args: any[]): Promise<any>; | ||
} | ||
} | ||
@@ -334,0 +351,0 @@ |
{ | ||
"name": "bottleneck", | ||
"main": "bottleneck.js", | ||
"version": "2.14.1", | ||
"version": "2.15.0", | ||
"homepage": "https://github.com/SGrondin/bottleneck", | ||
@@ -6,0 +6,0 @@ "authors": [ |
@@ -169,3 +169,3 @@ "use strict"; | ||
_run(next, wait, index) { | ||
_run(next, wait, index, retryCount) { | ||
var _this = this; | ||
@@ -184,3 +184,3 @@ | ||
var _ref = _asyncToGenerator(function* (...args) { | ||
var e, running; | ||
var e, error, eventInfo, retry, retryAfter, running; | ||
@@ -190,19 +190,29 @@ if (!done) { | ||
done = true; | ||
clearTimeout(_this._scheduled[index].expiration); | ||
delete _this._scheduled[index]; | ||
eventInfo = { | ||
args: next.args, | ||
options: next.options, | ||
retryCount | ||
}; | ||
if ((error = args[0]) != null) { | ||
retry = yield _this.Events.trigger("failed", error, eventInfo); | ||
if (retry != null) { | ||
retryAfter = ~~retry; | ||
_this.Events.trigger("retry", `Retrying ${next.options.id} after ${retryAfter} ms`, eventInfo); | ||
return _this._run(next, retryAfter, index, retryCount + 1); | ||
} | ||
} | ||
_this._states.next(next.options.id); // DONE | ||
clearTimeout(_this._scheduled[index].expiration); | ||
delete _this._scheduled[index]; | ||
_this.Events.trigger("debug", `Completed ${next.options.id}`, eventInfo); | ||
_this.Events.trigger("debug", `Completed ${next.options.id}`, { | ||
args: next.args, | ||
options: next.options | ||
}); | ||
_this.Events.trigger("done", `Completed ${next.options.id}`, eventInfo); | ||
_this.Events.trigger("done", `Completed ${next.options.id}`, { | ||
args: next.args, | ||
options: next.options | ||
}); | ||
var _ref2 = yield _this._store.__free__(index, next.options.weight); | ||
@@ -212,6 +222,3 @@ | ||
_this.Events.trigger("debug", `Freed ${next.options.id}`, { | ||
args: next.args, | ||
options: next.options | ||
}); | ||
_this.Events.trigger("debug", `Freed ${next.options.id}`, eventInfo); | ||
@@ -223,4 +230,4 @@ if (running === 0 && _this.empty()) { | ||
return typeof next.cb === "function" ? next.cb(...args) : void 0; | ||
} catch (error) { | ||
e = error; | ||
} catch (error1) { | ||
e = error1; | ||
return _this.Events.trigger("error", e); | ||
@@ -236,5 +243,7 @@ } | ||
this._states.next(next.options.id); // RUNNING | ||
if (retryCount === 0) { | ||
// RUNNING | ||
this._states.next(next.options.id); | ||
} | ||
return this._scheduled[index] = { | ||
@@ -247,5 +256,7 @@ timeout: setTimeout(() => { | ||
this._states.next(next.options.id); // EXECUTING | ||
if (retryCount === 0) { | ||
// EXECUTING | ||
this._states.next(next.options.id); | ||
} | ||
if (this._limiter != null) { | ||
@@ -312,3 +323,3 @@ return this._limiter.submit(next.options, next.task, ...next.args, completed); | ||
this._run(next, wait, index); | ||
this._run(next, wait, index, 0); | ||
} | ||
@@ -486,4 +497,4 @@ | ||
}); | ||
} catch (error) { | ||
e = error; | ||
} catch (error1) { | ||
e = error1; | ||
@@ -490,0 +501,0 @@ _this2._states.remove(options.id); |
"use strict"; | ||
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } } | ||
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; } | ||
var Events; | ||
@@ -45,30 +49,68 @@ Events = class Events { | ||
trigger(name, ...args) { | ||
if (name !== "debug") { | ||
this.trigger("debug", `Event triggered: ${name}`, args); | ||
listenerCount(name) { | ||
if (this._events[name] != null) { | ||
return this._events[name].length; | ||
} else { | ||
return 0; | ||
} | ||
} | ||
if (this._events[name] == null) { | ||
return; | ||
} | ||
trigger(name, ...args) { | ||
var _this = this; | ||
this._events[name] = this._events[name].filter(function (listener) { | ||
return listener.status !== "none"; | ||
}); | ||
return this._events[name].forEach(listener => { | ||
var e, ret; | ||
return _asyncToGenerator(function* () { | ||
var e, promises; | ||
if (listener.status === "none") { | ||
return; | ||
} | ||
try { | ||
if (name !== "debug") { | ||
_this.trigger("debug", `Event triggered: ${name}`, args); | ||
} | ||
if (listener.status === "once") { | ||
listener.status = "none"; | ||
} | ||
if (_this._events[name] == null) { | ||
return; | ||
} | ||
try { | ||
ret = typeof listener.cb === "function" ? listener.cb(...args) : void 0; | ||
return ret != null ? typeof ret.then === "function" ? ret.then(function () {}).catch(e => { | ||
return this.trigger("error", e); | ||
}) : void 0 : void 0; | ||
_this._events[name] = _this._events[name].filter(function (listener) { | ||
return listener.status !== "none"; | ||
}); | ||
promises = _this._events[name].map( | ||
/*#__PURE__*/ | ||
function () { | ||
var _ref = _asyncToGenerator(function* (listener) { | ||
var e, returned; | ||
if (listener.status === "none") { | ||
return; | ||
} | ||
if (listener.status === "once") { | ||
listener.status = "none"; | ||
} | ||
try { | ||
returned = typeof listener.cb === "function" ? listener.cb(...args) : void 0; | ||
if ((returned != null ? returned.then : void 0) != null && typeof returned.then === "function") { | ||
return yield returned; | ||
} else { | ||
return returned; | ||
} | ||
} catch (error) { | ||
e = error; | ||
if ("name" !== "error") { | ||
_this.trigger("error", e); | ||
} | ||
return null; | ||
} | ||
}); | ||
return function (_x) { | ||
return _ref.apply(this, arguments); | ||
}; | ||
}()); | ||
return (yield Promise.all(promises)).find(function (x) { | ||
return x != null; | ||
}); | ||
} catch (error) { | ||
@@ -78,6 +120,8 @@ e = error; | ||
if ("name" !== "error") { | ||
return this.trigger("error", e); | ||
_this.trigger("error", e); | ||
} | ||
return null; | ||
} | ||
}); | ||
})(); | ||
} | ||
@@ -84,0 +128,0 @@ |
@@ -1,1 +0,1 @@ | ||
{"version":"2.14.1"} | ||
{"version":"2.15.0"} |
125
light.js
@@ -148,32 +148,55 @@ /** | ||
trigger(name, ...args) { | ||
if (name !== "debug") { | ||
this.trigger("debug", `Event triggered: ${name}`, args); | ||
listenerCount(name) { | ||
if (this._events[name] != null) { | ||
return this._events[name].length; | ||
} else { | ||
return 0; | ||
} | ||
if (this._events[name] == null) { | ||
return; | ||
} | ||
this._events[name] = this._events[name].filter(function(listener) { | ||
return listener.status !== "none"; | ||
}); | ||
return this._events[name].forEach((listener) => { | ||
var e, ret; | ||
if (listener.status === "none") { | ||
} | ||
async trigger(name, ...args) { | ||
var e, promises; | ||
try { | ||
if (name !== "debug") { | ||
this.trigger("debug", `Event triggered: ${name}`, args); | ||
} | ||
if (this._events[name] == null) { | ||
return; | ||
} | ||
if (listener.status === "once") { | ||
listener.status = "none"; | ||
} | ||
try { | ||
ret = typeof listener.cb === "function" ? listener.cb(...args) : void 0; | ||
return ret != null ? typeof ret.then === "function" ? ret.then(function() {}).catch((e) => { | ||
return this.trigger("error", e); | ||
}) : void 0 : void 0; | ||
} catch (error) { | ||
e = error; | ||
{ | ||
return this.trigger("error", e); | ||
this._events[name] = this._events[name].filter(function(listener) { | ||
return listener.status !== "none"; | ||
}); | ||
promises = this._events[name].map(async(listener) => { | ||
var e, returned; | ||
if (listener.status === "none") { | ||
return; | ||
} | ||
if (listener.status === "once") { | ||
listener.status = "none"; | ||
} | ||
try { | ||
returned = typeof listener.cb === "function" ? listener.cb(...args) : void 0; | ||
if (((returned != null ? returned.then : void 0) != null) && typeof returned.then === "function") { | ||
return (await returned); | ||
} else { | ||
return returned; | ||
} | ||
} catch (error) { | ||
e = error; | ||
{ | ||
this.trigger("error", e); | ||
} | ||
return null; | ||
} | ||
}); | ||
return ((await Promise.all(promises))).find(function(x) { | ||
return x != null; | ||
}); | ||
} catch (error) { | ||
e = error; | ||
{ | ||
this.trigger("error", e); | ||
} | ||
}); | ||
return null; | ||
} | ||
} | ||
@@ -589,3 +612,3 @@ | ||
var version = "2.14.1"; | ||
var version = "2.15.0"; | ||
var version$1 = { | ||
@@ -965,3 +988,3 @@ version: version | ||
_run(next, wait, index) { | ||
_run(next, wait, index, retryCount) { | ||
var completed, done; | ||
@@ -974,22 +997,26 @@ this.Events.trigger("debug", `Scheduling ${next.options.id}`, { | ||
completed = async(...args) => { | ||
var e, running; | ||
var e, error, eventInfo, retry, retryAfter, running; | ||
if (!done) { | ||
try { | ||
done = true; | ||
this._states.next(next.options.id); // DONE | ||
clearTimeout(this._scheduled[index].expiration); | ||
delete this._scheduled[index]; | ||
this.Events.trigger("debug", `Completed ${next.options.id}`, { | ||
eventInfo = { | ||
args: next.args, | ||
options: next.options | ||
}); | ||
this.Events.trigger("done", `Completed ${next.options.id}`, { | ||
args: next.args, | ||
options: next.options | ||
}); | ||
options: next.options, | ||
retryCount | ||
}; | ||
if ((error = args[0]) != null) { | ||
retry = (await this.Events.trigger("failed", error, eventInfo)); | ||
if (retry != null) { | ||
retryAfter = ~~retry; | ||
this.Events.trigger("retry", `Retrying ${next.options.id} after ${retryAfter} ms`, eventInfo); | ||
return this._run(next, retryAfter, index, retryCount + 1); | ||
} | ||
} | ||
this._states.next(next.options.id); // DONE | ||
this.Events.trigger("debug", `Completed ${next.options.id}`, eventInfo); | ||
this.Events.trigger("done", `Completed ${next.options.id}`, eventInfo); | ||
({running} = (await this._store.__free__(index, next.options.weight))); | ||
this.Events.trigger("debug", `Freed ${next.options.id}`, { | ||
args: next.args, | ||
options: next.options | ||
}); | ||
this.Events.trigger("debug", `Freed ${next.options.id}`, eventInfo); | ||
if (running === 0 && this.empty()) { | ||
@@ -999,4 +1026,4 @@ this.Events.trigger("idle"); | ||
return typeof next.cb === "function" ? next.cb(...args) : void 0; | ||
} catch (error) { | ||
e = error; | ||
} catch (error1) { | ||
e = error1; | ||
return this.Events.trigger("error", e); | ||
@@ -1006,3 +1033,5 @@ } | ||
}; | ||
this._states.next(next.options.id); // RUNNING | ||
if (retryCount === 0) { // RUNNING | ||
this._states.next(next.options.id); | ||
} | ||
return this._scheduled[index] = { | ||
@@ -1014,3 +1043,5 @@ timeout: setTimeout(() => { | ||
}); | ||
this._states.next(next.options.id); // EXECUTING | ||
if (retryCount === 0) { // EXECUTING | ||
this._states.next(next.options.id); | ||
} | ||
if (this._limiter != null) { | ||
@@ -1054,3 +1085,3 @@ return this._limiter.submit(next.options, next.task, ...next.args, completed); | ||
} | ||
this._run(next, wait, index); | ||
this._run(next, wait, index, 0); | ||
} | ||
@@ -1177,4 +1208,4 @@ return this.Promise.resolve(success); | ||
this.Events.trigger("debug", `Queued ${options.id}`, {args, options, reachedHWM, blocked}); | ||
} catch (error) { | ||
e = error; | ||
} catch (error1) { | ||
e = error1; | ||
this._states.remove(options.id); | ||
@@ -1181,0 +1212,0 @@ this.Events.trigger("debug", `Could not queue ${options.id}`, { |
{ | ||
"name": "bottleneck", | ||
"version": "2.14.1", | ||
"version": "2.15.0", | ||
"description": "Distributed task scheduler and rate limiter", | ||
@@ -8,4 +8,4 @@ "main": "lib/index.js", | ||
"scripts": { | ||
"test": "./node_modules/mocha/bin/mocha test", | ||
"test-all": "DATASTORE=ioredis npm test && DATASTORE=redis npm test && BUILD=bundle npm test && npm test" | ||
"test": "mocha test", | ||
"test-all": "./scripts/test_all.sh" | ||
}, | ||
@@ -12,0 +12,0 @@ "repository": { |
@@ -241,1 +241,8 @@ /// <reference path="bottleneck.d.ts" /> | ||
.then(() => console.log("Flushed!")) | ||
const object = {} | ||
const emitter = new Bottleneck.Events(object) | ||
const listenerCount: number = emitter.listenerCount('info') | ||
emitter.trigger('info', 'hello', 'world', 123).then(function (result) { | ||
console.log(result) | ||
}) |
@@ -1,2 +0,2 @@ | ||
if (process.env.BUILD === 'bundle') { | ||
if (process.env.BUILD === 'es5') { | ||
module.exports = require('../es5.js') | ||
@@ -3,0 +3,0 @@ } else if (process.env.BUILD === 'light') { |
@@ -64,2 +64,4 @@ var makeTest = require('./context') | ||
myObject.doSomething() | ||
c.mustEqual(myObject.emitter.listenerCount('info'), 1) | ||
c.mustEqual(myObject.emitter.listenerCount('nothing'), 0) | ||
}) | ||
@@ -66,0 +68,0 @@ |
@@ -97,3 +97,3 @@ var States = require('../lib/States') | ||
} catch (err) { | ||
if (process.env.BUILD !== 'bundle' && process.env.BUILD !== 'light') { | ||
if (process.env.BUILD !== 'es5' && process.env.BUILD !== 'light') { | ||
assert(err instanceof Bottleneck.BottleneckError) | ||
@@ -100,0 +100,0 @@ } |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
548423
87
11365
5