Take a promise-returning function in Utilities.Internal.forEachChunkAsync()

Instead of a promise-yielding generator
This commit is contained in:
Dan Stillman 2015-07-19 17:24:28 -04:00
parent 4f55f28e7d
commit 9d0d79c9c2
4 changed files with 11 additions and 14 deletions

View file

@ -460,7 +460,7 @@ Zotero.DataObjects.prototype.updateVersion = Zotero.Promise.method(function (ids
return Zotero.Utilities.Internal.forEachChunkAsync( return Zotero.Utilities.Internal.forEachChunkAsync(
ids, ids,
Zotero.DB.MAX_BOUND_PARAMETERS, Zotero.DB.MAX_BOUND_PARAMETERS,
function* (chunk) { Zotero.Promise.coroutine(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++) {
@ -470,7 +470,7 @@ Zotero.DataObjects.prototype.updateVersion = Zotero.Promise.method(function (ids
obj.updateVersion(version, true); obj.updateVersion(version, true);
} }
} }
}.bind(this) }.bind(this))
); );
}); });
@ -487,7 +487,7 @@ Zotero.DataObjects.prototype.updateSynced = Zotero.Promise.method(function (ids,
return Zotero.Utilities.Internal.forEachChunkAsync( return Zotero.Utilities.Internal.forEachChunkAsync(
ids, ids,
Zotero.DB.MAX_BOUND_PARAMETERS, Zotero.DB.MAX_BOUND_PARAMETERS,
function* (chunk) { Zotero.Promise.coroutine(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++) {
@ -497,7 +497,7 @@ Zotero.DataObjects.prototype.updateSynced = Zotero.Promise.method(function (ids,
obj.updateSynced(!!synced, true); obj.updateSynced(!!synced, true);
} }
} }
}.bind(this) }.bind(this))
); );
}); });

View file

@ -528,10 +528,10 @@ Zotero.Items = function() {
deletedIDs = yield this.getDeleted(libraryID, true, days); deletedIDs = yield this.getDeleted(libraryID, true, days);
if (deletedIDs.length) { if (deletedIDs.length) {
yield Zotero.Utilities.Internal.forEachChunkAsync(deletedIDs, 50, function* (chunk) { yield Zotero.Utilities.Internal.forEachChunkAsync(deletedIDs, 50, Zotero.Promise.coroutine(function* (chunk) {
yield this.erase(chunk); yield this.erase(chunk);
yield Zotero.Notifier.trigger('refresh', 'trash', libraryID); yield Zotero.Notifier.trigger('refresh', 'trash', libraryID);
}.bind(this)); }.bind(this)));
} }
if (deletedIDs.length) { if (deletedIDs.length) {

View file

@ -191,7 +191,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,
function* (chunk) { Zotero.Promise.coroutine(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
@ -204,7 +204,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 = {};
@ -292,7 +292,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,
function* (chunk) { Zotero.Promise.coroutine(function* (chunk) {
let placeholders = chunk.map(function () '?').join(','); let placeholders = chunk.map(function () '?').join(',');
sql = 'UPDATE items SET synced=0, clientDateModified=? ' sql = 'UPDATE items SET synced=0, clientDateModified=? '
@ -300,7 +300,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, ['primaryData', 'tags'], true); yield Zotero.Items.reload(oldItemIDs, ['primaryData', 'tags'], true);
} })
); );
}.bind(this)); }.bind(this));

View file

@ -35,8 +35,7 @@ Zotero.Utilities.Internal = {
* *
* @param {Array} arr * @param {Array} arr
* @param {Integer} chunkSize * @param {Integer} chunkSize
* @param {Function|GeneratorFunction} func - A promise-returning function or a * @param {Function} func - A promise-returning function
* 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) {
@ -45,8 +44,6 @@ 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;