-
-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathpool-worker.js
More file actions
58 lines (53 loc) · 1.89 KB
/
Copy pathpool-worker.js
File metadata and controls
58 lines (53 loc) · 1.89 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
'use strict';
const { parentPort, workerData } = require('worker_threads');
const workerFn = require('./worker');
const { parseCamaroJson } = require('./json-parse');
const { CTRL, STATE, writeResultToSab } = require('./sab-ipc');
const sabChannel = workerData && workerData.sab ? workerData.sab : null;
parentPort.on('message', async ({ id, task, sab: useSab, waitForSab }) => {
try {
if (useSab && sabChannel) {
const xmlLen = Atomics.load(sabChannel.control, CTRL.XML_LEN);
const localTask = {
fn: task.fn,
args: [sabChannel.xml.subarray(0, xmlLen), ...task.args.slice(1)],
};
Atomics.store(sabChannel.control, CTRL.STATE, STATE.BUSY);
const raw = await workerFn(localTask);
if (waitForSab) {
writeResultToSab(sabChannel, raw);
Atomics.store(sabChannel.control, CTRL.STATE, STATE.DONE);
Atomics.notify(sabChannel.control, CTRL.STATE, 1);
return;
}
Atomics.store(sabChannel.control, CTRL.STATE, STATE.DONE);
// JSON strings are cheaply cloned by V8. Avoiding the explicit
// UTF-8 encode into SAB and decode on the parent removes two
// copies from the latency-sensitive single-request path.
parentPort.postMessage({ id, raw: true, result: raw });
return;
}
const xmlBuf = task.args[0];
const transfer = [];
const recycleXml = task.recycleXml && xmlBuf instanceof Uint8Array && xmlBuf.buffer && xmlBuf.buffer.byteLength > 0;
if (recycleXml) transfer.push(xmlBuf.buffer);
parentPort.postMessage(
{
id,
result: parseCamaroJson(await workerFn(task)),
xmlBuf: recycleXml ? xmlBuf : undefined,
},
transfer
);
} catch (err) {
if (useSab && sabChannel) {
Atomics.store(sabChannel.control, CTRL.STATE, STATE.ERROR);
Atomics.notify(sabChannel.control, CTRL.STATE, 1);
}
parentPort.postMessage({
id,
sab: useSab && sabChannel ? true : undefined,
error: err && err.message ? err.message : String(err),
});
}
});