commit 87fa51849f7387cef4d1a9caf1f64d1acf2e9328
parent bf1ee0d52b9efe5b86460b2dbe31d3551e6eab01
Author: Dan Stillman <dstillman@zotero.org>
Date: Sun, 24 May 2015 22:56:44 -0400
Accept promise-yielding generators directly in forEachChunkAsync()
Diffstat:
3 files changed, 16 insertions(+), 9 deletions(-)
diff --git a/chrome/content/zotero/xpcom/data/dataObjects.js b/chrome/content/zotero/xpcom/data/dataObjects.js
@@ -448,7 +448,9 @@ Zotero.DataObjects.prototype.updateVersion = Zotero.Promise.method(function (ids
let sql = "UPDATE " + this.table + " SET version=" + version + " "
+ "WHERE " + this.idColumn + " IN (";
return Zotero.Utilities.Internal.forEachChunkAsync(
- ids, Zotero.DB.MAX_BOUND_PARAMETERS, Zotero.Promise.coroutine(function* (chunk) {
+ ids,
+ Zotero.DB.MAX_BOUND_PARAMETERS,
+ function* (chunk) {
yield Zotero.DB.queryAsync(sql + chunk.map(() => '?').join(', ') + ')', chunk);
// Update the internal 'version' property of any loaded objects
for (let i = 0; i < chunk.length; i++) {
@@ -458,7 +460,7 @@ Zotero.DataObjects.prototype.updateVersion = Zotero.Promise.method(function (ids
obj.updateVersion(version, true);
}
}
- }.bind(this))
+ }.bind(this)
);
});
@@ -473,7 +475,9 @@ Zotero.DataObjects.prototype.updateSynced = Zotero.Promise.method(function (ids,
let sql = "UPDATE " + this.table + " SET synced=" + (synced ? 1 : 0) + " "
+ "WHERE " + this.idColumn + " IN (";
return Zotero.Utilities.Internal.forEachChunkAsync(
- ids, Zotero.DB.MAX_BOUND_PARAMETERS, Zotero.Promise.coroutine(function* (chunk) {
+ ids,
+ Zotero.DB.MAX_BOUND_PARAMETERS,
+ function* (chunk) {
yield Zotero.DB.queryAsync(sql + chunk.map(() => '?').join(', ') + ')', chunk);
// Update the internal 'synced' property of any loaded objects
for (let i = 0; i < chunk.length; i++) {
@@ -483,7 +487,7 @@ Zotero.DataObjects.prototype.updateSynced = Zotero.Promise.method(function (ids,
obj.updateSynced(!!synced, true);
}
}
- }.bind(this))
+ }.bind(this)
);
});
diff --git a/chrome/content/zotero/xpcom/data/tags.js b/chrome/content/zotero/xpcom/data/tags.js
@@ -252,7 +252,7 @@ Zotero.Tags = new function() {
yield Zotero.Utilities.Internal.forEachChunkAsync(
oldItemIDs,
Zotero.DB.MAX_BOUND_PARAMETERS - 2,
- Zotero.Promise.coroutine(function* (chunk) {
+ function* (chunk) {
let placeholders = chunk.map(function () '?').join(',');
// This is ugly, but it's much faster than doing replaceTag() for each item
@@ -265,7 +265,7 @@ Zotero.Tags = new function() {
yield Zotero.DB.queryAsync(sql, [Zotero.DB.transactionDateTime].concat(chunk));
yield Zotero.Items.reload(oldItemIDs, ['tags'], true);
- })
+ }
);
var notifierData = {};
@@ -331,7 +331,7 @@ Zotero.Tags = new function() {
yield Zotero.Utilities.Internal.forEachChunkAsync(
Zotero.Utilities.arrayUnique(oldItemIDs),
Zotero.DB.MAX_BOUND_PARAMETERS - 1,
- Zotero.Promise.coroutine(function* (chunk) {
+ function* (chunk) {
let placeholders = chunk.map(function () '?').join(',');
sql = 'UPDATE items SET clientDateModified=? '
@@ -339,7 +339,7 @@ Zotero.Tags = new function() {
yield Zotero.DB.queryAsync(sql, [Zotero.DB.transactionDateTime].concat(chunk));
yield Zotero.Items.reload(oldItemIDs, ['tags']);
- })
+ }
);
}.bind(this));
diff --git a/chrome/content/zotero/xpcom/utilities_internal.js b/chrome/content/zotero/xpcom/utilities_internal.js
@@ -35,7 +35,8 @@ Zotero.Utilities.Internal = {
*
* @param {Array} arr
* @param {Integer} chunkSize
- * @param {Function} func - A promise-returning function
+ * @param {Function|GeneratorFunction} func - A promise-returning function or a
+ * promise-yielding generator
* @return {Array} The return values from the successive runs
*/
"forEachChunkAsync": Zotero.Promise.coroutine(function* (arr, chunkSize, func) {
@@ -44,6 +45,8 @@ Zotero.Utilities.Internal = {
var num = arr.length;
var done = 0;
+ func = Zotero.Promise.coroutine(func);
+
do {
var chunk = tmpArray.splice(0, chunkSize);
done += chunk.length;