Remove pumpGenerator/promiseGenerator

And update integration code to use ES6 generators and Bluebird's .each()
instead.
This commit is contained in:
Dan Stillman 2016-05-17 11:14:49 -04:00
parent 0cc9926309
commit a285e97815
2 changed files with 15 additions and 76 deletions

View file

@ -1455,7 +1455,8 @@ Zotero.Integration.Fields.prototype.updateSession = function() {
if(me._session.reload) {
//this._session.restoreProcessorState(); TODO doesn't appear to be working properly
me._session.updateUpdateIndices();
return Zotero.promiseGenerator(me._session._updateCitations())
// Iterate through citations, yielding for UI updates
return Zotero.Promise.each(me._session._updateCitations(), () => {})
.then(function() {
me._session.updateIndices = {};
me._session.updateItemIDs = {};
@ -1539,9 +1540,14 @@ Zotero.Integration.Fields.prototype.updateDocument = function(forceCitations, fo
// Update citations
this._session.updateUpdateIndices(forceCitations);
var me = this;
return Zotero.promiseGenerator(this._session._updateCitations()).then(function() {
return Zotero.promiseGenerator(me._updateDocument(forceCitations, forceBibliography,
ignoreCitationChanges));
// Iterate through citations, yielding for UI updates
return Zotero.Promise.each(this._session._updateCitations(), () => {}).then(function() {
return Zotero.Promise.each(
me._updateDocument(
forceCitations, forceBibliography, ignoreCitationChanges
),
() => {}
);
});
}
@ -1552,7 +1558,7 @@ Zotero.Integration.Fields.prototype.updateDocument = function(forceCitations, fo
* @param {Boolean} [ignoreCitationChanges] Whether to ignore changes to citations that have been
* modified since they were created, instead of showing a warning
*/
Zotero.Integration.Fields.prototype._updateDocument = function(forceCitations, forceBibliography,
Zotero.Integration.Fields.prototype._updateDocument = function* (forceCitations, forceBibliography,
ignoreCitationChanges) {
if(this.progressCallback) {
var nFieldUpdates = Object.keys(this._session.updateIndices).length;
@ -1569,7 +1575,7 @@ Zotero.Integration.Fields.prototype._updateDocument = function(forceCitations, f
} catch(e) {
Zotero.logError(e);
}
yield undefined;
yield;
}
var citation = this._session.citationsByIndex[i];
@ -1681,7 +1687,7 @@ Zotero.Integration.Fields.prototype._updateDocument = function(forceCitations, f
} catch(e) {
Zotero.logError(e);
}
yield undefined;
yield;
}
if(bibliographyText) {
@ -2639,7 +2645,7 @@ Zotero.Integration.Session.prototype.formatCitation = function(index, citation)
/**
* Updates the list of citations to be serialized to the document
*/
Zotero.Integration.Session.prototype._updateCitations = function() {
Zotero.Integration.Session.prototype._updateCitations = function* () {
/*var allUpdatesForced = false;
var forcedUpdates = {};
if(force) {
@ -2683,7 +2689,7 @@ Zotero.Integration.Session.prototype._updateCitations = function() {
}
this.citeprocCitationIDs[citation.citationID] = true;
delete this.newIndices[index];
yield undefined;
yield;
}
}

View file

@ -1789,73 +1789,6 @@ Components.utils.import("resource://gre/modules/osfile.jsm");
}
/**
* Pumps a generator until it yields false. See itemTreeView.js for an example.
*
* If errorHandler is specified, exceptions in the generator will be caught
* and passed to the callback
*/
this.pumpGenerator = function(generator, ms, errorHandler, doneHandler) {
_waiting++;
var timer = Components.classes["@mozilla.org/timer;1"].
createInstance(Components.interfaces.nsITimer),
yielded,
useJIT = Components.utils.methodjit;
var timerCallback = {"notify":function() {
// XXX Remove when we drop support for Fx <24
if(useJIT !== undefined) Components.utils.methodjit = useJIT;
var err = false;
_waiting--;
try {
if((yielded = generator.next()) !== false) {
_waiting++;
return;
}
} catch(e if e.toString() === "[object StopIteration]") {
// There must be a better way to perform this check
} catch(e) {
err = e;
}
timer.cancel();
_runningTimers.splice(_runningTimers.indexOf(timer), 1);
// requeue nsITimerCallbacks that came up during generator pumping but couldn't execute
for(var i in _waitTimers) {
_waitTimers[i].initWithCallback(_waitTimerCallbacks[i], 0, Components.interfaces.nsITimer.TYPE_ONE_SHOT);
}
_waitTimers = [];
_waitTimerCallbacks = [];
if(err) {
if(errorHandler) {
errorHandler(err);
} else {
throw err;
}
} else if(doneHandler) {
doneHandler(yielded);
}
}}
timer.initWithCallback(timerCallback, ms ? ms : 0, Components.interfaces.nsITimer.TYPE_REPEATING_SLACK);
// add timer to global scope so that it doesn't get garbage collected before it completes
_runningTimers.push(timer);
};
/**
* Pumps a generator until it yields false. Unlike the above, this returns a promise.
*/
this.promiseGenerator = function(generator, ms) {
var deferred = Zotero.Promise.defer();
this.pumpGenerator(generator, ms,
function(e) { deferred.reject(e); },
function(data) { deferred.resolve(data) });
return deferred.promise;
};
this.spawn = function (generator, thisObject) {
if (thisObject) {
return Zotero.Promise.coroutine(generator.bind(thisObject))();