Q-ize integration.js
Adds a new function, Zotero.promiseGenerator, that returns a promise that is fulfilled by the last thing yielded by a generator, or rejected with an error.
This commit is contained in:
parent
755db116cc
commit
d550ac92b4
4 changed files with 961 additions and 975 deletions
chrome/content/zotero
|
@ -565,10 +565,10 @@ var Zotero_Citation_Dialog = new function () {
|
|||
if(_previewShown) {
|
||||
document.documentElement.getButton("extra2").label = Zotero.getString("citation.hideEditor");
|
||||
if(text) {
|
||||
io.preview(function(preview) {
|
||||
io.preview().then(function(preview) {
|
||||
_originalHTML = preview;
|
||||
editor.value = text;
|
||||
});
|
||||
}).end();
|
||||
} else {
|
||||
_updatePreview();
|
||||
}
|
||||
|
@ -581,9 +581,7 @@ var Zotero_Citation_Dialog = new function () {
|
|||
* called when accept button is clicked
|
||||
*/
|
||||
function accept() {
|
||||
Zotero.debug("Trying to accept");
|
||||
_getCitation();
|
||||
Zotero.debug("got citation");
|
||||
var isCustom = _previewShown && io.citation.citationItems.length // if a citation is selected
|
||||
&& _originalHTML
|
||||
&& document.getElementById('editor').value != _originalHTML // and citation has been edited
|
||||
|
@ -623,7 +621,7 @@ var Zotero_Citation_Dialog = new function () {
|
|||
|
||||
editor.readonly = !io.citation.citationItems.length;
|
||||
if(io.citation.citationItems.length) {
|
||||
io.preview(function(preview) {
|
||||
io.preview().then(function(preview) {
|
||||
editor.value = preview;
|
||||
|
||||
if(editor.initialized) {
|
||||
|
|
|
@ -278,7 +278,7 @@ var Zotero_QuickFormat = new function () {
|
|||
// Save current search so that when we get items, we know whether it's too late to
|
||||
// process them or not
|
||||
var lastSearchTime = currentSearchTime = Date.now();
|
||||
io.getItems(function(citedItems) {
|
||||
io.getItems().then(function(citedItems) {
|
||||
// Don't do anything if panel is already closed
|
||||
if(isAsync &&
|
||||
((referencePanel.state !== "open" && referencePanel.state !== "showing")
|
||||
|
@ -314,7 +314,7 @@ var Zotero_QuickFormat = new function () {
|
|||
}
|
||||
|
||||
_updateItemList(citedItems, citedItemsMatchingSearch, searchResultIDs, isAsync);
|
||||
});
|
||||
}).end();
|
||||
|
||||
if(!completed) {
|
||||
// We are going to have to wait until items have been retrieved from the document.
|
||||
|
|
File diff suppressed because it is too large
Load diff
|
@ -38,6 +38,11 @@ const ZOTERO_CONFIG = {
|
|||
VERSION: "3.0.8.SOURCE"
|
||||
};
|
||||
|
||||
// Commonly used imports accessible anywhere
|
||||
Components.utils.import("resource://zotero/q.js");
|
||||
Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
|
||||
Components.utils.import("resource://gre/modules/Services.jsm");
|
||||
|
||||
/*
|
||||
* Core functions
|
||||
*/
|
||||
|
@ -1465,16 +1470,17 @@ const ZOTERO_CONFIG = {
|
|||
* If errorHandler is specified, exceptions in the generator will be caught
|
||||
* and passed to the callback
|
||||
*/
|
||||
this.pumpGenerator = function(generator, ms, errorHandler) {
|
||||
this.pumpGenerator = function(generator, ms, errorHandler, doneHandler) {
|
||||
_waiting++;
|
||||
|
||||
var timer = Components.classes["@mozilla.org/timer;1"].
|
||||
createInstance(Components.interfaces.nsITimer);
|
||||
createInstance(Components.interfaces.nsITimer),
|
||||
yielded;
|
||||
var timerCallback = {"notify":function() {
|
||||
var err = false;
|
||||
_waiting--;
|
||||
try {
|
||||
if(generator.next()) {
|
||||
if((yielded = generator.next()) !== false) {
|
||||
_waiting++;
|
||||
return;
|
||||
}
|
||||
|
@ -1500,12 +1506,25 @@ const ZOTERO_CONFIG = {
|
|||
} 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 = Q.defer();
|
||||
this.pumpGenerator(generator, ms,
|
||||
function(e) { deferred.reject(e); },
|
||||
function(data) { deferred.resolve(data) });
|
||||
return deferred.promise;
|
||||
};
|
||||
|
||||
/**
|
||||
* Emulates the behavior of window.setTimeout, but ensures that callbacks do not get called
|
||||
|
|
Loading…
Add table
Reference in a new issue