-internal.js
5.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
import originalThen from './then';
import originalResolve from './promise/resolve';
import instrument from './instrument';
import { config } from './config';
import Promise from './promise';
function withOwnPromise() {
return new TypeError('A promises callback cannot return that same promise.');
}
function objectOrFunction(x) {
let type = typeof x;
return x !== null && (type === 'object' || type === 'function');
}
export function noop() {}
export const PENDING = void 0;
export const FULFILLED = 1;
export const REJECTED = 2;
export const TRY_CATCH_ERROR = { error: null };
export function getThen(promise) {
try {
return promise.then;
} catch(error) {
TRY_CATCH_ERROR.error = error;
return TRY_CATCH_ERROR;
}
}
let tryCatchCallback;
function tryCatcher() {
try {
let target = tryCatchCallback;
tryCatchCallback = null;
return target.apply(this, arguments);
} catch(e) {
TRY_CATCH_ERROR.error = e;
return TRY_CATCH_ERROR;
}
}
export function tryCatch(fn) {
tryCatchCallback = fn;
return tryCatcher;
}
function handleForeignThenable(promise, thenable, then) {
config.async(promise => {
let sealed = false;
let result = tryCatch(then).call(
thenable,
value => {
if (sealed) { return; }
sealed = true;
if (thenable === value) {
fulfill(promise, value);
} else {
resolve(promise, value);
}
},
reason => {
if (sealed) { return; }
sealed = true;
reject(promise, reason);
},
'Settle: ' + (promise._label || ' unknown promise')
);
if (!sealed && result === TRY_CATCH_ERROR) {
sealed = true;
let error = TRY_CATCH_ERROR.error;
TRY_CATCH_ERROR.error = null;
reject(promise, error);
}
}, promise);
}
function handleOwnThenable(promise, thenable) {
if (thenable._state === FULFILLED) {
fulfill(promise, thenable._result);
} else if (thenable._state === REJECTED) {
thenable._onError = null;
reject(promise, thenable._result);
} else {
subscribe(thenable, undefined, value => {
if (thenable === value) {
fulfill(promise, value);
} else {
resolve(promise, value);
}
}, reason => reject(promise, reason));
}
}
export function handleMaybeThenable(promise, maybeThenable, then) {
let isOwnThenable =
maybeThenable.constructor === promise.constructor &&
then === originalThen &&
promise.constructor.resolve === originalResolve;
if (isOwnThenable) {
handleOwnThenable(promise, maybeThenable);
} else if (then === TRY_CATCH_ERROR) {
let error = TRY_CATCH_ERROR.error
TRY_CATCH_ERROR.error = null;
reject(promise, error);
} else if (typeof then === 'function') {
handleForeignThenable(promise, maybeThenable, then);
} else {
fulfill(promise, maybeThenable);
}
}
export function resolve(promise, value) {
if (promise === value) {
fulfill(promise, value);
} else if (objectOrFunction(value)) {
handleMaybeThenable(promise, value, getThen(value));
} else {
fulfill(promise, value);
}
}
export function publishRejection(promise) {
if (promise._onError) {
promise._onError(promise._result);
}
publish(promise);
}
export function fulfill(promise, value) {
if (promise._state !== PENDING) { return; }
promise._result = value;
promise._state = FULFILLED;
if (promise._subscribers.length === 0) {
if (config.instrument) {
instrument('fulfilled', promise);
}
} else {
config.async(publish, promise);
}
}
export function reject(promise, reason) {
if (promise._state !== PENDING) { return; }
promise._state = REJECTED;
promise._result = reason;
config.async(publishRejection, promise);
}
export function subscribe(parent, child, onFulfillment, onRejection) {
let subscribers = parent._subscribers;
let length = subscribers.length;
parent._onError = null;
subscribers[length] = child;
subscribers[length + FULFILLED] = onFulfillment;
subscribers[length + REJECTED] = onRejection;
if (length === 0 && parent._state) {
config.async(publish, parent);
}
}
export function publish(promise) {
let subscribers = promise._subscribers;
let settled = promise._state;
if (config.instrument) {
instrument(settled === FULFILLED ? 'fulfilled' : 'rejected', promise);
}
if (subscribers.length === 0) { return; }
let child, callback, result = promise._result;
for (let i = 0; i < subscribers.length; i += 3) {
child = subscribers[i];
callback = subscribers[i + settled];
if (child) {
invokeCallback(settled, child, callback, result);
} else {
callback(result);
}
}
promise._subscribers.length = 0;
}
export function invokeCallback(state, promise, callback, result) {
let hasCallback = typeof callback === 'function';
let value;
if (hasCallback) {
value = tryCatch(callback)(result);
} else {
value = result;
}
if (promise._state !== PENDING) {
// noop
} else if (value === promise) {
reject(promise, withOwnPromise());
} else if (value === TRY_CATCH_ERROR) {
let error = TRY_CATCH_ERROR.error;
TRY_CATCH_ERROR.error = null; // release
reject(promise, error);
} else if (hasCallback) {
resolve(promise, value);
} else if (state === FULFILLED) {
fulfill(promise, value);
} else if (state === REJECTED) {
reject(promise, value);
}
}
export function initializePromise(promise, resolver) {
let resolved = false;
try {
resolver(value => {
if (resolved) { return; }
resolved = true;
resolve(promise, value);
}, reason => {
if (resolved) { return; }
resolved = true;
reject(promise, reason);
});
} catch(e) {
reject(promise, e);
}
}