From b7cba469f08cf60c185a34a9da435a815cbee3d0 Mon Sep 17 00:00:00 2001 From: Dan Stillman Date: Tue, 26 Nov 2013 01:12:15 -0500 Subject: [PATCH] Add pause() method to ConcurrentCaller Delay running any new functions for the specified time --- resource/concurrent-caller.js | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/resource/concurrent-caller.js b/resource/concurrent-caller.js index b02824757..29f936a40 100644 --- a/resource/concurrent-caller.js +++ b/resource/concurrent-caller.js @@ -57,6 +57,7 @@ ConcurrentCaller = function (numConcurrent) { this._queue = []; this._logger = null; 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 */ @@ -128,7 +137,14 @@ ConcurrentCaller.prototype._onFunctionDone = function (promise) { // run it now let f = self._queue.shift(); 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 () { self._log("Running new function (" + self._numRunning + "/" + self._numConcurrent + " running, "