deno.land / x / deno@v1.28.2 / runtime / js / 11_workers.js

نووسراو ببینە
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
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license."use strict";
((window) => { const core = window.Deno.core; const ops = core.ops; const { Error, ObjectPrototypeIsPrototypeOf, StringPrototypeStartsWith, String, SymbolIterator, SymbolToStringTag, } = window.__bootstrap.primordials; const webidl = window.__bootstrap.webidl; const { URL } = window.__bootstrap.url; const { getLocationHref } = window.__bootstrap.location; const { serializePermissions } = window.__bootstrap.permissions; const { log } = window.__bootstrap.util; const { ErrorEvent, MessageEvent, defineEventHandler } = window.__bootstrap.event; const { EventTarget } = window.__bootstrap.eventTarget; const { deserializeJsMessageData, serializeJsMessageData, MessagePortPrototype, } = window.__bootstrap.messagePort;
function createWorker( specifier, hasSourceCode, sourceCode, permissions, name, workerType, ) { return ops.op_create_worker({ hasSourceCode, name, permissions: serializePermissions(permissions), sourceCode, specifier, workerType, }); }
function hostTerminateWorker(id) { ops.op_host_terminate_worker(id); }
function hostPostMessage(id, data) { ops.op_host_post_message(id, data); }
function hostRecvCtrl(id) { return core.opAsync("op_host_recv_ctrl", id); }
function hostRecvMessage(id) { return core.opAsync("op_host_recv_message", id); }
class Worker extends EventTarget { #id = 0; #name = "";
// "RUNNING" | "CLOSED" | "TERMINATED" // "TERMINATED" means that any controls or messages received will be // discarded. "CLOSED" means that we have received a control // indicating that the worker is no longer running, but there might // still be messages left to receive. #status = "RUNNING";
constructor(specifier, options = {}) { super(); specifier = String(specifier); const { deno, name, type = "classic", } = options;
const workerType = webidl.converters["WorkerType"](type);
if ( StringPrototypeStartsWith(specifier, "./") || StringPrototypeStartsWith(specifier, "../") || StringPrototypeStartsWith(specifier, "/") || workerType === "classic" ) { const baseUrl = getLocationHref(); if (baseUrl != null) { specifier = new URL(specifier, baseUrl).href; } }
this.#name = name; let hasSourceCode, sourceCode; if (workerType === "classic") { hasSourceCode = true; sourceCode = `importScripts("#");`; } else { hasSourceCode = false; sourceCode = ""; }
const id = createWorker( specifier, hasSourceCode, sourceCode, deno?.permissions, name, workerType, ); this.#id = id; this.#pollControl(); this.#pollMessages(); }
#handleError(e) { const event = new ErrorEvent("error", { cancelable: true, message: e.message, lineno: e.lineNumber ? e.lineNumber : undefined, colno: e.columnNumber ? e.columnNumber : undefined, filename: e.fileName, error: null, });
this.dispatchEvent(event); // Don't bubble error event to window for loader errors (`!e.fileName`). // TODO(nayeemrmn): It's not correct to use `e.fileName` to detect user // errors. It won't be there for non-awaited async ops for example. if (e.fileName && !event.defaultPrevented) { window.dispatchEvent(event); }
return event.defaultPrevented; }
#pollControl = async () => { while (this.#status === "RUNNING") { const [type, data] = await hostRecvCtrl(this.#id);
// If terminate was called then we ignore all messages if (this.#status === "TERMINATED") { return; }
switch (type) { case 1: { // TerminalError this.#status = "CLOSED"; } /* falls through */ case 2: { // Error if (!this.#handleError(data)) { throw new Error("Unhandled error in child worker."); } break; } case 3: { // Close log(`Host got "close" message from worker: ${this.#name}`); this.#status = "CLOSED"; return; } default: { throw new Error(`Unknown worker event: "${type}"`); } } } };
#pollMessages = async () => { while (this.#status !== "TERMINATED") { const data = await hostRecvMessage(this.#id); if (this.#status === "TERMINATED" || data === null) { return; } let message, transferables; try { const v = deserializeJsMessageData(data); message = v[0]; transferables = v[1]; } catch (err) { const event = new MessageEvent("messageerror", { cancelable: false, data: err, }); this.dispatchEvent(event); return; } const event = new MessageEvent("message", { cancelable: false, data: message, ports: transferables.filter((t) => ObjectPrototypeIsPrototypeOf(MessagePortPrototype, t) ), }); this.dispatchEvent(event); } };
postMessage(message, transferOrOptions = {}) { const prefix = "Failed to execute 'postMessage' on 'MessagePort'"; webidl.requiredArguments(arguments.length, 1, { prefix }); message = webidl.converters.any(message); let options; if ( webidl.type(transferOrOptions) === "Object" && transferOrOptions !== undefined && transferOrOptions[SymbolIterator] !== undefined ) { const transfer = webidl.converters["sequence<object>"]( transferOrOptions, { prefix, context: "Argument 2" }, ); options = { transfer }; } else { options = webidl.converters.StructuredSerializeOptions( transferOrOptions, { prefix, context: "Argument 2", }, ); } const { transfer } = options; const data = serializeJsMessageData(message, transfer); if (this.#status === "RUNNING") { hostPostMessage(this.#id, data); } }
terminate() { if (this.#status !== "TERMINATED") { this.#status = "TERMINATED"; hostTerminateWorker(this.#id); } }
[SymbolToStringTag] = "Worker"; }
defineEventHandler(Worker.prototype, "error"); defineEventHandler(Worker.prototype, "message"); defineEventHandler(Worker.prototype, "messageerror");
webidl.converters["WorkerType"] = webidl.createEnumConverter("WorkerType", [ "classic", "module", ]);
window.__bootstrap.worker = { Worker, };})(this);
deno

Version Info

Tagged at
a year ago