commit b7cba469f08cf60c185a34a9da435a815cbee3d0
parent fe83d4db72d6237cfa99c484bfb1c052f50b107f
Author: Dan Stillman <dstillman@zotero.org>
Date: Tue, 26 Nov 2013 01:12:15 -0500
Add pause() method to ConcurrentCaller
Delay running any new functions for the specified time
Diffstat:
1 file changed, 17 insertions(+), 1 deletion(-)
diff --git 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;
};
@@ -75,6 +76,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
*/
ConcurrentCaller.prototype.fcall = function (func) {
@@ -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, "