-
-
Notifications
You must be signed in to change notification settings - Fork 185
Expand file tree
/
Copy pathPromise.ts
More file actions
215 lines (184 loc) · 8.39 KB
/
Promise.ts
File metadata and controls
215 lines (184 loc) · 8.39 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
/* 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 FulfillCallback<TData, TResult> = (value: TData) => TResult | PromiseLike<TResult>;
type RejectCallback<TResult> = (reason: any) => TResult | PromiseLike<TResult>;
function promiseDeferred<T>() {
let resolve: FulfillCallback<T, unknown>;
let reject: RejectCallback<unknown>;
const promise = new Promise<T>((res, rej) => {
resolve = res;
reject = rej;
});
return { promise, resolve, reject };
}
function isPromiseLike<T>(thing: unknown): thing is PromiseLike<T> {
return thing instanceof __TS__Promise;
}
export class __TS__Promise<T> implements Promise<T> {
public state = PromiseState.Pending;
public value?: T;
public rejectionReason?: any;
private fulfilledCallbacks: Array<FulfillCallback<T, unknown>> = [];
private rejectedCallbacks: Array<RejectCallback<unknown>> = [];
private finallyCallbacks: Array<() => void> = [];
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<TData>(this: void, data: TData): Promise<TData> {
// Create and return a promise instance that is already resolved
const promise = new __TS__Promise<TData>(() => {});
promise.state = PromiseState.Fulfilled;
promise.value = data;
return promise;
}
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/reject
public static reject(this: void, reason: any): Promise<never> {
// Create and return a promise instance that is already rejected
const promise = new __TS__Promise<never>(() => {});
promise.state = PromiseState.Rejected;
promise.rejectionReason = reason;
return promise;
}
constructor(executor: (resolve: (data: T) => void, reject: (reason: any) => void) => void) {
try {
executor(this.resolve.bind(this), this.reject.bind(this));
} catch (e) {
// When a promise executor throws, the promise should be rejected with the thrown object as reason
this.reject(e);
}
}
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/then
public then<TResult1 = T, TResult2 = never>(
onFulfilled?: FulfillCallback<T, TResult1>,
onRejected?: RejectCallback<TResult2>
): Promise<TResult1 | TResult2> {
const { promise, resolve, reject } = promiseDeferred<T | TResult1 | TResult2>();
const isFulfilled = this.state === PromiseState.Fulfilled;
const isRejected = this.state === PromiseState.Rejected;
if (onFulfilled) {
const internalCallback = this.createPromiseResolvingCallback(onFulfilled, resolve, reject);
this.fulfilledCallbacks.push(internalCallback);
if (isFulfilled) {
// If promise already resolved, immediately call callback
internalCallback(this.value);
}
} else {
// We always want to resolve our child promise if this promise is resolved, even if we have no handler
this.fulfilledCallbacks.push(v => resolve(v));
}
if (onRejected) {
const internalCallback = this.createPromiseResolvingCallback(onRejected, resolve, reject);
this.rejectedCallbacks.push(internalCallback);
if (isRejected) {
// If promise already rejected, immediately call callback
internalCallback(this.rejectionReason);
}
} else {
// We always want to reject our child promise if this promise is rejected, even if we have no handler
this.rejectedCallbacks.push(err => reject(err));
}
if (isFulfilled) {
// If promise already resolved, also resolve returned promise
resolve(this.value);
}
if (isRejected) {
// If promise already rejected, also reject returned promise
reject(this.rejectionReason);
}
return promise as Promise<TResult1 | TResult2>;
}
// 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
public finally(onFinally?: () => void): Promise<T> {
if (onFinally) {
this.finallyCallbacks.push(onFinally);
if (this.state !== PromiseState.Pending) {
// If promise already resolved or rejected, immediately fire finally callback
onFinally();
}
}
return this;
}
private resolve(data: T): void {
if (data instanceof __TS__Promise) {
data.then(
v => this.resolve(v),
err => this.reject(err)
);
return;
}
// 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 = data;
for (const callback of this.fulfilledCallbacks) {
callback(data);
}
for (const callback of this.finallyCallbacks) {
callback();
}
}
}
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;
for (const callback of this.rejectedCallbacks) {
callback(reason);
}
for (const callback of this.finallyCallbacks) {
callback();
}
}
}
private createPromiseResolvingCallback<TResult1, TResult2>(
f: FulfillCallback<T, TResult1> | RejectCallback<TResult2>,
resolve: FulfillCallback<TResult1 | TResult2, unknown>,
reject: RejectCallback<unknown>
) {
return value => {
try {
this.handleCallbackData(f(value), resolve, reject);
} catch (e) {
// If a handler function throws an error, the promise returned by then gets rejected with the thrown error as its value
reject(e);
}
};
}
private handleCallbackData<TResult1, TResult2, TResult extends TResult1 | TResult2>(
data: TResult | PromiseLike<TResult>,
resolve: FulfillCallback<TResult1 | TResult2, unknown>,
reject: RejectCallback<unknown>
) {
if (isPromiseLike<TResult>(data)) {
const nextpromise = data 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
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
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.
data.then(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
resolve(data);
}
}
}