2016-11-22 01:41:20 +03: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/. */
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
2019-01-17 21:18:31 +03:00
|
|
|
const { AppConstants } = ChromeUtils.import(
|
|
|
|
"resource://gre/modules/AppConstants.jsm"
|
|
|
|
);
|
|
|
|
const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
|
|
|
|
const { XPCOMUtils } = ChromeUtils.import(
|
|
|
|
"resource://gre/modules/XPCOMUtils.jsm"
|
|
|
|
);
|
2019-07-05 12:01:24 +03:00
|
|
|
|
2019-01-17 21:18:31 +03:00
|
|
|
const { error, stack, TimeoutError } = ChromeUtils.import(
|
|
|
|
"chrome://marionette/content/error.js"
|
|
|
|
);
|
|
|
|
const { truncate } = ChromeUtils.import(
|
|
|
|
"chrome://marionette/content/format.js"
|
2019-07-05 12:01:24 +03:00
|
|
|
);
|
2019-01-17 21:18:31 +03:00
|
|
|
const { Log } = ChromeUtils.import("chrome://marionette/content/log.js");
|
2018-06-06 16:40:05 +03:00
|
|
|
|
2018-10-11 19:40:36 +03:00
|
|
|
XPCOMUtils.defineLazyGetter(this, "log", Log.get);
|
2016-11-22 01:41:20 +03:00
|
|
|
|
2018-04-17 11:43:27 +03:00
|
|
|
this.EXPORTED_SYMBOLS = [
|
2019-01-10 13:10:14 +03:00
|
|
|
"executeSoon",
|
2018-11-08 16:11:14 +03:00
|
|
|
"DebounceCallback",
|
2018-09-11 18:49:18 +03:00
|
|
|
"IdlePromise",
|
2018-12-20 19:07:02 +03:00
|
|
|
"MessageManagerDestroyedPromise",
|
2018-04-17 11:43:27 +03:00
|
|
|
"PollPromise",
|
2018-08-03 18:10:23 +03:00
|
|
|
"Sleep",
|
2018-04-17 11:43:27 +03:00
|
|
|
"TimedPromise",
|
2019-01-10 13:10:47 +03:00
|
|
|
"waitForEvent",
|
2019-01-10 13:12:17 +03:00
|
|
|
"waitForMessage",
|
2019-01-10 13:14:22 +03:00
|
|
|
"waitForObserverTopic",
|
2018-04-17 11:43:27 +03:00
|
|
|
];
|
|
|
|
|
2017-08-01 20:28:13 +03:00
|
|
|
const { TYPE_ONE_SHOT, TYPE_REPEATING_SLACK } = Ci.nsITimer;
|
|
|
|
|
2018-11-08 16:11:40 +03:00
|
|
|
const PROMISE_TIMEOUT = AppConstants.DEBUG ? 4500 : 1500;
|
|
|
|
|
2019-01-10 13:10:14 +03:00
|
|
|
/**
|
|
|
|
* Dispatch a function to be executed on the main thread.
|
|
|
|
*
|
|
|
|
* @param {function} func
|
|
|
|
* Function to be executed.
|
|
|
|
*/
|
|
|
|
function executeSoon(func) {
|
|
|
|
if (typeof func != "function") {
|
|
|
|
throw new TypeError();
|
|
|
|
}
|
|
|
|
|
|
|
|
Services.tm.dispatchToMainThread(func);
|
|
|
|
}
|
|
|
|
|
2017-07-26 15:11:53 +03:00
|
|
|
/**
|
2017-08-01 20:26:09 +03:00
|
|
|
* @callback Condition
|
2017-07-26 15:11:53 +03:00
|
|
|
*
|
|
|
|
* @param {function(*)} resolve
|
|
|
|
* To be called when the condition has been met. Will return the
|
|
|
|
* resolved value.
|
|
|
|
* @param {function} reject
|
|
|
|
* To be called when the condition has not been met. Will cause
|
|
|
|
* the condition to be revaluated or time out.
|
|
|
|
*
|
|
|
|
* @return {*}
|
2018-02-26 14:44:32 +03:00
|
|
|
* The value from calling ``resolve``.
|
2017-07-26 15:11:53 +03:00
|
|
|
*/
|
|
|
|
|
2016-11-22 01:41:20 +03:00
|
|
|
/**
|
2018-02-26 14:44:32 +03:00
|
|
|
* Runs a Promise-like function off the main thread until it is resolved
|
|
|
|
* through ``resolve`` or ``rejected`` callbacks. The function is
|
|
|
|
* guaranteed to be run at least once, irregardless of the timeout.
|
2016-11-22 01:41:20 +03:00
|
|
|
*
|
2018-02-26 14:44:32 +03:00
|
|
|
* The ``func`` is evaluated every ``interval`` for as long as its
|
|
|
|
* runtime duration does not exceed ``interval``. Evaluations occur
|
|
|
|
* sequentially, meaning that evaluations of ``func`` are queued if
|
|
|
|
* the runtime evaluation duration of ``func`` is greater than ``interval``.
|
2016-11-22 01:41:20 +03:00
|
|
|
*
|
2018-02-26 14:44:32 +03:00
|
|
|
* ``func`` is given two arguments, ``resolve`` and ``reject``,
|
|
|
|
* of which one must be called for the evaluation to complete.
|
|
|
|
* Calling ``resolve`` with an argument indicates that the expected
|
|
|
|
* wait condition was met and will return the passed value to the
|
|
|
|
* caller. Conversely, calling ``reject`` will evaluate ``func``
|
|
|
|
* again until the ``timeout`` duration has elapsed or ``func`` throws.
|
|
|
|
* The passed value to ``reject`` will also be returned to the caller
|
|
|
|
* once the wait has expired.
|
2016-11-22 01:41:20 +03:00
|
|
|
*
|
2018-02-26 14:44:32 +03:00
|
|
|
* Usage::
|
2016-11-22 01:41:20 +03:00
|
|
|
*
|
2017-10-02 18:50:11 +03:00
|
|
|
* let els = new PollPromise((resolve, reject) => {
|
2016-11-22 01:41:20 +03:00
|
|
|
* let res = document.querySelectorAll("p");
|
|
|
|
* if (res.length > 0) {
|
|
|
|
* resolve(Array.from(res));
|
|
|
|
* } else {
|
|
|
|
* reject([]);
|
|
|
|
* }
|
2019-01-10 13:12:24 +03:00
|
|
|
* }, {timeout: 1000});
|
2016-11-22 01:41:20 +03:00
|
|
|
*
|
2017-08-01 20:26:09 +03:00
|
|
|
* @param {Condition} func
|
2016-11-22 01:41:20 +03:00
|
|
|
* Function to run off the main thread.
|
2019-01-10 13:12:24 +03:00
|
|
|
* @param {number=} [timeout] timeout
|
|
|
|
* Desired timeout if wanted. If 0 or less than the runtime evaluation
|
2018-02-26 14:44:32 +03:00
|
|
|
* time of ``func``, ``func`` is guaranteed to run at least once.
|
2019-01-10 13:12:24 +03:00
|
|
|
* Defaults to using no timeout.
|
2017-10-02 18:50:11 +03:00
|
|
|
* @param {number=} [interval=10] interval
|
2018-02-26 14:44:32 +03:00
|
|
|
* Duration between each poll of ``func`` in milliseconds.
|
2017-10-02 18:50:11 +03:00
|
|
|
* Defaults to 10 milliseconds.
|
2016-11-22 01:41:20 +03:00
|
|
|
*
|
2017-07-26 15:11:53 +03:00
|
|
|
* @return {Promise.<*>}
|
2018-02-26 14:44:32 +03:00
|
|
|
* Yields the value passed to ``func``'s
|
|
|
|
* ``resolve`` or ``reject`` callbacks.
|
2016-11-22 01:41:20 +03:00
|
|
|
*
|
2017-07-26 15:11:53 +03:00
|
|
|
* @throws {*}
|
2018-02-26 14:44:32 +03:00
|
|
|
* If ``func`` throws, its error is propagated.
|
2018-08-03 18:08:45 +03:00
|
|
|
* @throws {TypeError}
|
|
|
|
* If `timeout` or `interval`` are not numbers.
|
|
|
|
* @throws {RangeError}
|
|
|
|
* If `timeout` or `interval` are not unsigned integers.
|
2016-11-22 01:41:20 +03:00
|
|
|
*/
|
2019-01-10 13:12:24 +03:00
|
|
|
function PollPromise(func, { timeout = null, interval = 10 } = {}) {
|
2016-11-22 01:41:20 +03:00
|
|
|
const timer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
|
|
|
|
|
2018-08-03 18:08:45 +03:00
|
|
|
if (typeof func != "function") {
|
|
|
|
throw new TypeError();
|
|
|
|
}
|
2019-01-10 13:12:24 +03:00
|
|
|
if (timeout != null && typeof timeout != "number") {
|
2018-08-03 18:08:45 +03:00
|
|
|
throw new TypeError();
|
|
|
|
}
|
2019-01-10 13:12:24 +03:00
|
|
|
if (typeof interval != "number") {
|
|
|
|
throw new TypeError();
|
|
|
|
}
|
|
|
|
if (
|
|
|
|
(timeout && (!Number.isInteger(timeout) || timeout < 0)) ||
|
2020-03-14 02:38:52 +03:00
|
|
|
!Number.isInteger(interval) ||
|
|
|
|
interval < 0
|
2018-08-03 18:08:45 +03:00
|
|
|
) {
|
|
|
|
throw new RangeError();
|
|
|
|
}
|
|
|
|
|
2016-11-22 01:41:20 +03:00
|
|
|
return new Promise((resolve, reject) => {
|
2019-01-10 13:12:24 +03:00
|
|
|
let start, end;
|
|
|
|
|
|
|
|
if (Number.isInteger(timeout)) {
|
|
|
|
start = new Date().getTime();
|
|
|
|
end = start + timeout;
|
|
|
|
}
|
2016-11-22 01:41:20 +03:00
|
|
|
|
|
|
|
let evalFn = () => {
|
|
|
|
new Promise(func)
|
|
|
|
.then(resolve, rejected => {
|
|
|
|
if (error.isError(rejected)) {
|
|
|
|
throw rejected;
|
|
|
|
}
|
2019-07-05 12:01:24 +03:00
|
|
|
|
2019-01-10 13:12:24 +03:00
|
|
|
// return if there is a timeout and set to 0,
|
|
|
|
// allowing |func| to be evaluated at least once
|
|
|
|
if (
|
|
|
|
typeof end != "undefined" &&
|
|
|
|
(start == end || new Date().getTime() >= end)
|
|
|
|
) {
|
2016-11-22 01:41:20 +03:00
|
|
|
resolve(rejected);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(reject);
|
|
|
|
};
|
|
|
|
|
|
|
|
// the repeating slack timer waits |interval|
|
|
|
|
// before invoking |evalFn|
|
|
|
|
evalFn();
|
|
|
|
|
2017-08-01 20:28:13 +03:00
|
|
|
timer.init(evalFn, interval, TYPE_REPEATING_SLACK);
|
2016-11-22 01:41:20 +03:00
|
|
|
}).then(
|
|
|
|
res => {
|
|
|
|
timer.cancel();
|
|
|
|
return res;
|
|
|
|
},
|
|
|
|
err => {
|
|
|
|
timer.cancel();
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
);
|
2017-10-02 18:50:11 +03:00
|
|
|
}
|
2017-08-01 20:28:13 +03:00
|
|
|
|
|
|
|
/**
|
2018-02-26 14:44:32 +03:00
|
|
|
* Represents the timed, eventual completion (or failure) of an
|
|
|
|
* asynchronous operation, and its resulting value.
|
2017-08-01 20:28:13 +03:00
|
|
|
*
|
2018-02-26 14:44:32 +03:00
|
|
|
* In contrast to a regular Promise, it times out after ``timeout``.
|
2017-08-01 20:28:13 +03:00
|
|
|
*
|
|
|
|
* @param {Condition} func
|
2018-02-26 14:44:32 +03:00
|
|
|
* Function to run, which will have its ``reject``
|
|
|
|
* callback invoked after the ``timeout`` duration is reached.
|
|
|
|
* It is given two callbacks: ``resolve(value)`` and
|
|
|
|
* ``reject(error)``.
|
2018-11-08 16:11:40 +03:00
|
|
|
* @param {timeout=} timeout
|
2018-02-26 14:44:32 +03:00
|
|
|
* ``condition``'s ``reject`` callback will be called
|
2018-08-03 18:08:45 +03:00
|
|
|
* after this timeout, given in milliseconds.
|
2018-11-08 16:11:40 +03:00
|
|
|
* By default 1500 ms in an optimised build and 4500 ms in
|
|
|
|
* debug builds.
|
2017-08-01 20:28:13 +03:00
|
|
|
* @param {Error=} [throws=TimeoutError] throws
|
2018-02-26 14:44:32 +03:00
|
|
|
* When the ``timeout`` is hit, this error class will be
|
2017-08-01 20:28:13 +03:00
|
|
|
* thrown. If it is null, no error is thrown and the promise is
|
|
|
|
* instead resolved on timeout.
|
|
|
|
*
|
|
|
|
* @return {Promise.<*>}
|
|
|
|
* Timed promise.
|
2018-08-03 18:08:45 +03:00
|
|
|
*
|
|
|
|
* @throws {TypeError}
|
|
|
|
* If `timeout` is not a number.
|
|
|
|
* @throws {RangeError}
|
|
|
|
* If `timeout` is not an unsigned integer.
|
2017-08-01 20:28:13 +03:00
|
|
|
*/
|
2018-11-08 16:11:40 +03:00
|
|
|
function TimedPromise(
|
|
|
|
fn,
|
|
|
|
{ timeout = PROMISE_TIMEOUT, throws = TimeoutError } = {}
|
|
|
|
) {
|
2017-08-01 20:28:13 +03:00
|
|
|
const timer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
|
|
|
|
|
2018-08-03 18:08:45 +03:00
|
|
|
if (typeof fn != "function") {
|
|
|
|
throw new TypeError();
|
|
|
|
}
|
|
|
|
if (typeof timeout != "number") {
|
|
|
|
throw new TypeError();
|
|
|
|
}
|
|
|
|
if (!Number.isInteger(timeout) || timeout < 0) {
|
|
|
|
throw new RangeError();
|
|
|
|
}
|
|
|
|
|
2017-08-01 20:28:13 +03:00
|
|
|
return new Promise((resolve, reject) => {
|
2020-01-02 17:39:33 +03:00
|
|
|
let trace;
|
|
|
|
|
2017-08-01 20:28:13 +03:00
|
|
|
// Reject only if |throws| is given. Otherwise it is assumed that
|
|
|
|
// the user is OK with the promise timing out.
|
2017-10-03 16:35:47 +03:00
|
|
|
let bail = () => {
|
2017-08-01 20:28:13 +03:00
|
|
|
if (throws !== null) {
|
|
|
|
let err = new throws();
|
|
|
|
reject(err);
|
|
|
|
} else {
|
2020-01-02 17:39:33 +03:00
|
|
|
log.warn(`TimedPromise timed out after ${timeout} ms`, trace);
|
2017-08-01 20:28:13 +03:00
|
|
|
resolve();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-01-02 17:39:33 +03:00
|
|
|
trace = stack();
|
2017-08-01 20:28:13 +03:00
|
|
|
timer.initWithCallback({ notify: bail }, timeout, TYPE_ONE_SHOT);
|
|
|
|
|
|
|
|
try {
|
|
|
|
fn(resolve, reject);
|
|
|
|
} catch (e) {
|
|
|
|
reject(e);
|
|
|
|
}
|
|
|
|
}).then(
|
|
|
|
res => {
|
|
|
|
timer.cancel();
|
|
|
|
return res;
|
|
|
|
},
|
|
|
|
err => {
|
|
|
|
timer.cancel();
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
2018-04-17 11:43:27 +03:00
|
|
|
|
2018-08-03 18:10:23 +03:00
|
|
|
/**
|
|
|
|
* Pauses for the given duration.
|
|
|
|
*
|
|
|
|
* @param {number} timeout
|
|
|
|
* Duration to wait before fulfilling promise in milliseconds.
|
|
|
|
*
|
|
|
|
* @return {Promise}
|
|
|
|
* Promise that fulfills when the `timeout` is elapsed.
|
|
|
|
*
|
|
|
|
* @throws {TypeError}
|
|
|
|
* If `timeout` is not a number.
|
|
|
|
* @throws {RangeError}
|
|
|
|
* If `timeout` is not an unsigned integer.
|
|
|
|
*/
|
|
|
|
function Sleep(timeout) {
|
|
|
|
if (typeof timeout != "number") {
|
|
|
|
throw new TypeError();
|
|
|
|
}
|
2019-02-23 13:54:36 +03:00
|
|
|
return new TimedPromise(() => {}, { timeout, throws: null });
|
2018-08-03 18:10:23 +03:00
|
|
|
}
|
|
|
|
|
2018-12-20 19:07:02 +03:00
|
|
|
/**
|
|
|
|
* Detects when the specified message manager has been destroyed.
|
|
|
|
*
|
|
|
|
* One can observe the removal and detachment of a content browser
|
|
|
|
* (`<xul:browser>`) or a chrome window by its message manager
|
|
|
|
* disconnecting.
|
|
|
|
*
|
|
|
|
* When a browser is associated with a tab, this is safer than only
|
|
|
|
* relying on the event `TabClose` which signalises the _intent to_
|
|
|
|
* remove a tab and consequently would lead to the destruction of
|
|
|
|
* the content browser and its browser message manager.
|
|
|
|
*
|
|
|
|
* When closing a chrome window it is safer than only relying on
|
|
|
|
* the event 'unload' which signalises the _intent to_ close the
|
|
|
|
* chrome window and consequently would lead to the destruction of
|
|
|
|
* the window and its window message manager.
|
|
|
|
*
|
|
|
|
* @param {MessageListenerManager} messageManager
|
|
|
|
* The message manager to observe for its disconnect state.
|
|
|
|
* Use the browser message manager when closing a content browser,
|
|
|
|
* and the window message manager when closing a chrome window.
|
|
|
|
*
|
|
|
|
* @return {Promise}
|
|
|
|
* A promise that resolves when the message manager has been destroyed.
|
|
|
|
*/
|
|
|
|
function MessageManagerDestroyedPromise(messageManager) {
|
|
|
|
return new Promise(resolve => {
|
|
|
|
function observe(subject, topic) {
|
|
|
|
log.trace(`Received observer notification ${topic}`);
|
|
|
|
|
|
|
|
if (subject == messageManager) {
|
|
|
|
Services.obs.removeObserver(this, "message-manager-disconnect");
|
|
|
|
resolve();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Services.obs.addObserver(observe, "message-manager-disconnect");
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-09-11 18:49:18 +03:00
|
|
|
/**
|
|
|
|
* Throttle until the main thread is idle and `window` has performed
|
|
|
|
* an animation frame (in that order).
|
|
|
|
*
|
2018-09-18 15:51:21 +03:00
|
|
|
* @param {ChromeWindow} win
|
2018-09-11 18:49:18 +03:00
|
|
|
* Window to request the animation frame from.
|
|
|
|
*
|
|
|
|
* @return Promise
|
|
|
|
*/
|
2018-09-18 15:51:21 +03:00
|
|
|
function IdlePromise(win) {
|
2018-09-11 18:49:18 +03:00
|
|
|
return new Promise(resolve => {
|
|
|
|
Services.tm.idleDispatchToMainThread(() => {
|
2018-09-18 15:51:21 +03:00
|
|
|
win.requestAnimationFrame(resolve);
|
2018-09-11 18:49:18 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2018-11-08 16:11:14 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Wraps a callback function, that, as long as it continues to be
|
|
|
|
* invoked, will not be triggered. The given function will be
|
|
|
|
* called after the timeout duration is reached, after no more
|
|
|
|
* events fire.
|
|
|
|
*
|
|
|
|
* This class implements the {@link EventListener} interface,
|
|
|
|
* which means it can be used interchangably with `addEventHandler`.
|
|
|
|
*
|
|
|
|
* Debouncing events can be useful when dealing with e.g. DOM events
|
|
|
|
* that fire at a high rate. It is generally advisable to avoid
|
|
|
|
* computationally expensive operations such as DOM modifications
|
|
|
|
* under these circumstances.
|
|
|
|
*
|
|
|
|
* One such high frequenecy event is `resize` that can fire multiple
|
|
|
|
* times before the window reaches its final dimensions. In order
|
|
|
|
* to delay an operation until the window has completed resizing,
|
|
|
|
* it is possible to use this technique to only invoke the callback
|
|
|
|
* after the last event has fired::
|
|
|
|
*
|
|
|
|
* let cb = new DebounceCallback(event => {
|
|
|
|
* // fires after the final resize event
|
|
|
|
* console.log("resize", event);
|
|
|
|
* });
|
|
|
|
* window.addEventListener("resize", cb);
|
|
|
|
*
|
|
|
|
* Note that it is not possible to use this synchronisation primitive
|
|
|
|
* with `addEventListener(..., {once: true})`.
|
|
|
|
*
|
|
|
|
* @param {function(Event)} fn
|
|
|
|
* Callback function that is guaranteed to be invoked once only,
|
|
|
|
* after `timeout`.
|
|
|
|
* @param {number=} [timeout = 250] timeout
|
|
|
|
* Time since last event firing, before `fn` will be invoked.
|
|
|
|
*/
|
|
|
|
class DebounceCallback {
|
|
|
|
constructor(fn, { timeout = 250 } = {}) {
|
|
|
|
if (typeof fn != "function" || typeof timeout != "number") {
|
|
|
|
throw new TypeError();
|
|
|
|
}
|
|
|
|
if (!Number.isInteger(timeout) || timeout < 0) {
|
|
|
|
throw new RangeError();
|
|
|
|
}
|
|
|
|
|
|
|
|
this.fn = fn;
|
|
|
|
this.timeout = timeout;
|
|
|
|
this.timer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
|
|
|
|
}
|
|
|
|
|
|
|
|
handleEvent(ev) {
|
|
|
|
this.timer.cancel();
|
|
|
|
this.timer.initWithCallback(
|
|
|
|
() => {
|
|
|
|
this.timer.cancel();
|
|
|
|
this.fn(ev);
|
|
|
|
},
|
|
|
|
this.timeout,
|
|
|
|
TYPE_ONE_SHOT
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.DebounceCallback = DebounceCallback;
|
2019-01-10 13:10:47 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Wait for an event to be fired on a specified element.
|
|
|
|
*
|
|
|
|
* This method has been duplicated from BrowserTestUtils.jsm.
|
|
|
|
*
|
|
|
|
* Because this function is intended for testing, any error in checkFn
|
|
|
|
* will cause the returned promise to be rejected instead of waiting for
|
|
|
|
* the next event, since this is probably a bug in the test.
|
|
|
|
*
|
|
|
|
* Usage::
|
|
|
|
*
|
|
|
|
* let promiseEvent = waitForEvent(element, "eventName");
|
|
|
|
* // Do some processing here that will cause the event to be fired
|
|
|
|
* // ...
|
|
|
|
* // Now wait until the Promise is fulfilled
|
|
|
|
* let receivedEvent = await promiseEvent;
|
|
|
|
*
|
|
|
|
* The promise resolution/rejection handler for the returned promise is
|
|
|
|
* guaranteed not to be called until the next event tick after the event
|
|
|
|
* listener gets called, so that all other event listeners for the element
|
|
|
|
* are executed before the handler is executed::
|
|
|
|
*
|
|
|
|
* let promiseEvent = waitForEvent(element, "eventName");
|
|
|
|
* // Same event tick here.
|
|
|
|
* await promiseEvent;
|
|
|
|
* // Next event tick here.
|
|
|
|
*
|
|
|
|
* If some code, such like adding yet another event listener, needs to be
|
|
|
|
* executed in the same event tick, use raw addEventListener instead and
|
|
|
|
* place the code inside the event listener::
|
|
|
|
*
|
|
|
|
* element.addEventListener("load", () => {
|
|
|
|
* // Add yet another event listener in the same event tick as the load
|
|
|
|
* // event listener.
|
|
|
|
* p = waitForEvent(element, "ready");
|
|
|
|
* }, { once: true });
|
|
|
|
*
|
|
|
|
* @param {Element} subject
|
|
|
|
* The element that should receive the event.
|
|
|
|
* @param {string} eventName
|
|
|
|
* Name of the event to listen to.
|
|
|
|
* @param {Object=} options
|
|
|
|
* Extra options.
|
|
|
|
* @param {boolean=} options.capture
|
|
|
|
* True to use a capturing listener.
|
|
|
|
* @param {function(Event)=} options.checkFn
|
|
|
|
* Called with the ``Event`` object as argument, should return ``true``
|
|
|
|
* if the event is the expected one, or ``false`` if it should be
|
|
|
|
* ignored and listening should continue. If not specified, the first
|
|
|
|
* event with the specified name resolves the returned promise.
|
|
|
|
* @param {boolean=} options.wantsUntrusted
|
|
|
|
* True to receive synthetic events dispatched by web content.
|
|
|
|
*
|
|
|
|
* @return {Promise.<Event>}
|
|
|
|
* Promise which resolves to the received ``Event`` object, or rejects
|
|
|
|
* in case of a failure.
|
|
|
|
*/
|
|
|
|
function waitForEvent(
|
|
|
|
subject,
|
|
|
|
eventName,
|
|
|
|
{ capture = false, checkFn = null, wantsUntrusted = false } = {}
|
|
|
|
) {
|
|
|
|
if (subject == null || !("addEventListener" in subject)) {
|
|
|
|
throw new TypeError();
|
|
|
|
}
|
|
|
|
if (typeof eventName != "string") {
|
|
|
|
throw new TypeError();
|
|
|
|
}
|
|
|
|
if (capture != null && typeof capture != "boolean") {
|
|
|
|
throw new TypeError();
|
|
|
|
}
|
|
|
|
if (checkFn != null && typeof checkFn != "function") {
|
|
|
|
throw new TypeError();
|
|
|
|
}
|
|
|
|
if (wantsUntrusted != null && typeof wantsUntrusted != "boolean") {
|
|
|
|
throw new TypeError();
|
|
|
|
}
|
|
|
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
subject.addEventListener(
|
|
|
|
eventName,
|
|
|
|
function listener(event) {
|
|
|
|
log.trace(`Received DOM event ${event.type} for ${event.target}`);
|
|
|
|
try {
|
|
|
|
if (checkFn && !checkFn(event)) {
|
2019-07-05 12:01:24 +03:00
|
|
|
return;
|
|
|
|
}
|
2019-01-10 13:10:47 +03:00
|
|
|
subject.removeEventListener(eventName, listener, capture);
|
|
|
|
executeSoon(() => resolve(event));
|
|
|
|
} catch (ex) {
|
2019-07-05 12:01:24 +03:00
|
|
|
try {
|
2019-01-10 13:10:47 +03:00
|
|
|
subject.removeEventListener(eventName, listener, capture);
|
|
|
|
} catch (ex2) {
|
|
|
|
// Maybe the provided object does not support removeEventListener.
|
2019-07-05 12:01:24 +03:00
|
|
|
}
|
2019-01-10 13:10:47 +03:00
|
|
|
executeSoon(() => reject(ex));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
capture,
|
|
|
|
wantsUntrusted
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
2019-01-10 13:12:17 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Wait for a message to be fired from a particular message manager.
|
|
|
|
*
|
|
|
|
* This method has been duplicated from BrowserTestUtils.jsm.
|
|
|
|
*
|
|
|
|
* @param {nsIMessageManager} messageManager
|
|
|
|
* The message manager that should be used.
|
|
|
|
* @param {string} messageName
|
|
|
|
* The message to wait for.
|
|
|
|
* @param {Object=} options
|
|
|
|
* Extra options.
|
|
|
|
* @param {function(Message)=} options.checkFn
|
|
|
|
* Called with the ``Message`` object as argument, should return ``true``
|
|
|
|
* if the message is the expected one, or ``false`` if it should be
|
|
|
|
* ignored and listening should continue. If not specified, the first
|
|
|
|
* message with the specified name resolves the returned promise.
|
|
|
|
*
|
|
|
|
* @return {Promise.<Object>}
|
|
|
|
* Promise which resolves to the data property of the received
|
|
|
|
* ``Message``.
|
|
|
|
*/
|
|
|
|
function waitForMessage(
|
|
|
|
messageManager,
|
|
|
|
messageName,
|
|
|
|
{ checkFn = undefined } = {}
|
|
|
|
) {
|
|
|
|
if (messageManager == null || !("addMessageListener" in messageManager)) {
|
|
|
|
throw new TypeError();
|
|
|
|
}
|
|
|
|
if (typeof messageName != "string") {
|
|
|
|
throw new TypeError();
|
|
|
|
}
|
|
|
|
if (checkFn && typeof checkFn != "function") {
|
|
|
|
throw new TypeError();
|
|
|
|
}
|
|
|
|
|
|
|
|
return new Promise(resolve => {
|
|
|
|
messageManager.addMessageListener(messageName, function onMessage(msg) {
|
|
|
|
log.trace(`Received ${messageName} for ${msg.target}`);
|
|
|
|
if (checkFn && !checkFn(msg)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
messageManager.removeMessageListener(messageName, onMessage);
|
|
|
|
resolve(msg.data);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2019-01-10 13:14:22 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Wait for the specified observer topic to be observed.
|
|
|
|
*
|
|
|
|
* This method has been duplicated from TestUtils.jsm.
|
|
|
|
*
|
|
|
|
* Because this function is intended for testing, any error in checkFn
|
|
|
|
* will cause the returned promise to be rejected instead of waiting for
|
|
|
|
* the next notification, since this is probably a bug in the test.
|
|
|
|
*
|
|
|
|
* @param {string} topic
|
|
|
|
* The topic to observe.
|
|
|
|
* @param {Object=} options
|
|
|
|
* Extra options.
|
|
|
|
* @param {function(String,Object)=} options.checkFn
|
|
|
|
* Called with ``subject``, and ``data`` as arguments, should return true
|
|
|
|
* if the notification is the expected one, or false if it should be
|
|
|
|
* ignored and listening should continue. If not specified, the first
|
|
|
|
* notification for the specified topic resolves the returned promise.
|
|
|
|
*
|
|
|
|
* @return {Promise.<Array<String, Object>>}
|
|
|
|
* Promise which resolves to an array of ``subject``, and ``data`` from
|
|
|
|
* the observed notification.
|
|
|
|
*/
|
|
|
|
function waitForObserverTopic(topic, { checkFn = null } = {}) {
|
|
|
|
if (typeof topic != "string") {
|
|
|
|
throw new TypeError();
|
|
|
|
}
|
|
|
|
if (checkFn != null && typeof checkFn != "function") {
|
|
|
|
throw new TypeError();
|
|
|
|
}
|
|
|
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
Services.obs.addObserver(function observer(subject, topic, data) {
|
|
|
|
log.trace(`Received observer notification ${topic}`);
|
|
|
|
try {
|
|
|
|
if (checkFn && !checkFn(subject, data)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Services.obs.removeObserver(observer, topic);
|
|
|
|
resolve({ subject, data });
|
|
|
|
} catch (ex) {
|
|
|
|
Services.obs.removeObserver(observer, topic);
|
|
|
|
reject(ex);
|
|
|
|
}
|
|
|
|
}, topic);
|
|
|
|
});
|
|
|
|
}
|