Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* vim: set ts=2 et sw=2 tw=80 filetype=javascript: */ |
michael@0 | 3 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 6 | |
michael@0 | 7 | "use strict"; |
michael@0 | 8 | |
michael@0 | 9 | this.EXPORTED_SYMBOLS = [ |
michael@0 | 10 | "Promise" |
michael@0 | 11 | ]; |
michael@0 | 12 | |
michael@0 | 13 | /** |
michael@0 | 14 | * This module implements the "promise" construct, according to the |
michael@0 | 15 | * "Promises/A+" proposal as known in April 2013, documented here: |
michael@0 | 16 | * |
michael@0 | 17 | * <http://promises-aplus.github.com/promises-spec/> |
michael@0 | 18 | * |
michael@0 | 19 | * A promise is an object representing a value that may not be available yet. |
michael@0 | 20 | * Internally, a promise can be in one of three states: |
michael@0 | 21 | * |
michael@0 | 22 | * - Pending, when the final value is not available yet. This is the only state |
michael@0 | 23 | * that may transition to one of the other two states. |
michael@0 | 24 | * |
michael@0 | 25 | * - Resolved, when and if the final value becomes available. A resolution |
michael@0 | 26 | * value becomes permanently associated with the promise. This may be any |
michael@0 | 27 | * value, including "undefined". |
michael@0 | 28 | * |
michael@0 | 29 | * - Rejected, if an error prevented the final value from being determined. A |
michael@0 | 30 | * rejection reason becomes permanently associated with the promise. This may |
michael@0 | 31 | * be any value, including "undefined", though it is generally an Error |
michael@0 | 32 | * object, like in exception handling. |
michael@0 | 33 | * |
michael@0 | 34 | * A reference to an existing promise may be received by different means, for |
michael@0 | 35 | * example as the return value of a call into an asynchronous API. In this |
michael@0 | 36 | * case, the state of the promise can be observed but not directly controlled. |
michael@0 | 37 | * |
michael@0 | 38 | * To observe the state of a promise, its "then" method must be used. This |
michael@0 | 39 | * method registers callback functions that are called as soon as the promise is |
michael@0 | 40 | * either resolved or rejected. The method returns a new promise, that in turn |
michael@0 | 41 | * is resolved or rejected depending on the state of the original promise and on |
michael@0 | 42 | * the behavior of the callbacks. For example, unhandled exceptions in the |
michael@0 | 43 | * callbacks cause the new promise to be rejected, even if the original promise |
michael@0 | 44 | * is resolved. See the documentation of the "then" method for details. |
michael@0 | 45 | * |
michael@0 | 46 | * Promises may also be created using the "Promise.defer" function, the main |
michael@0 | 47 | * entry point of this module. The function, along with the new promise, |
michael@0 | 48 | * returns separate methods to change its state to be resolved or rejected. |
michael@0 | 49 | * See the documentation of the "Deferred" prototype for details. |
michael@0 | 50 | * |
michael@0 | 51 | * ----------------------------------------------------------------------------- |
michael@0 | 52 | * |
michael@0 | 53 | * Cu.import("resource://gre/modules/Promise.jsm"); |
michael@0 | 54 | * |
michael@0 | 55 | * // This function creates and returns a new promise. |
michael@0 | 56 | * function promiseValueAfterTimeout(aValue, aTimeout) |
michael@0 | 57 | * { |
michael@0 | 58 | * let deferred = Promise.defer(); |
michael@0 | 59 | * |
michael@0 | 60 | * try { |
michael@0 | 61 | * // An asynchronous operation will trigger the resolution of the promise. |
michael@0 | 62 | * // In this example, we don't have a callback that triggers a rejection. |
michael@0 | 63 | * do_timeout(aTimeout, function () { |
michael@0 | 64 | * deferred.resolve(aValue); |
michael@0 | 65 | * }); |
michael@0 | 66 | * } catch (ex) { |
michael@0 | 67 | * // Generally, functions returning promises propagate exceptions through |
michael@0 | 68 | * // the returned promise, though they may also choose to fail early. |
michael@0 | 69 | * deferred.reject(ex); |
michael@0 | 70 | * } |
michael@0 | 71 | * |
michael@0 | 72 | * // We don't return the deferred to the caller, but only the contained |
michael@0 | 73 | * // promise, so that the caller cannot accidentally change its state. |
michael@0 | 74 | * return deferred.promise; |
michael@0 | 75 | * } |
michael@0 | 76 | * |
michael@0 | 77 | * // This code uses the promise returned be the function above. |
michael@0 | 78 | * let promise = promiseValueAfterTimeout("Value", 1000); |
michael@0 | 79 | * |
michael@0 | 80 | * let newPromise = promise.then(function onResolve(aValue) { |
michael@0 | 81 | * do_print("Resolved with this value: " + aValue); |
michael@0 | 82 | * }, function onReject(aReason) { |
michael@0 | 83 | * do_print("Rejected with this reason: " + aReason); |
michael@0 | 84 | * }); |
michael@0 | 85 | * |
michael@0 | 86 | * // Unexpected errors should always be reported at the end of a promise chain. |
michael@0 | 87 | * newPromise.then(null, Components.utils.reportError); |
michael@0 | 88 | * |
michael@0 | 89 | * ----------------------------------------------------------------------------- |
michael@0 | 90 | */ |
michael@0 | 91 | |
michael@0 | 92 | // These constants must be defined on the "this" object for them to be visible |
michael@0 | 93 | // by subscripts in B2G, since "this" does not match the global scope. |
michael@0 | 94 | this.Cc = Components.classes; |
michael@0 | 95 | this.Ci = Components.interfaces; |
michael@0 | 96 | this.Cu = Components.utils; |
michael@0 | 97 | this.Cr = Components.results; |
michael@0 | 98 | |
michael@0 | 99 | this.Cc["@mozilla.org/moz/jssubscript-loader;1"] |
michael@0 | 100 | .getService(this.Ci.mozIJSSubScriptLoader) |
michael@0 | 101 | .loadSubScript("resource://gre/modules/Promise-backend.js", this); |