-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpromise.ts
404 lines (393 loc) · 12.8 KB
/
promise.ts
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
import { isArray } from './alias';
import { noop } from './function';
const enum State {
pending,
resolved,
fulfilled,
rejected,
}
type Status<T> =
| { readonly state: State.pending;
}
| { readonly state: State.resolved;
readonly promise: PromiseLike<T>;
}
| { readonly state: State.fulfilled;
readonly value: T;
}
| { readonly state: State.rejected;
readonly reason: unknown;
};
export const internal = Symbol.for('spica/promise::internal');
interface AtomicPromiseLike<T> {
readonly [internal]: Internal<T>;
then<TResult1 = T, TResult2 = never>(onfulfilled?: ((value: T) => TResult1 | PromiseLike<TResult1>) | undefined | null, onrejected?: ((reason: unknown) => TResult2 | PromiseLike<TResult2>) | undefined | null): AtomicPromise<TResult1 | TResult2>;
}
export class AtomicPromise<T = undefined> implements Promise<T>, AtomicPromiseLike<T> {
public readonly [Symbol.toStringTag]: string = 'Promise';
public static get [Symbol.species]() {
return AtomicPromise;
}
public static all<T extends readonly unknown[] | []>(values: T): AtomicPromise<{ -readonly [P in keyof T]: Awaited<T[P]> }>;
public static all<T>(values: Iterable<T | PromiseLike<T>>): AtomicPromise<Awaited<T>[]>;
public static all<T>(vs: Iterable<T | PromiseLike<T> | AtomicPromiseLike<T>>): AtomicPromise<T[]> {
return new AtomicPromise<T[]>((resolve, reject) => {
const values = isArray(vs) ? vs : [...vs];
const results: T[] = Array(values.length);
let done = false;
let count = 0;
for (let i = 0; !done && i < values.length; ++i) {
const value = values[i];
if (!isPromiseLike(value)) {
results[i] = value;
++count;
continue;
}
if (isAtomicPromiseLike(value)) {
const { status } = value[internal];
switch (status.state) {
case State.fulfilled:
results[i] = status.value;
++count;
continue;
case State.rejected:
return reject(status.reason);
}
}
value.then(
value => {
results[i] = value;
++count;
count === values.length && resolve(results);
},
reason => {
reject(reason);
done = true;
});
}
count === values.length && resolve(results);
});
}
public static race<T extends readonly unknown[] | []>(values: T): AtomicPromise<Awaited<T[number]>>;
public static race<T>(values: Iterable<T | PromiseLike<T>>): AtomicPromise<Awaited<T>>;
public static race<T>(vs: Iterable<T | PromiseLike<T> | AtomicPromiseLike<T>>): AtomicPromise<T> {
return new AtomicPromise<T>((resolve, reject) => {
const values = isArray(vs) ? vs : [...vs];
for (let i = 0; i < values.length; ++i) {
const value = values[i];
if (!isPromiseLike(value)) {
return resolve(value);
}
if (isAtomicPromiseLike(value)) {
const { status } = value[internal];
switch (status.state) {
case State.fulfilled:
return resolve(status.value);
case State.rejected:
return reject(status.reason);
}
}
}
let done = false;
for (let i = 0; !done && i < values.length; ++i) {
const value = values[i] as PromiseLike<T>;
value.then(
value => {
resolve(value);
done = true;
},
reason => {
reject(reason);
done = true;
});
}
});
}
public static allSettled<T extends readonly unknown[] | []>(values: T): AtomicPromise<{ -readonly [P in keyof T]: PromiseSettledResult<Awaited<T[P]>> }>;
public static allSettled<T>(values: Iterable<T>): AtomicPromise<PromiseSettledResult<Awaited<T>>[]>;
public static allSettled<T>(vs: Iterable<T>): AtomicPromise<unknown> {
return new AtomicPromise<PromiseSettledResult<T extends PromiseLike<infer U> ? U : T>[]>(resolve => {
const values = isArray(vs) ? vs : [...vs];
const results: PromiseSettledResult<T extends PromiseLike<infer U> ? U : T>[] = Array(values.length);
let count = 0;
for (let i = 0; i < values.length; ++i) {
const value = values[i];
if (!isPromiseLike(value)) {
results[i] = {
status: 'fulfilled',
value: value as any,
};
++count;
continue;
}
if (isAtomicPromiseLike(value)) {
const { status } = value[internal];
switch (status.state) {
case State.fulfilled:
results[i] = {
status: 'fulfilled',
value: status.value,
};
++count;
continue;
case State.rejected:
results[i] = {
status: 'rejected',
reason: status.reason,
};
++count;
continue;
}
}
value.then(
value => {
results[i] = {
status: 'fulfilled',
value: value,
};
++count;
count === values.length && resolve(results);
},
reason => {
results[i] = {
status: 'rejected',
reason,
};
++count;
count === values.length && resolve(results);
});
}
count === values.length && resolve(results);
});
}
public static any<T extends readonly unknown[] | []>(values: T): AtomicPromise<Awaited<T[number]>>;
public static any<T>(values: Iterable<T | PromiseLike<T>>): AtomicPromise<Awaited<T>>;
public static any<T>(vs: Iterable<T | PromiseLike<T>>): AtomicPromise<T> {
return new AtomicPromise<T>((resolve, reject) => {
const values = isArray(vs) ? vs : [...vs];
const reasons: unknown[] = Array(values.length);
let done = false;
let count = 0;
for (let i = 0; !done && i < values.length; ++i) {
const value = values[i];
if (!isPromiseLike(value)) {
return resolve(value);
}
if (isAtomicPromiseLike(value)) {
const { status } = value[internal];
switch (status.state) {
case State.fulfilled:
return resolve(status.value);
case State.rejected:
reasons[i] = status.reason;
++count;
continue;
}
}
value.then(
value => {
resolve(value);
done = true;
},
reason => {
reasons[i] = reason;
++count;
count === values.length && reject(new AggregateError(reasons, 'All promises were rejected'));
});
}
count === values.length && reject(new AggregateError(reasons, 'All promises were rejected'));
});
}
public static resolve(): AtomicPromise<undefined>;
public static resolve<T>(value: T | PromiseLike<T>): AtomicPromise<T>;
public static resolve<T>(value?: T | PromiseLike<T>): AtomicPromise<T> {
const p = new AtomicPromise<T>(noop);
p[internal].resolve(value!);
return p;
}
public static reject<T = never>(reason?: unknown): AtomicPromise<T> {
const p = new AtomicPromise<T>(noop);
p[internal].reject(reason);
return p;
}
constructor(
executor: (resolve: (value: T | PromiseLike<T>) => void, reject: (reason?: unknown) => void) => void,
) {
if (executor === noop) return;
try {
executor(
value => void this[internal].resolve(value),
reason => void this[internal].reject(reason));
}
catch (reason) {
this[internal].reject(reason);
}
}
public readonly [internal]: Internal<T> = new Internal();
public then<TResult1 = T, TResult2 = never>(onfulfilled?: ((value: T) => TResult1 | PromiseLike<TResult1>) | undefined | null, onrejected?: ((reason: unknown) => TResult2 | PromiseLike<TResult2>) | undefined | null): AtomicPromise<TResult1 | TResult2> {
const p = new AtomicPromise<TResult1 | TResult2>(noop);
this[internal].then(p[internal], onfulfilled, onrejected);
return p;
}
public catch<TResult = never>(onrejected?: ((reason: unknown) => TResult | PromiseLike<TResult>) | undefined | null): AtomicPromise<T | TResult> {
return this.then(undefined, onrejected);
}
public finally(onfinally?: (() => void) | undefined | null): AtomicPromise<T> {
return this.then(onfinally, onfinally).then(() => this);
}
}
interface FulfillReaction {
readonly internal: Internal<unknown>;
readonly state: true;
readonly procedure: ((param: unknown) => unknown) | undefined | null;
}
interface RejectReaction {
readonly internal: Internal<unknown>;
readonly state: false;
readonly procedure: ((param: unknown) => unknown) | undefined | null;
}
export class Internal<T> {
public status: Status<T> = { state: State.pending };
public isPending(): boolean {
return this.status.state === State.pending;
}
public resolve(value: T | PromiseLike<T>): void {
if (!this.isPending()) return;
if (!isPromiseLike(value)) {
this.status = {
state: State.fulfilled,
value: value,
};
return this.resume();
}
if (isAtomicPromiseLike(value)) {
return value[internal].then(this);
}
this.status = {
state: State.resolved,
promise: value,
};
return void value.then(
value => {
assert(this.status.state === State.resolved);
this.status = {
state: State.fulfilled,
value,
};
this.resume();
},
reason => {
assert(this.status.state === State.resolved);
this.status = {
state: State.rejected,
reason,
};
this.resume();
});
}
public reject(reason: unknown): void {
if (!this.isPending()) return;
this.status = {
state: State.rejected,
reason,
};
return this.resume();
}
public fulfillReactions: FulfillReaction[] = [];
public rejectReactions: RejectReaction[] = [];
public then<TResult1, TResult2>(
internal: Internal<TResult1 | TResult2>,
onfulfilled?: ((value: T) => TResult1 | PromiseLike<TResult1>) | undefined | null,
onrejected?: ((reason: unknown) => TResult2 | PromiseLike<TResult2>) | undefined | null,
): void {
assert(internal);
const { status, fulfillReactions, rejectReactions } = this;
switch (status.state) {
case State.fulfilled:
if (fulfillReactions.length !== 0) break;
return call(internal, true, onfulfilled, status.value);
case State.rejected:
if (rejectReactions.length !== 0) break;
return call(internal, false, onrejected, status.reason);
}
fulfillReactions.push({
internal,
state: true,
procedure: onfulfilled,
});
rejectReactions.push({
internal,
state: false,
procedure: onrejected,
});
}
public resume(): void {
const { status, fulfillReactions, rejectReactions } = this;
switch (status.state) {
case State.pending:
case State.resolved:
return;
case State.fulfilled:
if (rejectReactions.length !== 0) {
this.rejectReactions = [];
}
if (fulfillReactions.length === 0) return;
react(fulfillReactions, status.value);
this.fulfillReactions = [];
return;
case State.rejected:
if (fulfillReactions.length !== 0) {
this.fulfillReactions = [];
}
if (rejectReactions.length === 0) return;
react(rejectReactions, status.reason);
this.rejectReactions = [];
return;
}
}
}
function react(reactions: readonly (FulfillReaction | RejectReaction)[], param: unknown): void {
for (let i = 0; i < reactions.length; ++i) {
const { internal, state, procedure } = reactions[i];
call(internal, state, procedure, param);
}
}
function call(
internal: Internal<unknown>,
state: boolean,
procedure: ((param: unknown) => unknown) | undefined | null,
param: unknown,
): void {
if (procedure == null) return state ? internal.resolve(param) : internal.reject(param);
try {
internal.resolve(procedure(param));
}
catch (reason) {
internal.reject(reason);
}
}
export function isPromiseLike(value: any): value is PromiseLike<any> {
return value != null && typeof value === 'object'
&& 'then' in value && typeof value.then === 'function';
}
function isAtomicPromiseLike(value: any): value is AtomicPromiseLike<any> {
assert(isPromiseLike(value));
return internal in value;
}
export const never: Promise<never> = new class Never extends Promise<never> {
public static override get [Symbol.species]() {
return Never;
}
constructor() {
super(noop);
}
public override then() {
return this;
}
public override catch() {
return this;
}
public override finally() {
return this;
}
}();