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
|
|
|
|
2018-02-23 22:50:01 +03:00
|
|
|
var EXPORTED_SYMBOLS = ["Async"];
|
2011-05-31 05:52:23 +04:00
|
|
|
|
2019-01-17 21:18:31 +03:00
|
|
|
const {Services} = ChromeUtils.import("resource://gre/modules/Services.jsm");
|
2011-05-31 05:52:23 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Helpers for various async operations.
|
|
|
|
*/
|
2018-02-23 22:50:01 +03:00
|
|
|
var 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-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;
|
|
|
|
},
|
|
|
|
|
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);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2019-04-11 23:05:24 +03:00
|
|
|
// Returns a method that yields every X calls.
|
|
|
|
// Common case is calling the returned method every iteration in a loop.
|
|
|
|
jankYielder(yieldEvery = 50) {
|
2017-06-06 01:50:07 +03:00
|
|
|
let iterations = 0;
|
2019-04-11 23:05:24 +03:00
|
|
|
return async () => {
|
|
|
|
Async.checkAppReady(); // Let it throw!
|
|
|
|
if (++iterations % yieldEvery === 0) {
|
|
|
|
await Async.promiseYield();
|
|
|
|
}
|
2017-10-15 21:50:30 +03:00
|
|
|
};
|
2018-01-05 02:06:57 +03:00
|
|
|
},
|
|
|
|
|
2018-02-22 21:19:16 +03:00
|
|
|
/**
|
2019-04-11 23:05:24 +03:00
|
|
|
* Turn a synchronous iterator/iterable into an async iterator that calls a
|
|
|
|
* Async.jankYielder at each step.
|
2018-02-22 21:19:16 +03:00
|
|
|
*
|
|
|
|
* @param iterable {Iterable}
|
2019-04-11 23:05:24 +03:00
|
|
|
* Iterable or iterator that should be wrapped. (Anything usable in
|
|
|
|
* for..of should work)
|
Bug 1536170 - Replace Async.jankYielder r=tcsc,markh,eoger
`Async.jankYielder` is known to, unfortunately, cause jank by creating a lot of
immediately resolved promises that must be then GCed. For a collection of 50
items, it will create 50 promises and 49 of them will immediately resolve.
Instead of `Async.jankYielder`, we now have `Async.yieldState`, which simply
keeps track of whether or not the caller should yield to the event loop. Two
higher level looping constructs are built on top of it:
* `Async.yieldingIterator`, which has been rewritten to not create extraneous
promises; and
* `Async.yieldingForEach`, which is a replacement for awaiting
`Async.jankYielder` in a loop. Instead, it accepts the loop body as a
function.
Each of these can share an instance of an `Async.yieldState`, which allows an
object with multiple loops to yield every N iterations overall, instead of
every N iterations of each loop, which keeps the behaviour of using one
`Async.jankYielders` in multiple places.
Differential Revision: https://phabricator.services.mozilla.com/D26229
--HG--
extra : moz-landing-system : lando
2019-04-11 21:39:43 +03:00
|
|
|
*
|
2019-04-11 23:05:24 +03:00
|
|
|
* @param [maybeYield = 50] {number|() => Promise<void>}
|
|
|
|
* Either an existing jankYielder to use, or a number to provide as the
|
|
|
|
* argument to Async.jankYielder.
|
2018-02-22 21:19:16 +03:00
|
|
|
*/
|
2019-04-11 23:05:24 +03:00
|
|
|
async* yieldingIterator(iterable, maybeYield = 50) {
|
|
|
|
if (typeof maybeYield == "number") {
|
|
|
|
maybeYield = Async.jankYielder(maybeYield);
|
|
|
|
}
|
|
|
|
for (let item of iterable) {
|
|
|
|
await maybeYield();
|
|
|
|
yield item;
|
2018-02-22 21:19:16 +03:00
|
|
|
}
|
Bug 1536170 - Replace Async.jankYielder r=tcsc,markh,eoger
`Async.jankYielder` is known to, unfortunately, cause jank by creating a lot of
immediately resolved promises that must be then GCed. For a collection of 50
items, it will create 50 promises and 49 of them will immediately resolve.
Instead of `Async.jankYielder`, we now have `Async.yieldState`, which simply
keeps track of whether or not the caller should yield to the event loop. Two
higher level looping constructs are built on top of it:
* `Async.yieldingIterator`, which has been rewritten to not create extraneous
promises; and
* `Async.yieldingForEach`, which is a replacement for awaiting
`Async.jankYielder` in a loop. Instead, it accepts the loop body as a
function.
Each of these can share an instance of an `Async.yieldState`, which allows an
object with multiple loops to yield every N iterations overall, instead of
every N iterations of each loop, which keeps the behaviour of using one
`Async.jankYielders` in multiple places.
Differential Revision: https://phabricator.services.mozilla.com/D26229
--HG--
extra : moz-landing-system : lando
2019-04-11 21:39:43 +03:00
|
|
|
},
|
|
|
|
|
2018-01-05 02:06:57 +03:00
|
|
|
asyncQueueCaller(log) {
|
|
|
|
return new AsyncQueueCaller(log);
|
|
|
|
},
|
|
|
|
|
|
|
|
asyncObserver(log, obj) {
|
|
|
|
return new AsyncObserver(log, obj);
|
2018-08-31 08:59:17 +03:00
|
|
|
},
|
2011-05-31 05:52:23 +04:00
|
|
|
};
|
2018-01-05 02:06:57 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Allows consumers to enqueue asynchronous callbacks to be called in order.
|
|
|
|
* Typically this is used when providing a callback to a caller that doesn't
|
|
|
|
* await on promises.
|
|
|
|
*/
|
|
|
|
class AsyncQueueCaller {
|
|
|
|
constructor(log) {
|
|
|
|
this._log = log;
|
|
|
|
this._queue = Promise.resolve();
|
2018-04-23 06:55:06 +03:00
|
|
|
this.QueryInterface = ChromeUtils.generateQI([Ci.nsIObserver, Ci.nsISupportsWeakReference]);
|
2018-01-05 02:06:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* /!\ Never await on another function that calls enqueueCall /!\
|
|
|
|
* on the same queue or we will deadlock.
|
|
|
|
*/
|
|
|
|
enqueueCall(func) {
|
|
|
|
this._queue = (async () => {
|
|
|
|
await this._queue;
|
|
|
|
try {
|
2018-05-29 21:30:22 +03:00
|
|
|
return await func();
|
2018-01-05 02:06:57 +03:00
|
|
|
} catch (e) {
|
|
|
|
this._log.error(e);
|
2018-05-29 21:30:22 +03:00
|
|
|
return false;
|
2018-01-05 02:06:57 +03:00
|
|
|
}
|
|
|
|
})();
|
|
|
|
}
|
|
|
|
|
|
|
|
promiseCallsComplete() {
|
|
|
|
return this._queue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Subclass of AsyncQueueCaller that can be used with Services.obs directly.
|
|
|
|
* When this observe() is called, it will enqueue a call to the consumers's
|
|
|
|
* observe().
|
|
|
|
*/
|
|
|
|
class AsyncObserver extends AsyncQueueCaller {
|
|
|
|
constructor(obj, log) {
|
|
|
|
super(log);
|
|
|
|
this.obj = obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
observe(subject, topic, data) {
|
|
|
|
this.enqueueCall(() => this.obj.observe(subject, topic, data));
|
|
|
|
}
|
|
|
|
|
|
|
|
promiseObserversComplete() {
|
|
|
|
return this.promiseCallsComplete();
|
|
|
|
}
|
|
|
|
}
|