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
|
@ -565,10 +565,10 @@ var Zotero_Citation_Dialog = new function () {
|
||||||
if(_previewShown) {
|
if(_previewShown) {
|
||||||
document.documentElement.getButton("extra2").label = Zotero.getString("citation.hideEditor");
|
document.documentElement.getButton("extra2").label = Zotero.getString("citation.hideEditor");
|
||||||
if(text) {
|
if(text) {
|
||||||
io.preview(function(preview) {
|
io.preview().then(function(preview) {
|
||||||
_originalHTML = preview;
|
_originalHTML = preview;
|
||||||
editor.value = text;
|
editor.value = text;
|
||||||
});
|
}).end();
|
||||||
} else {
|
} else {
|
||||||
_updatePreview();
|
_updatePreview();
|
||||||
}
|
}
|
||||||
|
@ -581,9 +581,7 @@ var Zotero_Citation_Dialog = new function () {
|
||||||
* called when accept button is clicked
|
* called when accept button is clicked
|
||||||
*/
|
*/
|
||||||
function accept() {
|
function accept() {
|
||||||
Zotero.debug("Trying to accept");
|
|
||||||
_getCitation();
|
_getCitation();
|
||||||
Zotero.debug("got citation");
|
|
||||||
var isCustom = _previewShown && io.citation.citationItems.length // if a citation is selected
|
var isCustom = _previewShown && io.citation.citationItems.length // if a citation is selected
|
||||||
&& _originalHTML
|
&& _originalHTML
|
||||||
&& document.getElementById('editor').value != _originalHTML // and citation has been edited
|
&& 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;
|
editor.readonly = !io.citation.citationItems.length;
|
||||||
if(io.citation.citationItems.length) {
|
if(io.citation.citationItems.length) {
|
||||||
io.preview(function(preview) {
|
io.preview().then(function(preview) {
|
||||||
editor.value = preview;
|
editor.value = preview;
|
||||||
|
|
||||||
if(editor.initialized) {
|
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
|
// Save current search so that when we get items, we know whether it's too late to
|
||||||
// process them or not
|
// process them or not
|
||||||
var lastSearchTime = currentSearchTime = Date.now();
|
var lastSearchTime = currentSearchTime = Date.now();
|
||||||
io.getItems(function(citedItems) {
|
io.getItems().then(function(citedItems) {
|
||||||
// Don't do anything if panel is already closed
|
// Don't do anything if panel is already closed
|
||||||
if(isAsync &&
|
if(isAsync &&
|
||||||
((referencePanel.state !== "open" && referencePanel.state !== "showing")
|
((referencePanel.state !== "open" && referencePanel.state !== "showing")
|
||||||
|
@ -314,7 +314,7 @@ var Zotero_QuickFormat = new function () {
|
||||||
}
|
}
|
||||||
|
|
||||||
_updateItemList(citedItems, citedItemsMatchingSearch, searchResultIDs, isAsync);
|
_updateItemList(citedItems, citedItemsMatchingSearch, searchResultIDs, isAsync);
|
||||||
});
|
}).end();
|
||||||
|
|
||||||
if(!completed) {
|
if(!completed) {
|
||||||
// We are going to have to wait until items have been retrieved from the document.
|
// 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"
|
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
|
* Core functions
|
||||||
*/
|
*/
|
||||||
|
@ -1465,16 +1470,17 @@ const ZOTERO_CONFIG = {
|
||||||
* If errorHandler is specified, exceptions in the generator will be caught
|
* If errorHandler is specified, exceptions in the generator will be caught
|
||||||
* and passed to the callback
|
* and passed to the callback
|
||||||
*/
|
*/
|
||||||
this.pumpGenerator = function(generator, ms, errorHandler) {
|
this.pumpGenerator = function(generator, ms, errorHandler, doneHandler) {
|
||||||
_waiting++;
|
_waiting++;
|
||||||
|
|
||||||
var timer = Components.classes["@mozilla.org/timer;1"].
|
var timer = Components.classes["@mozilla.org/timer;1"].
|
||||||
createInstance(Components.interfaces.nsITimer);
|
createInstance(Components.interfaces.nsITimer),
|
||||||
|
yielded;
|
||||||
var timerCallback = {"notify":function() {
|
var timerCallback = {"notify":function() {
|
||||||
var err = false;
|
var err = false;
|
||||||
_waiting--;
|
_waiting--;
|
||||||
try {
|
try {
|
||||||
if(generator.next()) {
|
if((yielded = generator.next()) !== false) {
|
||||||
_waiting++;
|
_waiting++;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -1500,12 +1506,25 @@ const ZOTERO_CONFIG = {
|
||||||
} else {
|
} else {
|
||||||
throw err;
|
throw err;
|
||||||
}
|
}
|
||||||
|
} else if(doneHandler) {
|
||||||
|
doneHandler(yielded);
|
||||||
}
|
}
|
||||||
}}
|
}}
|
||||||
timer.initWithCallback(timerCallback, ms ? ms : 0, Components.interfaces.nsITimer.TYPE_REPEATING_SLACK);
|
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
|
// add timer to global scope so that it doesn't get garbage collected before it completes
|
||||||
_runningTimers.push(timer);
|
_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
|
* Emulates the behavior of window.setTimeout, but ensures that callbacks do not get called
|
||||||
|
|
Loading…
Reference in New Issue
Block a user