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 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. |
michael@0 | 4 | */ |
michael@0 | 5 | |
michael@0 | 6 | 'use strict'; |
michael@0 | 7 | |
michael@0 | 8 | /* |
michael@0 | 9 | * Uses `Promise.jsm` as a core implementation, with additional sugar |
michael@0 | 10 | * from previous implementation, with inspiration from `Q` and `when` |
michael@0 | 11 | * |
michael@0 | 12 | * https://developer.mozilla.org/en-US/docs/Mozilla/JavaScript_code_modules/Promise.jsm |
michael@0 | 13 | * https://github.com/cujojs/when |
michael@0 | 14 | * https://github.com/kriskowal/q |
michael@0 | 15 | */ |
michael@0 | 16 | const PROMISE_URI = 'resource://gre/modules/Promise.jsm'; |
michael@0 | 17 | |
michael@0 | 18 | getEnvironment.call(this, function ({ require, exports, module, Cu }) { |
michael@0 | 19 | |
michael@0 | 20 | const { defer, resolve, all, reject, race } = Cu.import(PROMISE_URI, {}).Promise; |
michael@0 | 21 | |
michael@0 | 22 | module.metadata = { |
michael@0 | 23 | 'stability': 'unstable' |
michael@0 | 24 | }; |
michael@0 | 25 | |
michael@0 | 26 | let promised = (function() { |
michael@0 | 27 | // Note: Define shortcuts and utility functions here in order to avoid |
michael@0 | 28 | // slower property accesses and unnecessary closure creations on each |
michael@0 | 29 | // call of this popular function. |
michael@0 | 30 | |
michael@0 | 31 | var call = Function.call; |
michael@0 | 32 | var concat = Array.prototype.concat; |
michael@0 | 33 | |
michael@0 | 34 | // Utility function that does following: |
michael@0 | 35 | // execute([ f, self, args...]) => f.apply(self, args) |
michael@0 | 36 | function execute (args) call.apply(call, args) |
michael@0 | 37 | |
michael@0 | 38 | // Utility function that takes promise of `a` array and maybe promise `b` |
michael@0 | 39 | // as arguments and returns promise for `a.concat(b)`. |
michael@0 | 40 | function promisedConcat(promises, unknown) { |
michael@0 | 41 | return promises.then(function (values) { |
michael@0 | 42 | return resolve(unknown) |
michael@0 | 43 | .then(function (value) values.concat([value])); |
michael@0 | 44 | }); |
michael@0 | 45 | } |
michael@0 | 46 | |
michael@0 | 47 | return function promised(f, prototype) { |
michael@0 | 48 | /** |
michael@0 | 49 | Returns a wrapped `f`, which when called returns a promise that resolves to |
michael@0 | 50 | `f(...)` passing all the given arguments to it, which by the way may be |
michael@0 | 51 | promises. Optionally second `prototype` argument may be provided to be used |
michael@0 | 52 | a prototype for a returned promise. |
michael@0 | 53 | |
michael@0 | 54 | ## Example |
michael@0 | 55 | |
michael@0 | 56 | var promise = promised(Array)(1, promise(2), promise(3)) |
michael@0 | 57 | promise.then(console.log) // => [ 1, 2, 3 ] |
michael@0 | 58 | **/ |
michael@0 | 59 | |
michael@0 | 60 | return function promised() { |
michael@0 | 61 | // create array of [ f, this, args... ] |
michael@0 | 62 | return concat.apply([ f, this ], arguments). |
michael@0 | 63 | // reduce it via `promisedConcat` to get promised array of fulfillments |
michael@0 | 64 | reduce(promisedConcat, resolve([], prototype)). |
michael@0 | 65 | // finally map that to promise of `f.apply(this, args...)` |
michael@0 | 66 | then(execute); |
michael@0 | 67 | }; |
michael@0 | 68 | }; |
michael@0 | 69 | })(); |
michael@0 | 70 | |
michael@0 | 71 | exports.promised = promised; |
michael@0 | 72 | exports.all = all; |
michael@0 | 73 | exports.defer = defer; |
michael@0 | 74 | exports.resolve = resolve; |
michael@0 | 75 | exports.reject = reject; |
michael@0 | 76 | exports.race = race; |
michael@0 | 77 | exports.Promise = Promise; |
michael@0 | 78 | |
michael@0 | 79 | }); |
michael@0 | 80 | |
michael@0 | 81 | function getEnvironment (callback) { |
michael@0 | 82 | let Cu, _exports, _module, _require; |
michael@0 | 83 | |
michael@0 | 84 | // CommonJS / SDK |
michael@0 | 85 | if (typeof(require) === 'function') { |
michael@0 | 86 | Cu = require('chrome').Cu; |
michael@0 | 87 | _exports = exports; |
michael@0 | 88 | _module = module; |
michael@0 | 89 | _require = require; |
michael@0 | 90 | } |
michael@0 | 91 | // JSM |
michael@0 | 92 | else if (String(this).indexOf('BackstagePass') >= 0) { |
michael@0 | 93 | Cu = this['Components'].utils; |
michael@0 | 94 | _exports = this.Promise = {}; |
michael@0 | 95 | _module = { uri: __URI__, id: 'promise/core' }; |
michael@0 | 96 | _require = uri => { |
michael@0 | 97 | let imports = {}; |
michael@0 | 98 | Cu.import(uri, imports); |
michael@0 | 99 | return imports; |
michael@0 | 100 | }; |
michael@0 | 101 | this.EXPORTED_SYMBOLS = ['Promise']; |
michael@0 | 102 | // mozIJSSubScriptLoader.loadSubscript |
michael@0 | 103 | } else if (~String(this).indexOf('Sandbox')) { |
michael@0 | 104 | Cu = this['Components'].utils; |
michael@0 | 105 | _exports = this; |
michael@0 | 106 | _module = { id: 'promise/core' }; |
michael@0 | 107 | _require = uri => {}; |
michael@0 | 108 | } |
michael@0 | 109 | |
michael@0 | 110 | callback({ |
michael@0 | 111 | Cu: Cu, |
michael@0 | 112 | exports: _exports, |
michael@0 | 113 | module: _module, |
michael@0 | 114 | require: _require |
michael@0 | 115 | }); |
michael@0 | 116 | } |
michael@0 | 117 |