B2G/gecko/dom/browser-element/mochitest/browserElementTestHelpers.js
/**
 * Creates deferred object containing fresh promise & methods to either resolve
 * or reject it. The result is an object with the following properties:
 * - `promise` Eventual value representation implementing CommonJS [Promises/A]
 *   (http://wiki.commonjs.org/wiki/Promises/A) API.
 * - `resolve` Single shot function that resolves enclosed `promise` with a
 *   given `value`.
 * - `reject` Single shot function that rejects enclosed `promise` with a given
 *   `reason`.
 *
 * An optional `prototype` argument is used as a prototype of the returned
 * `promise` allowing one to implement additional API. If prototype is not
 * passed then it falls back to `Object.prototype`.
 *
 *  ## Example
 *
 *  function fetchURI(uri, type) {
 *    var deferred = defer();
 *    var request = new XMLHttpRequest();
 *    request.open("GET", uri, true);
 *    request.responseType = type;
 *    request.onload = function onload() {
 *      deferred.resolve(request.response);
 *    }
 *    request.onerror = function(event) {
 *     deferred.reject(event);
 *    }
 *    request.send();
 *
 *    return deferred.promise;
 *  }
 */
Creates deferred object containing fresh promise & methods to either resolve
or reject it. The result is an object with the following properties:
- `promise` Eventual value representation implementing CommonJS [Promises/A]
API.

An optional `prototype` argument is used as a prototype of the returned
`promise` allowing one to implement additional API. If prototype is not
passed then it falls back to `Object.prototype`.

Example




Creates deferred object containing fresh promise & methods to either resolve or reject it . The result is an object with the following properties : - ` promise ` Eventual value representation implementing CommonJS [ Promises / A ] API . An optional ` prototype ` argument is used as a prototype of the returned ` promise ` allowing one to implement additional API . If prototype is not passed then it falls back to ` Object . prototype ` . Example
function defer(prototype) {
  var observers = [];
  var result = null;
  prototype = (prototype || prototype === null) ? prototype : Object.prototype;
  var promise = Object.create(prototype, {
    then: { value: function then(onFulfill, onError) {
      var deferred = defer(prototype);
      function resolve(value) {
        try {
          deferred.resolve(onFulfill ? onFulfill(value) : value);
        }
        catch(error) {
          if (exports._reportErrors && typeof(console) === 'object')
            console.error(error);
          deferred.resolve(rejected(error));
        }
      }
      function reject(reason) {
        try {
          if (onError) deferred.resolve(onError(reason));
          else deferred.resolve(rejected(reason));
        }
        catch(error) {
          if (exports._reportErrors && typeof(console) === 'object')
            console.error(error)
          deferred.resolve(rejected(error));
        }
      }
      if (observers) {
        observers.push({ resolve: resolve, reject: reject });
      }
      else {
        result.then(resolve, reject);
      }
      return deferred.promise;
    }}
  })
  var deferred = {
    promise: promise,
    resolve: function resolve(value) {
      if (!result) {
        result = isPromise(value) ? value : fulfilled(value);
        while (observers.length) {
          var observer = observers.shift();
          result.then(observer.resolve, observer.reject);
        }
        observers = null;
      }
    },
    reject: function reject(reason) {
      deferred.resolve(rejected(reason));
    }
  };
  return deferred;
}
function defer ( prototype ) { var observers = [ ] ; var result = null ; prototype = ( prototype || prototype === null ) ? prototype : Object . prototype ; var promise = Object . create ( prototype , { then : { value : function then ( onFulfill , onError ) { var deferred = defer ( prototype ) ; function resolve ( value ) { try { deferred . resolve ( onFulfill ? onFulfill ( value ) : value ) ; } catch ( error ) { if ( exports . _reportErrors && typeof ( console ) === 'object' ) console . error ( error ) ; deferred . resolve ( rejected ( error ) ) ; } } function reject ( reason ) { try { if ( onError ) deferred . resolve ( onError ( reason ) ) ; else deferred . resolve ( rejected ( reason ) ) ; } catch ( error ) { if ( exports . _reportErrors && typeof ( console ) === 'object' ) console . error ( error ) deferred . resolve ( rejected ( error ) ) ; } } if ( observers ) { observers . push ( { resolve : resolve , reject : reject } ) ; } else { result . then ( resolve , reject ) ; } return deferred . promise ; } } } ) var deferred = { promise : promise , resolve : function resolve ( value ) { if ( ! result ) { result = isPromise ( value ) ? value : fulfilled ( value ) ; while ( observers . length ) { var observer = observers . shift ( ) ; result . then ( observer . resolve , observer . reject ) ; } observers = null ; } } , reject : function reject ( reason ) { deferred . resolve ( rejected ( reason ) ) ; } } ; return deferred ; }
Creates deferred object containing fresh promise & methods to either resolve
 or reject it.
Creates deferred object containing fresh promise & methods to either resolve or reject it .
// Define FIFO queue of observer pairs. Once promise is resolved & all queued // observers are forwarded to `result` and variable is set to `null`. // Promise `result`, which will be assigned a resolution value once promise // is resolved. Note that result will always be assigned promise (or alike) // object to take care of propagation through promise chains. If result is // `null` promise is not resolved yet. // Create an object implementing promise API. // If `onFulfill` handler is provided resolve `deferred.promise` with // result of invoking it with a resolution value. If handler is not // provided propagate value through. // `onFulfill` may throw exception in which case resulting promise // is rejected with thrown exception. // Note: Following is equivalent of `deferred.reject(error)`, // we use this shortcut to reduce a stack. // If enclosed promise (`this.promise`) observers queue is still alive // enqueue a new observer pair into it. Note that this does not // necessary means that promise is pending, it may already be resolved, // but we still have to queue observers to guarantee an order of // propagation. // Otherwise just forward observer pair right to a `result` promise. /**
     * Resolves associated `promise` to a given `value`, unless it's already
     * resolved or rejected. Note that resolved promise is not necessary a
     * successfully fulfilled. Promise may be resolved with a promise `value`
     * in which case `value` promise's fulfillment / rejection will propagate
     * up to a promise resolved with `value`.
     */ // Store resolution `value` in a `result` as a promise, so that all // the subsequent handlers can be simply forwarded to it. Since // `result` will be a promise all the value / error propagation will // be uniformly taken care of. // Forward already registered observers to a `result` promise in the // order they were registered. Note that we intentionally dequeue // observer at a time until queue is exhausted. This makes sure that // handlers registered as side effect of observer forwarding are // queued instead of being invoked immediately, guaranteeing FIFO // order. // Once `observers` queue is exhausted we `null`-ify it, so that // new handlers are forwarded straight to the `result`. /**
     * Rejects associated `promise` with a given `reason`, unless it's already
     * resolved / rejected. This is just a (better performing) convenience
     * shortcut for `deferred.resolve(reject(reason))`.
     */ // Note that if promise is resolved that does not necessary means that it // is successfully fulfilled. Resolution value may be a promise in which // case its result propagates. In other words if promise `a` is resolved // with promise `b`, `a` is either fulfilled or rejected depending // on weather `b` is fulfilled or rejected. Here `deferred.promise` is // resolved with a promise pre-rejected with a given `reason`, there for // `deferred.promise` is rejected with a given `reason`. This may feel // little awkward first, but doing it this way greatly simplifies // propagation through promise chains.