-
-
Notifications
You must be signed in to change notification settings - Fork 185
Expand file tree
/
Copy pathPromise.ts
More file actions
239 lines (209 loc) · 9.8 KB
/
Promise.ts
File metadata and controls
239 lines (209 loc) · 9.8 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
/* eslint-disable @typescript-eslint/promise-function-async */
// Promises implemented based on https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise
// and https://promisesaplus.com/
export const enum PromiseState {
Pending,
Fulfilled,
Rejected,
}
type PromiseExecutor<T> = ConstructorParameters<typeof Promise<T>>[0];
type PromiseResolve<T> = Parameters<PromiseExecutor<T>>[0];
type PromiseReject = Parameters<PromiseExecutor<unknown>>[1];
type PromiseResolveCallback<TValue, TResult> = (value: TValue) => TResult | PromiseLike<TResult>;
type PromiseRejectCallback<TResult> = (reason: any) => TResult | PromiseLike<TResult>;
function makeDeferredPromiseFactory(this: void) {
let resolve: PromiseResolve<any>;
let reject: PromiseReject;
const executor: PromiseExecutor<any> = (res, rej) => {
resolve = res;
reject = rej;
};
return function <T>(this: void) {
const promise = new Promise<T>(executor);
return $multi(promise, resolve, reject);
};
}
const makeDeferredPromise = makeDeferredPromiseFactory();
function isPromiseLike<T>(this: void, value: unknown): value is PromiseLike<T> {
return value instanceof __TS__Promise;
}
function doNothing(): void {}
const pcall = _G.pcall;
export class __TS__Promise<T> implements Promise<T> {
public state = PromiseState.Pending;
public value?: T;
public rejectionReason?: any;
private fulfilledCallbacks: Array<PromiseResolve<T>> = [];
private rejectedCallbacks: PromiseReject[] = [];
// @ts-ignore
public [Symbol.toStringTag]: string; // Required to implement interface, no output Lua
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/resolve
public static resolve<T>(this: void, value: T | PromiseLike<T>): __TS__Promise<Awaited<T>> {
if (value instanceof __TS__Promise) {
return value;
}
// Create and return a promise instance that is already resolved
const promise = new __TS__Promise<Awaited<T>>(doNothing);
promise.state = PromiseState.Fulfilled;
promise.value = value as Awaited<T>;
return promise;
}
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/reject
public static reject<T = never>(this: void, reason?: any): __TS__Promise<T> {
// Create and return a promise instance that is already rejected
const promise = new __TS__Promise<T>(doNothing);
promise.state = PromiseState.Rejected;
promise.rejectionReason = reason;
return promise;
}
constructor(executor: PromiseExecutor<T>) {
// Avoid unnecessary local functions allocations by using `pcall` explicitly
const [success, error] = pcall(
executor,
undefined,
v => this.resolve(v),
err => this.reject(err)
);
if (!success) {
// When a promise executor throws, the promise should be rejected with the thrown object as reason
this.reject(error);
}
}
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/then
public then<TResult1 = T, TResult2 = never>(
onFulfilled?: PromiseResolveCallback<T, TResult1>,
onRejected?: PromiseRejectCallback<TResult2>
): Promise<TResult1 | TResult2> {
const [promise, resolve, reject] = makeDeferredPromise<T | TResult1 | TResult2>();
this.addCallbacks(
// We always want to resolve our child promise if this promise is resolved, even if we have no handler
onFulfilled ? this.createPromiseResolvingCallback(onFulfilled, resolve, reject) : resolve,
// We always want to reject our child promise if this promise is rejected, even if we have no handler
onRejected ? this.createPromiseResolvingCallback(onRejected, resolve, reject) : reject
);
return promise as Promise<TResult1 | TResult2>;
}
// Both callbacks should never throw!
public addCallbacks(fulfilledCallback: (value: T) => void, rejectedCallback: (rejectionReason: any) => void): void {
if (this.state === PromiseState.Fulfilled) {
// If promise already resolved, immediately call callback. We don't even need to store rejected callback
// Tail call return is important!
return fulfilledCallback(this.value!);
}
if (this.state === PromiseState.Rejected) {
// Similar thing
return rejectedCallback(this.rejectionReason);
}
this.fulfilledCallbacks.push(fulfilledCallback as any);
this.rejectedCallbacks.push(rejectedCallback);
}
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/catch
public catch<TResult = never>(onRejected?: (reason: any) => TResult | PromiseLike<TResult>): Promise<T | TResult> {
return this.then(undefined, onRejected);
}
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/finally
// Delegates to .then() so that a new Promise is returned (per ES spec §27.2.5.3)
// and the original fulfillment value / rejection reason is preserved.
public finally(onFinally?: () => void): Promise<T> {
return this.then(
onFinally
? (value: T): T => {
onFinally();
return value;
}
: undefined,
onFinally
? (reason: any): never => {
onFinally();
throw reason;
}
: undefined
);
}
private resolve(value: T | PromiseLike<T>): void {
if (isPromiseLike(value)) {
// Tail call return is important!
return (value as __TS__Promise<T>).addCallbacks(
v => this.resolve(v),
err => this.reject(err)
);
}
// Resolve this promise, if it is still pending. This function is passed to the constructor function.
if (this.state === PromiseState.Pending) {
this.state = PromiseState.Fulfilled;
this.value = value;
// Tail call return is important!
return this.invokeCallbacks(this.fulfilledCallbacks, value);
}
}
private reject(reason: any): void {
// Reject this promise, if it is still pending. This function is passed to the constructor function.
if (this.state === PromiseState.Pending) {
this.state = PromiseState.Rejected;
this.rejectionReason = reason;
// Tail call return is important!
return this.invokeCallbacks(this.rejectedCallbacks, reason);
}
}
private invokeCallbacks<T>(callbacks: ReadonlyArray<(value: T) => void>, value: T): void {
const callbacksLength = callbacks.length;
if (callbacksLength !== 0) {
for (const i of $range(1, callbacksLength - 1)) {
callbacks[i - 1](value);
}
// Tail call optimization for a common case.
return callbacks[callbacksLength - 1](value);
}
}
private createPromiseResolvingCallback<TResult1, TResult2>(
f: PromiseResolveCallback<T, TResult1> | PromiseRejectCallback<TResult2>,
resolve: (data: TResult1 | TResult2) => void,
reject: (reason: any) => void
) {
return (value: T): void => {
const [success, resultOrError] = pcall<
undefined,
[T],
TResult1 | PromiseLike<TResult1> | TResult2 | PromiseLike<TResult2>
>(f, undefined, value);
if (!success) {
// Tail call return is important!
return reject(resultOrError);
}
// Tail call return is important!
return this.handleCallbackValue(resultOrError, resolve, reject);
};
}
private handleCallbackValue<TResult1, TResult2, TResult extends TResult1 | TResult2>(
value: TResult | PromiseLike<TResult>,
resolve: (data: TResult1 | TResult2) => void,
reject: (reason: any) => void
): void {
if (isPromiseLike<TResult>(value)) {
const nextpromise = value as __TS__Promise<TResult>;
if (nextpromise.state === PromiseState.Fulfilled) {
// If a handler function returns an already fulfilled promise,
// the promise returned by then gets fulfilled with that promise's value.
// Tail call return is important!
return resolve(nextpromise.value!);
} else if (nextpromise.state === PromiseState.Rejected) {
// If a handler function returns an already rejected promise,
// the promise returned by then gets fulfilled with that promise's value.
// Tail call return is important!
return reject(nextpromise.rejectionReason);
} else {
// If a handler function returns another pending promise object, the resolution/rejection
// of the promise returned by then will be subsequent to the resolution/rejection of
// the promise returned by the handler.
// We cannot use `then` because we need to do tail call, and `then` returns a Promise.
// `resolve` and `reject` should never throw.
return nextpromise.addCallbacks(resolve, reject);
}
} else {
// If a handler returns a value, the promise returned by then gets resolved with the returned value as its value
// If a handler doesn't return anything, the promise returned by then gets resolved with undefined
// Tail call return is important!
return resolve(value);
}
}
}