|
| 1 | +/* eslint-disable @typescript-eslint/promise-function-async */ |
| 2 | + |
| 3 | +// Promises implemented based on https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise |
| 4 | +// and https://promisesaplus.com/ |
| 5 | + |
| 6 | +enum __TS__PromiseState { |
| 7 | + Pending, |
| 8 | + Fulfilled, |
| 9 | + Rejected, |
| 10 | +} |
| 11 | + |
| 12 | +type FulfillCallback<TData, TResult> = (value: TData) => TResult | PromiseLike<TResult>; |
| 13 | +type RejectCallback<TResult> = (reason: any) => TResult | PromiseLike<TResult>; |
| 14 | + |
| 15 | +function __TS__PromiseDeferred<T>() { |
| 16 | + let resolve: FulfillCallback<T, unknown>; |
| 17 | + let reject: RejectCallback<unknown>; |
| 18 | + const promise = new Promise<T>((res, rej) => { |
| 19 | + resolve = res; |
| 20 | + reject = rej; |
| 21 | + }); |
| 22 | + |
| 23 | + return { promise, resolve, reject }; |
| 24 | +} |
| 25 | + |
| 26 | +function __TS__IsPromiseLike<T>(thing: unknown): thing is PromiseLike<T> { |
| 27 | + return thing instanceof __TS__Promise; |
| 28 | +} |
| 29 | + |
| 30 | +class __TS__Promise<T> implements Promise<T> { |
| 31 | + public state = __TS__PromiseState.Pending; |
| 32 | + public value?: T; |
| 33 | + public rejectionReason?: any; |
| 34 | + |
| 35 | + private fulfilledCallbacks: Array<FulfillCallback<T, unknown>> = []; |
| 36 | + private rejectedCallbacks: Array<RejectCallback<unknown>> = []; |
| 37 | + private finallyCallbacks: Array<() => void> = []; |
| 38 | + |
| 39 | + public [Symbol.toStringTag]: string; // Required to implement interface, no output Lua |
| 40 | + |
| 41 | + // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/resolve |
| 42 | + public static resolve<TData>(this: void, data: TData): Promise<TData> { |
| 43 | + // Create and return a promise instance that is already resolved |
| 44 | + const promise = new __TS__Promise<TData>(() => {}); |
| 45 | + promise.state = __TS__PromiseState.Fulfilled; |
| 46 | + promise.value = data; |
| 47 | + return promise; |
| 48 | + } |
| 49 | + |
| 50 | + // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/reject |
| 51 | + public static reject(this: void, reason: any): Promise<never> { |
| 52 | + // Create and return a promise instance that is already rejected |
| 53 | + const promise = new __TS__Promise<never>(() => {}); |
| 54 | + promise.state = __TS__PromiseState.Rejected; |
| 55 | + promise.rejectionReason = reason; |
| 56 | + return promise; |
| 57 | + } |
| 58 | + |
| 59 | + constructor(executor: (resolve: (data: T) => void, reject: (reason: any) => void) => void) { |
| 60 | + try { |
| 61 | + executor(this.resolve.bind(this), this.reject.bind(this)); |
| 62 | + } catch (e) { |
| 63 | + // When a promise executor throws, the promise should be rejected with the thrown object as reason |
| 64 | + this.reject(e); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/then |
| 69 | + public then<TResult1 = T, TResult2 = never>( |
| 70 | + onFulfilled?: FulfillCallback<T, TResult1>, |
| 71 | + onRejected?: RejectCallback<TResult2> |
| 72 | + ): Promise<TResult1 | TResult2> { |
| 73 | + const { promise, resolve, reject } = __TS__PromiseDeferred<TResult1 | TResult2>(); |
| 74 | + |
| 75 | + if (onFulfilled) { |
| 76 | + const internalCallback = this.createPromiseResolvingCallback(onFulfilled, resolve, reject); |
| 77 | + this.fulfilledCallbacks.push(internalCallback); |
| 78 | + |
| 79 | + if (this.state === __TS__PromiseState.Fulfilled) { |
| 80 | + // If promise already resolved, immediately call callback |
| 81 | + internalCallback(this.value); |
| 82 | + } |
| 83 | + } else { |
| 84 | + // We always want to resolve our child promise if this promise is resolved, even if we have no handler |
| 85 | + this.fulfilledCallbacks.push(() => resolve(undefined)); |
| 86 | + } |
| 87 | + |
| 88 | + if (onRejected) { |
| 89 | + const internalCallback = this.createPromiseResolvingCallback(onRejected, resolve, reject); |
| 90 | + this.rejectedCallbacks.push(internalCallback); |
| 91 | + |
| 92 | + if (this.state === __TS__PromiseState.Rejected) { |
| 93 | + // If promise already rejected, immediately call callback |
| 94 | + internalCallback(this.rejectionReason); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + return promise; |
| 99 | + } |
| 100 | + // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/catch |
| 101 | + public catch<TResult = never>(onRejected?: (reason: any) => TResult | PromiseLike<TResult>): Promise<T | TResult> { |
| 102 | + return this.then(undefined, onRejected); |
| 103 | + } |
| 104 | + // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/finally |
| 105 | + public finally(onFinally?: () => void): Promise<T> { |
| 106 | + if (onFinally) { |
| 107 | + this.finallyCallbacks.push(onFinally); |
| 108 | + |
| 109 | + if (this.state !== __TS__PromiseState.Pending) { |
| 110 | + // If promise already resolved or rejected, immediately fire finally callback |
| 111 | + onFinally(); |
| 112 | + } |
| 113 | + } |
| 114 | + return this; |
| 115 | + } |
| 116 | + |
| 117 | + private resolve(data: T): void { |
| 118 | + // Resolve this promise, if it is still pending. This function is passed to the constructor function. |
| 119 | + if (this.state === __TS__PromiseState.Pending) { |
| 120 | + this.state = __TS__PromiseState.Fulfilled; |
| 121 | + this.value = data; |
| 122 | + |
| 123 | + for (const callback of this.fulfilledCallbacks) { |
| 124 | + callback(data); |
| 125 | + } |
| 126 | + for (const callback of this.finallyCallbacks) { |
| 127 | + callback(); |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + private reject(reason: any): void { |
| 133 | + // Reject this promise, if it is still pending. This function is passed to the constructor function. |
| 134 | + if (this.state === __TS__PromiseState.Pending) { |
| 135 | + this.state = __TS__PromiseState.Rejected; |
| 136 | + this.rejectionReason = reason; |
| 137 | + |
| 138 | + for (const callback of this.rejectedCallbacks) { |
| 139 | + callback(reason); |
| 140 | + } |
| 141 | + for (const callback of this.finallyCallbacks) { |
| 142 | + callback(); |
| 143 | + } |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + private createPromiseResolvingCallback<TResult1, TResult2>( |
| 148 | + f: FulfillCallback<T, TResult1> | RejectCallback<TResult2>, |
| 149 | + resolve: FulfillCallback<TResult1 | TResult2, unknown>, |
| 150 | + reject: RejectCallback<unknown> |
| 151 | + ) { |
| 152 | + return value => { |
| 153 | + try { |
| 154 | + this.handleCallbackData(f(value), resolve, reject); |
| 155 | + } catch (e) { |
| 156 | + // If a handler function throws an error, the promise returned by then gets rejected with the thrown error as its value |
| 157 | + reject(e); |
| 158 | + } |
| 159 | + }; |
| 160 | + } |
| 161 | + private handleCallbackData<TResult1, TResult2, TResult extends TResult1 | TResult2>( |
| 162 | + data: TResult | PromiseLike<TResult>, |
| 163 | + resolve: FulfillCallback<TResult1 | TResult2, unknown>, |
| 164 | + reject: RejectCallback<unknown> |
| 165 | + ) { |
| 166 | + if (__TS__IsPromiseLike<TResult>(data)) { |
| 167 | + const nextpromise = data as __TS__Promise<TResult>; |
| 168 | + if (nextpromise.state === __TS__PromiseState.Fulfilled) { |
| 169 | + // If a handler function returns an already fulfilled promise, |
| 170 | + // the promise returned by then gets fulfilled with that promise's value |
| 171 | + resolve(nextpromise.value); |
| 172 | + } else if (nextpromise.state === __TS__PromiseState.Rejected) { |
| 173 | + // If a handler function returns an already rejected promise, |
| 174 | + // the promise returned by then gets fulfilled with that promise's value |
| 175 | + reject(nextpromise.rejectionReason); |
| 176 | + } else { |
| 177 | + // If a handler function returns another pending promise object, the resolution/rejection |
| 178 | + // of the promise returned by then will be subsequent to the resolution/rejection of |
| 179 | + // the promise returned by the handler. |
| 180 | + data.then(resolve, reject); |
| 181 | + } |
| 182 | + } else { |
| 183 | + // If a handler returns a value, the promise returned by then gets resolved with the returned value as its value |
| 184 | + // If a handler doesn't return anything, the promise returned by then gets resolved with undefined |
| 185 | + resolve(data); |
| 186 | + } |
| 187 | + } |
| 188 | +} |
0 commit comments