2012-04-06 10:26:06 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2011-05-31 05:52:23 +04:00
|
|
|
|
2012-10-31 20:13:28 +04:00
|
|
|
this.EXPORTED_SYMBOLS = ["Async"];
|
2011-05-31 05:52:23 +04:00
|
|
|
|
2015-10-07 15:03:21 +03:00
|
|
|
var {classes: Cc, interfaces: Ci, results: Cr, utils: Cu} = Components;
|
2011-05-31 05:52:23 +04:00
|
|
|
|
|
|
|
// Constants for makeSyncCallback, waitForSyncCallback.
|
|
|
|
const CB_READY = {};
|
|
|
|
const CB_COMPLETE = {};
|
|
|
|
const CB_FAIL = {};
|
|
|
|
|
|
|
|
const REASON_ERROR = Ci.mozIStorageStatementCallback.REASON_ERROR;
|
|
|
|
|
2018-01-30 02:20:18 +03:00
|
|
|
ChromeUtils.import("resource://gre/modules/Services.jsm");
|
2011-05-31 05:52:23 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Helpers for various async operations.
|
|
|
|
*/
|
2012-10-31 20:13:28 +04:00
|
|
|
this.Async = {
|
2011-05-31 05:52:23 +04:00
|
|
|
|
2011-08-26 21:25:42 +04:00
|
|
|
/**
|
|
|
|
* Execute an arbitrary number of asynchronous functions one after the
|
|
|
|
* other, passing the callback arguments on to the next one. All functions
|
|
|
|
* must take a callback function as their last argument. The 'this' object
|
|
|
|
* will be whatever chain()'s is.
|
2012-04-06 10:26:06 +04:00
|
|
|
*
|
2011-08-26 21:25:42 +04:00
|
|
|
* @usage this._chain = Async.chain;
|
|
|
|
* this._chain(this.foo, this.bar, this.baz)(args, for, foo)
|
2012-04-06 10:26:06 +04:00
|
|
|
*
|
2011-08-26 21:25:42 +04:00
|
|
|
* This is equivalent to:
|
|
|
|
*
|
|
|
|
* let self = this;
|
|
|
|
* self.foo(args, for, foo, function (bars, args) {
|
|
|
|
* self.bar(bars, args, function (baz, params) {
|
|
|
|
* self.baz(baz, params);
|
|
|
|
* });
|
|
|
|
* });
|
|
|
|
*/
|
|
|
|
chain: function chain() {
|
|
|
|
let funcs = Array.slice(arguments);
|
|
|
|
let thisObj = this;
|
|
|
|
return function callback() {
|
|
|
|
if (funcs.length) {
|
|
|
|
let args = Array.slice(arguments).concat(callback);
|
|
|
|
let f = funcs.shift();
|
|
|
|
f.apply(thisObj, args);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
2011-05-31 05:52:23 +04:00
|
|
|
/**
|
|
|
|
* Helpers for making asynchronous calls within a synchronous API possible.
|
|
|
|
*
|
|
|
|
* If you value your sanity, do not look closely at the following functions.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a sync callback that remembers state, in particular whether it has
|
|
|
|
* been called.
|
2014-05-28 04:00:00 +04:00
|
|
|
* The returned callback can be called directly passing an optional arg which
|
|
|
|
* will be returned by waitForSyncCallback(). The callback also has a
|
|
|
|
* .throw() method, which takes an error object and will cause
|
|
|
|
* waitForSyncCallback to fail with the error object thrown as an exception
|
|
|
|
* (but note that the .throw method *does not* itself throw - it just causes
|
|
|
|
* the wait function to throw).
|
2011-05-31 05:52:23 +04:00
|
|
|
*/
|
|
|
|
makeSyncCallback: function makeSyncCallback() {
|
|
|
|
// The main callback remembers the value it was passed, and that it got data.
|
|
|
|
let onComplete = function onComplete(data) {
|
|
|
|
onComplete.state = CB_COMPLETE;
|
|
|
|
onComplete.value = data;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Initialize private callback data in preparation for being called.
|
|
|
|
onComplete.state = CB_READY;
|
|
|
|
onComplete.value = null;
|
|
|
|
|
|
|
|
// Allow an alternate callback to trigger an exception to be thrown.
|
|
|
|
onComplete.throw = function onComplete_throw(data) {
|
|
|
|
onComplete.state = CB_FAIL;
|
|
|
|
onComplete.value = data;
|
|
|
|
};
|
|
|
|
|
|
|
|
return onComplete;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Wait for a sync callback to finish.
|
|
|
|
*/
|
|
|
|
waitForSyncCallback: function waitForSyncCallback(callback) {
|
|
|
|
// Grab the current thread so we can make it give up priority.
|
2017-06-21 19:59:28 +03:00
|
|
|
let tm = Cc["@mozilla.org/thread-manager;1"].getService();
|
2011-05-31 05:52:23 +04:00
|
|
|
|
|
|
|
// Keep waiting until our callback is triggered (unless the app is quitting).
|
2017-06-21 19:59:28 +03:00
|
|
|
tm.spinEventLoopUntil(() => !Async.checkAppReady || callback.state != CB_READY);
|
2011-05-31 05:52:23 +04:00
|
|
|
|
|
|
|
// Reset the state of the callback to prepare for another call.
|
|
|
|
let state = callback.state;
|
|
|
|
callback.state = CB_READY;
|
|
|
|
|
|
|
|
// Throw the value the callback decided to fail with.
|
|
|
|
if (state == CB_FAIL) {
|
|
|
|
throw callback.value;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return the value passed to the callback.
|
|
|
|
return callback.value;
|
|
|
|
},
|
|
|
|
|
2011-08-26 21:25:29 +04:00
|
|
|
/**
|
2016-11-15 07:28:52 +03:00
|
|
|
* Check if the app is still ready (not quitting). Returns true, or throws an
|
|
|
|
* exception if not ready.
|
2011-08-26 21:25:29 +04:00
|
|
|
*/
|
|
|
|
checkAppReady: function checkAppReady() {
|
|
|
|
// Watch for app-quit notification to stop any sync calls
|
|
|
|
Services.obs.addObserver(function onQuitApplication() {
|
|
|
|
Services.obs.removeObserver(onQuitApplication, "quit-application");
|
2017-06-06 01:50:07 +03:00
|
|
|
Async.checkAppReady = Async.promiseYield = function() {
|
2015-08-31 04:57:25 +03:00
|
|
|
let exception = Components.Exception("App. Quitting", Cr.NS_ERROR_ABORT);
|
|
|
|
exception.appIsShuttingDown = true;
|
|
|
|
throw exception;
|
2011-08-26 21:25:29 +04:00
|
|
|
};
|
2017-04-14 22:51:38 +03:00
|
|
|
}, "quit-application");
|
2011-08-26 21:25:29 +04:00
|
|
|
// In the common case, checkAppReady just returns true
|
|
|
|
return (Async.checkAppReady = function() { return true; })();
|
|
|
|
},
|
|
|
|
|
2016-11-15 07:28:52 +03:00
|
|
|
/**
|
|
|
|
* Check if the app is still ready (not quitting). Returns true if the app
|
|
|
|
* is ready, or false if it is being shut down.
|
|
|
|
*/
|
|
|
|
isAppReady() {
|
|
|
|
try {
|
2017-10-15 21:50:30 +03:00
|
|
|
return Async.checkAppReady();
|
2016-11-15 07:28:52 +03:00
|
|
|
} catch (ex) {
|
|
|
|
if (!Async.isShutdownException(ex)) {
|
|
|
|
throw ex;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
|
2015-08-31 04:57:25 +03:00
|
|
|
/**
|
|
|
|
* Check if the passed exception is one raised by checkAppReady. Typically
|
|
|
|
* this will be used in exception handlers to allow such exceptions to
|
|
|
|
* make their way to the top frame and allow the app to actually terminate.
|
|
|
|
*/
|
|
|
|
isShutdownException(exception) {
|
|
|
|
return exception && exception.appIsShuttingDown === true;
|
|
|
|
},
|
|
|
|
|
2011-05-31 05:52:23 +04:00
|
|
|
/**
|
|
|
|
* Return the two things you need to make an asynchronous call synchronous
|
|
|
|
* by spinning the event loop.
|
|
|
|
*/
|
|
|
|
makeSpinningCallback: function makeSpinningCallback() {
|
|
|
|
let cb = Async.makeSyncCallback();
|
|
|
|
function callback(error, ret) {
|
|
|
|
if (error)
|
|
|
|
cb.throw(error);
|
2014-05-28 04:00:00 +04:00
|
|
|
else
|
|
|
|
cb(ret);
|
2011-05-31 05:52:23 +04:00
|
|
|
}
|
2015-01-25 10:50:01 +03:00
|
|
|
callback.wait = () => Async.waitForSyncCallback(cb);
|
2011-05-31 05:52:23 +04:00
|
|
|
return callback;
|
|
|
|
},
|
|
|
|
|
2016-04-06 02:42:10 +03:00
|
|
|
promiseSpinningly(promise) {
|
|
|
|
let cb = Async.makeSpinningCallback();
|
2017-01-10 20:09:02 +03:00
|
|
|
promise.then(result => {
|
2016-04-06 02:42:10 +03:00
|
|
|
cb(null, result);
|
|
|
|
}, err => {
|
|
|
|
cb(err || new Error("Promise rejected without explicit error"));
|
|
|
|
});
|
|
|
|
return cb.wait();
|
|
|
|
},
|
2017-06-06 01:50:07 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A "tight loop" of promises can still lock up the browser for some time.
|
|
|
|
* Periodically waiting for a promise returned by this function will solve
|
|
|
|
* that.
|
|
|
|
* You should probably not use this method directly and instead use jankYielder
|
|
|
|
* below.
|
|
|
|
* Some reference here:
|
|
|
|
* - https://gist.github.com/jesstelford/bbb30b983bddaa6e5fef2eb867d37678
|
|
|
|
* - https://bugzilla.mozilla.org/show_bug.cgi?id=1094248
|
|
|
|
*/
|
|
|
|
promiseYield() {
|
|
|
|
return new Promise(resolve => {
|
|
|
|
Services.tm.currentThread.dispatch(resolve, Ci.nsIThread.DISPATCH_NORMAL);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
// Returns a method that yields every X calls.
|
|
|
|
// Common case is calling the returned method every iteration in a loop.
|
|
|
|
jankYielder(yieldEvery = 50) {
|
|
|
|
let iterations = 0;
|
|
|
|
return async () => {
|
|
|
|
Async.checkAppReady(); // Let it throw!
|
|
|
|
if (++iterations % yieldEvery === 0) {
|
|
|
|
await Async.promiseYield();
|
|
|
|
}
|
2017-10-15 21:50:30 +03:00
|
|
|
};
|
2017-06-06 01:50:07 +03:00
|
|
|
}
|
2011-05-31 05:52:23 +04:00
|
|
|
};
|