Left: | ||
Right: |
OLD | NEW |
---|---|
(Empty) | |
1 /* | |
2 * This file is part of Adblock Plus <https://adblockplus.org/>, | |
3 * Copyright (C) 2006-2015 Eyeo GmbH | |
4 * | |
5 * Adblock Plus is free software: you can redistribute it and/or modify | |
6 * it under the terms of the GNU General Public License version 3 as | |
7 * published by the Free Software Foundation. | |
8 * | |
9 * Adblock Plus is distributed in the hope that it will be useful, | |
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 * GNU General Public License for more details. | |
13 * | |
14 * You should have received a copy of the GNU General Public License | |
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. | |
16 */ | |
17 | |
18 "use strict"; | |
Sebastian Noack
2016/01/15 15:37:06
For reference, I discussed strict mode with Wladim
kzar
2016/01/15 19:00:42
Acknowledged.
kzar
2016/01/15 19:03:55
Shouldn't "use strict" be inside of the IIFE, othe
Sebastian Noack
2016/01/19 11:34:22
Putting "use script" on the top-level will turn on
kzar
2016/01/19 14:49:27
Acknowledged.
| |
19 | |
20 (function(global) | |
21 { | |
22 if ("Promise" in global) | |
23 return; | |
24 | |
25 var PENDING = 0; | |
26 var FULFILLED = 1; | |
27 var REJECTED = 2; | |
28 | |
29 global.Promise = function(executor) | |
30 { | |
31 this._state = PENDING; | |
32 this._value = undefined; | |
33 this._subscriptions = []; | |
34 | |
35 try | |
36 { | |
37 executor(this._emit.bind(this, FULFILLED), | |
38 this._emit.bind(this, REJECTED)); | |
39 } | |
40 catch (reason) | |
41 { | |
42 this._emit(REJECTED, reason); | |
43 } | |
44 }; | |
45 | |
46 Promise.prototype = { | |
47 _dispatch: function(onFulfilled, onRejected, resolve, reject) | |
48 { | |
49 var callback = this._state == FULFILLED ? onFulfilled : onRejected; | |
50 | |
51 if (typeof callback == "function") | |
52 { | |
53 var result; | |
54 | |
55 try | |
56 { | |
57 result = callback(this._value); | |
58 } | |
59 catch (reason) | |
60 { | |
61 reject(reason); | |
62 return; | |
63 } | |
64 | |
65 Promise.resolve(result).then(resolve, reject); | |
66 } | |
67 else if (this._state == FULFILLED) | |
68 { | |
69 resolve(this._value); | |
70 } | |
71 else if (this._state == REJECTED) | |
72 { | |
73 reject(this._value); | |
74 } | |
75 }, | |
76 _dispatchSubscriptions: function() | |
77 { | |
78 if (this._state == REJECTED && this._subscriptions.length == 0) | |
79 console.error('Uncaught (in promise)', this._value); | |
80 | |
81 for (var i = 0; i < this._subscriptions.length; i++) | |
82 this._dispatch.apply(this, this._subscriptions[i]); | |
83 | |
84 this._subscriptions = null; | |
85 }, | |
86 _emit: function(state, value) | |
87 { | |
88 if (this._state != PENDING) | |
89 return; | |
90 | |
91 this._state = state; | |
92 this._value = value; | |
93 | |
94 setTimeout(this._dispatchSubscriptions.bind(this), 0); | |
95 }, | |
96 then: function(onFulfilled, onRejected) | |
97 { | |
98 return new Promise(function(resolve, reject) | |
99 { | |
100 if (this._subscriptions) | |
101 this._subscriptions.push([onFulfilled, onRejected, resolve, reject]); | |
102 else | |
103 setTimeout( | |
104 this._dispatch.bind(this), 0, | |
105 onFulfilled, onRejected, resolve, reject | |
106 ); | |
107 }.bind(this)); | |
108 }, | |
109 catch: function(onRejected) | |
110 { | |
111 return this.then(undefined, onRejected); | |
112 } | |
113 }; | |
114 | |
115 Promise.resolve = function(value) | |
116 { | |
117 if (value instanceof Promise) | |
118 return value; | |
119 return new Promise(function(resolve, reject) { resolve(value); }); | |
120 }; | |
121 | |
122 Promise.reject = function(reason) | |
123 { | |
124 return new Promise(function(resolve, reject) { reject(reason); }); | |
125 }; | |
126 | |
127 Promise.all = function(promises) | |
128 { | |
129 return new Promise(function(resolve, reject) | |
130 { | |
131 var count = promises.length; | |
132 var result = new Array(count); | |
133 | |
134 if (count == 0) | |
135 { | |
136 resolve(result); | |
137 return; | |
138 } | |
139 | |
140 promises.forEach(function(promise, i) | |
141 { | |
142 Promise.resolve(promise).then( | |
143 function(value) | |
144 { | |
145 result[i] = value; | |
146 if (--count == 0) | |
147 resolve(result); | |
148 }, | |
149 reject | |
150 ); | |
151 }); | |
152 }); | |
153 }; | |
154 })(this); | |
OLD | NEW |