zotero/resource/concurrent-caller.js
Dan Stillman db0fa3c33e Async DB megacommit
Promise-based rewrite of most of the codebase, with asynchronous database and file access -- see https://github.com/zotero/zotero/issues/518 for details.

WARNING: This includes backwards-incompatible schema changes.

An incomplete list of other changes:

- Schema overhaul
  - Replace main tables with new versions with updated schema
  - Enable real foreign key support and remove previous triggers
  - Don't use NULLs for local libraryID, which broke the UNIQUE index
    preventing object key duplication. All code (Zotero and third-party)
    using NULL for the local library will need to be updated to use 0
    instead (already done for Zotero code)
  - Add 'compatibility' DB version that can be incremented manually to break DB
    compatibility with previous versions. 'userdata' upgrades will no longer
    automatically break compatibility.
  - Demote creators and tags from first-class objects to item properties
- New API syncing properties
  - 'synced'/'version' properties to data objects
  - 'etag' to groups
  - 'version' to libraries
- Create Zotero.DataObject that other objects inherit from
- Consolidate data object loading into Zotero.DataObjects
- Change object reloading so that only the loaded and changed parts of objects are reloaded, instead of reloading all data from the database (with some exceptions, including item primary data)
- Items and collections now have .parentItem and .parentKey properties, replacing item.getSource() and item.getSourceKey()
- New function Zotero.serial(fn), to wrap an async function such that all calls are run serially
- New function Zotero.Utilities.Internal.forEachChunkAsync(arr, chunkSize, func)
- Add tag selector loading message
- Various API and name changes, since everything was breaking anyway

Known broken things:

- Syncing (will be completely rewritten for API syncing)
- Translation architecture (needs promise-based rewrite)
- Duplicates view
- DB integrity check (from schema changes)
- Dragging (may be difficult to fix)

Lots of other big and little things are certainly broken, particularly with the UI, which can be affected by async code in all sorts of subtle ways.
2014-08-06 22:59:37 -04:00

183 lines
5 KiB
JavaScript

/*
***** BEGIN LICENSE BLOCK *****
Copyright © 2013 Center for History and New Media
George Mason University, Fairfax, Virginia, USA
http://zotero.org
This file is part of Zotero.
Zotero is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Zotero is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with Zotero. If not, see <http://www.gnu.org/licenses/>.
***** END LICENSE BLOCK *****
*/
EXPORTED_SYMBOLS = ["ConcurrentCaller"];
Components.utils.import("resource://zotero/bluebird.js");
/**
* Call a fixed number of functions at once, queueing the rest until slots
* open and returning a promise for the final completion. The functions do
* not need to return promises, but they should if they have asynchronous
* work to perform.
*
* Example:
*
* var caller = new ConcurrentCaller(2);
* caller.stopOnError = true;
* caller.fcall([foo, bar, baz, qux);
*
* In this example, foo and bar would run immediately, and baz and qux would
* be queued for later. When foo or bar finished, baz would be run, followed
* by qux when another slot opened.
*
* @param {Integer} numConcurrent The number of concurrent functions to run.
*/
ConcurrentCaller = function (numConcurrent) {
if (typeof numConcurrent == 'undefined') {
throw new Error("numConcurrent not provided");
}
this.stopOnError = false;
this.onError = null;
this._numConcurrent = numConcurrent;
this._numRunning = 0;
this._queue = [];
this._logger = null;
this._interval = 0;
this._pauseUntil = 0;
};
/**
* Set the interval between the end of one function run and the beginning
* of another, in milliseconds
*/
ConcurrentCaller.prototype.setInterval = function (ms) {
this._interval = ms;
};
ConcurrentCaller.prototype.setLogger = function (func) {
this._logger = 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) {
if (Array.isArray(func)) {
var promises = [];
for (var i in func) {
//this._log("Running fcall on function");
promises.push(this.fcall(func[i]));
}
return Promise.settle(promises);
}
// If we're at the maximum number of concurrent functions,
// queue this function for later
if (this._numRunning == this._numConcurrent) {
this._log("Already at " + this._numConcurrent + " -- queueing for later");
var deferred = Promise.defer();
this._queue.push({
func: Promise.method(func),
deferred: deferred
});
return deferred.promise;
}
this._log("Running function (" + this._numRunning + " current < " + this._numConcurrent + " max)");
// Otherwise run it now
this._numRunning++;
return this._onFunctionDone(Promise.try(func));
}
ConcurrentCaller.prototype.stop = function () {
self._log("Clearing queue");
self._queue = [];
};
ConcurrentCaller.prototype._onFunctionDone = function (promise) {
var self = this;
return promise.then(function (promise) {
self._numRunning--;
self._log("Done with function ("
+ self._numRunning + "/" + self._numConcurrent + " running, "
+ self._queue.length + " queued)");
// If there's a function to call and we're under the concurrent limit,
// run it now
let f = self._queue.shift();
if (f && self._numRunning < self._numConcurrent) {
// 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;
}
Promise.delay(interval)
.then(function () {
self._log("Running new function ("
+ self._numRunning + "/" + self._numConcurrent + " running, "
+ self._queue.length + " queued)");
self._numRunning++;
var p = self._onFunctionDone(f.func());
f.deferred.resolve(p);
});
}
return promise;
})
.catch(function (e) {
self._numRunning--;
self._log("Done with function (" + self._numRunning + "/" + self._numConcurrent + ", "
+ self._queue.length + " in queue)");
if (self.onError) {
self.onError(e);
}
if (self.stopOnError && self._queue.length) {
self._log("Stopping on error: " + e);
self._queue = [];
}
throw e;
});
};
ConcurrentCaller.prototype._log = function (msg) {
if (this._logger) {
this._logger("[ConcurrentCaller] " + msg);
}
};