commit 78365a095f5cad778bb7177dcdce21f043cb4261
parent 303ba345549c89d9ecb33f06ef34300062f463a8
Author: Dan Stillman <dstillman@zotero.org>
Date: Thu, 19 Mar 2009 22:25:00 +0000
Add Zotero.Utilities.processAsync(sets, callbacks, onDone) -- this can be used from translators to make it easier to correctly chain sets of asynchronous callbacks, since most translators that require multiple callbacks currently do it incorrectly.
This function definition will be automatically prepended to translators sent to older clients from the repo.
Diffstat:
1 file changed, 37 insertions(+), 0 deletions(-)
diff --git a/chrome/content/zotero/xpcom/utilities.js b/chrome/content/zotero/xpcom/utilities.js
@@ -535,6 +535,42 @@ Zotero.Utilities.prototype.capitalizeTitle = function(string, force) {
return string;
}
+
+/**
+ * Run sets of data through multiple asynchronous callbacks
+ *
+ * Each callback is passed the current set and a callback to call when done
+ *
+ * @param {Object[]} sets Sets of data
+ * @param {Function[]} callbacks
+ * @param {Function} onDone Function to call when done
+ */
+Zotero.Utilities.prototype.processAsync = function (sets, callbacks, onDone) {
+ var currentSet;
+ var index = 0;
+
+ var nextSet = function () {
+ if (!sets.length) {
+ onDone();
+ return;
+ }
+ index = 0;
+ currentSet = sets.shift();
+ callbacks[0](currentSet, nextCallback);
+ };
+ var nextCallback = function () {
+ callbacks[index](currentSet, nextCallback);
+ index++;
+ };
+
+ // Add a final callback to proceed to the next set
+ callbacks[callbacks.length] = function () {
+ nextSet();
+ }
+ nextSet();
+}
+
+
/**
* @class All functions accessible from within Zotero.Utilities namespace inside sandboxed
* translators
@@ -666,6 +702,7 @@ Zotero.Utilities.Translate.prototype.getItemArray = function(doc, inHere, urlRe,
return availableItems;
}
+
/**
* Load a single document in a hidden browser
*