sync module

Provides an assortment of synchronisation primitives.

class MessageManagerDestroyedPromise(messageManager)

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.

Arguments:
  • messageManager (MessageListenerManager) – 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.
Returns:

Promise – A promise that resolves when the message manager has been destroyed.

class PollPromise(func)

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.

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.

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.

Usage:

let els = new PollPromise((resolve, reject) => {
  let res = document.querySelectorAll("p");
  if (res.length > 0) {
    resolve(Array.from(res));
  } else {
    reject([]);
  }
});
Arguments:
  • func (Condition) – Function to run off the main thread.
  • timeout (number) – timeout Desired timeout. If 0 or less than the runtime evaluation time of func, func is guaranteed to run at least once. The default is 2000 milliseconds.
  • interval (number) – interval Duration between each poll of func in milliseconds. Defaults to 10 milliseconds.
Throws:
  • * – If func throws, its error is propagated.
  • TypeError – If timeout or interval` are not numbers.
  • RangeError – If timeout or interval are not unsigned integers.
Returns:

Promise.<*> – Yields the value passed to func’s resolve or reject callbacks.

class Sleep(timeout)

Pauses for the given duration.

Arguments:
  • timeout (number) – Duration to wait before fulfilling promise in milliseconds.
Throws:
  • TypeError – If timeout is not a number.
  • RangeError – If timeout is not an unsigned integer.
Returns:

Promise – Promise that fulfills when the timeout is elapsed.

class TimedPromise(fn)

Represents the timed, eventual completion (or failure) of an asynchronous operation, and its resulting value.

In contrast to a regular Promise, it times out after timeout.

Arguments:
  • func (Condition) – 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).
  • timeout (timeout) – timeout condition’s reject callback will be called after this timeout, given in milliseconds.
  • throws (Error) – throws When the timeout is hit, this error class will be thrown. If it is null, no error is thrown and the promise is instead resolved on timeout.
Throws:
  • TypeError – If timeout is not a number.
  • RangeError – If timeout is not an unsigned integer.
Returns:

Promise.<*> – Timed promise.