Add pause() method to ConcurrentCaller

Delay running any new functions for the specified time
This commit is contained in:
Dan Stillman 2013-11-26 01:12:15 -05:00
parent fe83d4db72
commit b7cba469f0

View File

@ -57,6 +57,7 @@ ConcurrentCaller = function (numConcurrent) {
this._queue = []; this._queue = [];
this._logger = null; this._logger = null;
this._interval = 0; this._interval = 0;
this._pauseUntil = 0;
}; };
@ -74,6 +75,14 @@ ConcurrentCaller.prototype.setLogger = function (func) {
}; };
/**
* Don't run any new functions for the specified amount of time
*/
ConcurrentCaller.prototype.pause = function (ms) {
this._pauseUntil = Date.now() + ms;
}
/** /**
* @param {Function[]|Function} func One or more functions to run * @param {Function[]|Function} func One or more functions to run
*/ */
@ -128,7 +137,14 @@ ConcurrentCaller.prototype._onFunctionDone = function (promise) {
// run it now // run it now
let f = self._queue.shift(); let f = self._queue.shift();
if (f && self._numRunning < self._numConcurrent) { if (f && self._numRunning < self._numConcurrent) {
Q.delay(self._interval) // Wait until the specified interval has elapsed or the current
// pause (if there is one) is over, whichever is longer
let interval = self._interval;
let now = Date.now();
if (self._pauseUntil > now && (self._pauseUntil - now > interval)) {
interval = self._pauseUntil - now;
}
Q.delay(interval)
.then(function () { .then(function () {
self._log("Running new function (" self._log("Running new function ("
+ self._numRunning + "/" + self._numConcurrent + " running, " + self._numRunning + "/" + self._numConcurrent + " running, "