Accept promise-yielding generators directly in forEachChunkAsync()

This commit is contained in:
Dan Stillman 2015-05-24 22:56:44 -04:00
parent bf1ee0d52b
commit 87fa51849f
3 changed files with 16 additions and 9 deletions

View file

@ -448,7 +448,9 @@ Zotero.DataObjects.prototype.updateVersion = Zotero.Promise.method(function (ids
let sql = "UPDATE " + this.table + " SET version=" + version + " " let sql = "UPDATE " + this.table + " SET version=" + version + " "
+ "WHERE " + this.idColumn + " IN ("; + "WHERE " + this.idColumn + " IN (";
return Zotero.Utilities.Internal.forEachChunkAsync( 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); yield Zotero.DB.queryAsync(sql + chunk.map(() => '?').join(', ') + ')', chunk);
// Update the internal 'version' property of any loaded objects // Update the internal 'version' property of any loaded objects
for (let i = 0; i < chunk.length; i++) { 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); 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) + " " let sql = "UPDATE " + this.table + " SET synced=" + (synced ? 1 : 0) + " "
+ "WHERE " + this.idColumn + " IN ("; + "WHERE " + this.idColumn + " IN (";
return Zotero.Utilities.Internal.forEachChunkAsync( 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); yield Zotero.DB.queryAsync(sql + chunk.map(() => '?').join(', ') + ')', chunk);
// Update the internal 'synced' property of any loaded objects // Update the internal 'synced' property of any loaded objects
for (let i = 0; i < chunk.length; i++) { 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); obj.updateSynced(!!synced, true);
} }
} }
}.bind(this)) }.bind(this)
); );
}); });

View file

@ -252,7 +252,7 @@ Zotero.Tags = new function() {
yield Zotero.Utilities.Internal.forEachChunkAsync( yield Zotero.Utilities.Internal.forEachChunkAsync(
oldItemIDs, oldItemIDs,
Zotero.DB.MAX_BOUND_PARAMETERS - 2, Zotero.DB.MAX_BOUND_PARAMETERS - 2,
Zotero.Promise.coroutine(function* (chunk) { function* (chunk) {
let placeholders = chunk.map(function () '?').join(','); let placeholders = chunk.map(function () '?').join(',');
// This is ugly, but it's much faster than doing replaceTag() for each item // 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.DB.queryAsync(sql, [Zotero.DB.transactionDateTime].concat(chunk));
yield Zotero.Items.reload(oldItemIDs, ['tags'], true); yield Zotero.Items.reload(oldItemIDs, ['tags'], true);
}) }
); );
var notifierData = {}; var notifierData = {};
@ -331,7 +331,7 @@ Zotero.Tags = new function() {
yield Zotero.Utilities.Internal.forEachChunkAsync( yield Zotero.Utilities.Internal.forEachChunkAsync(
Zotero.Utilities.arrayUnique(oldItemIDs), Zotero.Utilities.arrayUnique(oldItemIDs),
Zotero.DB.MAX_BOUND_PARAMETERS - 1, Zotero.DB.MAX_BOUND_PARAMETERS - 1,
Zotero.Promise.coroutine(function* (chunk) { function* (chunk) {
let placeholders = chunk.map(function () '?').join(','); let placeholders = chunk.map(function () '?').join(',');
sql = 'UPDATE items SET clientDateModified=? ' 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.DB.queryAsync(sql, [Zotero.DB.transactionDateTime].concat(chunk));
yield Zotero.Items.reload(oldItemIDs, ['tags']); yield Zotero.Items.reload(oldItemIDs, ['tags']);
}) }
); );
}.bind(this)); }.bind(this));

View file

@ -35,7 +35,8 @@ Zotero.Utilities.Internal = {
* *
* @param {Array} arr * @param {Array} arr
* @param {Integer} chunkSize * @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 * @return {Array} The return values from the successive runs
*/ */
"forEachChunkAsync": Zotero.Promise.coroutine(function* (arr, chunkSize, func) { "forEachChunkAsync": Zotero.Promise.coroutine(function* (arr, chunkSize, func) {
@ -44,6 +45,8 @@ Zotero.Utilities.Internal = {
var num = arr.length; var num = arr.length;
var done = 0; var done = 0;
func = Zotero.Promise.coroutine(func);
do { do {
var chunk = tmpArray.splice(0, chunkSize); var chunk = tmpArray.splice(0, chunkSize);
done += chunk.length; done += chunk.length;