Add Zotero.Sync.Runner.delayIndefinite()

Delays syncing until the returned function is run
This commit is contained in:
Dan Stillman 2018-05-04 19:53:54 -04:00
parent 86b94ae713
commit 7271fdf6b7

View File

@ -70,6 +70,7 @@ Zotero.Sync.Runner_Module = function (options = {}) {
var _enabled = false;
var _autoSyncTimer;
var _delaySyncUntil;
var _delayPromises = [];
var _firstInSession = true;
var _syncInProgress = false;
var _stopping = false;
@ -148,6 +149,21 @@ Zotero.Sync.Runner_Module = function (options = {}) {
yield Zotero.Promise.delay(delay);
}
// If paused, wait until we're done
while (true) {
if (_delayPromises.some(p => p.isPending())) {
this.setSyncStatus(Zotero.getString('sync.status.waiting'));
Zotero.debug("Syncing is paused -- waiting to sync");
yield Zotero.Promise.all(_delayPromises);
// If more were added, continue
if (_delayPromises.some(p => p.isPending())) {
continue;
}
_delayPromises = [];
}
break;
}
// purgeDataObjects() starts a transaction, so if there's an active one then show a
// nice message and wait until there's not. Another transaction could still start
// before purgeDataObjects() and result in a wait timeout, but this should reduce the
@ -958,6 +974,21 @@ Zotero.Sync.Runner_Module = function (options = {}) {
};
/**
* Delay syncs until the returned function is called
*
* @return {Function} - Resolve function
*/
this.delayIndefinite = function () {
var resolve;
var promise = new Zotero.Promise(function () {
resolve = arguments[0];
});
_delayPromises.push(promise);
return resolve;
};
/**
* Trigger updating of the main sync icon, the sync error icon, and
* library-specific sync error icons across all windows