-
Notifications
You must be signed in to change notification settings - Fork 2
/
worker-1.ts
287 lines (245 loc) · 6.55 KB
/
worker-1.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
// A simple way to run functions off the main thread with one-way or
// bi-directional communication.
const workerSource = `data:text/javascript,
self.onmessage = async ({ data }) => {
if (data.type == 'two-way-data') return;
if (data.type == 'two-way-init') {
eval('('+data.fn+')')({
send(value) {
postMessage({
id: data.id,
type: "result",
value,
});
},
receiver: (async function* () {
let resolve;
const { id } = data;
addEventListener("message", ({ data }) => {
if (data.id == id && data.type == "two-way-data") {
resolve(data.data);
}
});
while (true) {
yield await new Promise((res) => (resolve = res));
}
})(),
}, ...data.args);
return;
}
const result = eval('('+data.fn+')')(...data.args);
if (data.type == 'standard') {
postMessage({
id: data.id,
type: 'result',
value: await result,
});
} else if (data.type == 'generator') {
for await (const value of result) {
postMessage({
id: data.id,
type: 'result',
value,
})
}
postMessage({
id: data.id,
type: 'done',
})
}
};
`
export type ToWorker =
| {
readonly id: number
readonly type: "standard" | "generator"
readonly args: readonly unknown[]
readonly fn: string
}
| {
readonly id: number
readonly type: "two-way-init"
readonly args: readonly unknown[]
readonly fn: string
}
| {
readonly id: number
readonly type: "two-way-data"
readonly data: unknown
}
export type FromWorker =
| {
readonly id: number
readonly type: "result"
readonly value: any
}
| {
readonly id: number
readonly type: "done"
}
export interface WorkerHandle<Send, Receive> {
send(value: Send | PromiseLike<Send>): void
readonly receiver: AsyncGenerator<Awaited<Receive>>
}
export class Worker {
private worker = new globalThis.Worker(workerSource) as Omit<
globalThis.Worker,
"postMessage"
> & {
postMessage(value: ToWorker, options?: StructuredSerializeOptions): void
addEventListener(
type: "message",
fn: (event: MessageEvent<FromWorker>) => void,
): void
}
run<P extends readonly any[], R>(
fn: (...args: P) => R | PromiseLike<R>,
...args: P
): Promise<Awaited<R>> {
return new Promise<Awaited<R>>((resolve) => {
const id = Math.random()
const onMessage = ({ data }: MessageEvent<FromWorker>) => {
if (data.id == id) {
this.worker.removeEventListener("message", onMessage)
if (data.type == "result") resolve(data.value)
}
}
this.worker.addEventListener("message", onMessage)
this.worker.postMessage({
args,
id,
fn: fn.toString(),
type: "standard",
})
})
}
async *runGenerator<P extends readonly any[], R>(
fn: (
...args: P
) => AsyncIterable<R | PromiseLike<R>> | Iterable<R | PromiseLike<R>>,
...args: P
): AsyncIterableIterator<Awaited<R>> {
const id = Math.random()
const onMessage = ({ data }: MessageEvent<FromWorker>) => {
if (data.id == id) {
if (data.type == "done") {
this.worker.removeEventListener("message", onMessage)
} else {
resolve(data)
}
}
}
this.worker.addEventListener("message", onMessage)
this.worker.postMessage({
args,
id,
fn: fn.toString(),
type: "generator",
})
let resolve: (value: FromWorker) => void
while (true) {
const data = await new Promise<FromWorker>((res) => (resolve = res))
if (data.type == "result") {
yield await data.value
} else if (data.type == "done") {
return
}
}
}
twoWay<FromWorker, ToWorker, P extends readonly any[]>(
fn: (handle: WorkerHandle<FromWorker, ToWorker>, ...params: P) => void,
...params: P
): WorkerHandle<ToWorker, FromWorker> {
const id = Math.random()
const { worker } = this
worker.postMessage({
id,
type: "two-way-init",
args: params,
fn: fn.toString(),
})
return {
send(value) {
worker.postMessage({
id,
type: "two-way-data",
data: value,
})
},
receiver: (async function* () {
let resolve: (value: FromWorker) => void
worker.addEventListener("message", ({ data }) => {
if (data.id == id && data.type == "result") {
resolve(data.value)
}
})
while (true) {
yield await new Promise((res) => (resolve = res))
}
})(),
}
}
task<P extends readonly any[], R>(
fn: (...params: P) => R | PromiseLike<R>,
): (...params: P) => Promise<R> {
const handle = this.twoWay<
[id: number, value: R],
[id: number, params: P],
[string]
>(async (handle, fnSource) => {
const fn: (...params: P) => R | PromiseLike<R> = eval(fnSource)
for await (const [id, params] of handle.receiver) {
handle.send([id, await fn(...params)])
}
}, fn.toString())
return async (...params) => {
const id = Math.random()
handle.send([id, params])
for await (const [otherId, value] of handle.receiver) {
if (id == otherId) return value
}
throw new Error("The worker stopped unexpectedly.")
}
}
}
const worker = new Worker()
// Example 1: Running a function and getting the result.
const ex1 = worker.run(() => {
return 57
})
console.log(await ex1)
// Example 2: Passing data to the worker.
const ex2 = worker.run(
(a, b) => {
return a * b
},
575793485639n,
534537465938n,
)
console.log(await ex2)
// Example 3: Using a generator to send many values from the worker.
const generator = worker.runGenerator(function* () {
for (let i = 0; i < 10; i++) {
yield i
}
})
for await (const value of generator) {
console.log(value)
}
// Example 4: Setting up a reusable task.
const multiply = worker.task((a: number, b: number) => a * b)
console.log(await multiply(7, 9))
// Example 5: Creating a two-way communication channel.
const handle = worker.twoWay<number, string, []>(async (handle) => {
for await (const text of handle.receiver) {
console.log("worker got", text)
handle.send(Math.random())
}
})
;(async () => {
for await (const value of handle.receiver) {
console.log("script got", value)
setTimeout(() => handle.send(Math.random().toString(36).slice(2, 12)), 500)
}
})()
handle.send("initial")