-
Notifications
You must be signed in to change notification settings - Fork 107
/
Copy pathdeferred-promise.ts
186 lines (167 loc) · 5.3 KB
/
deferred-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
import { UsingDisposable, type Disposable } from "./lifetime"
import type { QuickJSHandle } from "./types"
import type { QuickJSRuntime } from "./runtime"
import type { QuickJSContext } from "./context"
export type { PromiseExecutor } from "./types"
/**
* A promise state inside QuickJS, which can be pending, fulfilled, or rejected.
* You can unwrap a JSPromiseState with {@link QuickJSContext#unwrapResult}.
*/
export type JSPromiseState =
| JSPromiseStatePending
| JSPromiseStateFulfilled
| JSPromiseStateRejected
/**
* Pending promise state.
* See {@link JSPromiseState}.
*/
export interface JSPromiseStatePending {
type: "pending"
/**
* The error property here allows unwrapping a JSPromiseState with {@link QuickJSContext#unwrapResult}.
* Unwrapping a pending promise will throw a {@link QuickJSPromisePending} error.
*/
get error(): Error
}
/**
* Fulfilled promise state.
* See {@link JSPromiseState}.
*/
export interface JSPromiseStateFulfilled {
type: "fulfilled"
value: QuickJSHandle
error?: undefined
/** Trying to get the promise state of a non-Promise value returns a fulfilled state with the original value, and `notAPromise: true`. */
notAPromise?: boolean
}
/**
* Rejected promise state.
* See {@link JSPromiseState}.
*/
export interface JSPromiseStateRejected {
type: "rejected"
error: QuickJSHandle
}
/**
* QuickJSDeferredPromise wraps a QuickJS promise {@link handle} and allows
* {@link resolve}ing or {@link reject}ing that promise. Use it to bridge asynchronous
* code on the host to APIs inside a QuickJSContext.
*
* Managing the lifetime of promises is tricky. There are three
* {@link QuickJSHandle}s inside of each deferred promise object: (1) the promise
* itself, (2) the `resolve` callback, and (3) the `reject` callback.
*
* - If the promise will be fulfilled before the end of it's {@link owner}'s lifetime,
* the only cleanup necessary is `deferred.handle.dispose()`, because
* calling {@link resolve} or {@link reject} will dispose of both callbacks automatically.
*
* - As the return value of a {@link VmFunctionImplementation}, return {@link handle},
* and ensure that either {@link resolve} or {@link reject} will be called. No other
* clean-up is necessary.
*
* - In other cases, call {@link dispose}, which will dispose {@link handle} as well as the
* QuickJS handles that back {@link resolve} and {@link reject}. For this object,
* {@link dispose} is idempotent.
*/
export class QuickJSDeferredPromise extends UsingDisposable implements Disposable {
public owner: QuickJSRuntime
public context: QuickJSContext
/**
* A handle of the Promise instance inside the QuickJSContext.
* You must dispose {@link handle} or the entire QuickJSDeferredPromise once you
* are finished with it.
*/
public handle: QuickJSHandle
/**
* A native promise that will resolve once this deferred is settled.
*/
public settled: Promise<void>
private resolveHandle: QuickJSHandle
private rejectHandle: QuickJSHandle
private onSettled!: () => void
/**
* Use {@link QuickJSContext#newPromise} to create a new promise instead of calling
* this constructor directly.
*/
constructor(args: {
context: QuickJSContext
promiseHandle: QuickJSHandle
resolveHandle: QuickJSHandle
rejectHandle: QuickJSHandle
}) {
super()
this.context = args.context
this.owner = args.context.runtime
this.handle = args.promiseHandle
this.settled = new Promise((resolve) => {
this.onSettled = resolve
})
this.resolveHandle = args.resolveHandle
this.rejectHandle = args.rejectHandle
}
/**
* Resolve {@link handle} with the given value, if any.
* Calling this method after calling {@link dispose} is a no-op.
*
* Note that after resolving a promise, you may need to call
* {@link QuickJSRuntime#executePendingJobs} to propagate the result to the promise's
* callbacks.
*/
resolve = (value?: QuickJSHandle) => {
if (!this.resolveHandle.alive) {
return
}
this.context
.unwrapResult(
this.context.callFunction(
this.resolveHandle,
this.context.undefined,
value || this.context.undefined,
),
)
.dispose()
this.disposeResolvers()
this.onSettled()
}
/**
* Reject {@link handle} with the given value, if any.
* Calling this method after calling {@link dispose} is a no-op.
*
* Note that after rejecting a promise, you may need to call
* {@link QuickJSRuntime#executePendingJobs} to propagate the result to the promise's
* callbacks.
*/
reject = (value?: QuickJSHandle) => {
if (!this.rejectHandle.alive) {
return
}
this.context
.unwrapResult(
this.context.callFunction(
this.rejectHandle,
this.context.undefined,
value || this.context.undefined,
),
)
.dispose()
this.disposeResolvers()
this.onSettled()
}
get alive() {
return this.handle.alive || this.resolveHandle.alive || this.rejectHandle.alive
}
dispose = () => {
if (this.handle.alive) {
this.handle.dispose()
}
this.disposeResolvers()
}
private disposeResolvers() {
if (this.resolveHandle.alive) {
this.resolveHandle.dispose()
}
if (this.rejectHandle.alive) {
this.rejectHandle.dispose()
}
}
}