-
Notifications
You must be signed in to change notification settings - Fork 2
/
worker.js
55 lines (40 loc) · 3.21 KB
/
worker.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
const assert = require('assert');
const { MessagePort, parentPort } = require('worker_threads');
let lastProcessID = 0;
let workerPorts = [];
parentPort.on('message', (value) => {
/*
███████╗██████╗ ██╗███╗ ██╗ ██╗ ██╗██████╗
██╔════╝██╔══██╗██║████╗ ██║ ██║ ██║██╔══██╗
███████╗██████╔╝██║██╔██╗ ██║ ██║ ██║██████╔╝
╚════██║██╔═══╝ ██║██║╚██╗██║ ██║ ██║██╔═══╝
███████║██║ ██║██║ ╚████║ ╚██████╔╝██║
╚══════╝╚═╝ ╚═╝╚═╝ ╚═══╝ ╚═════╝ ╚═╝
*/
if (value.hereAreYourPorts !== undefined && value.answerOnPort !== undefined) {
for (let port of value.hereAreYourPorts) {
assert(port instanceof MessagePort);
workerPorts.push(port);
}
console.log('\x1b[37m', `Worker recieved ${workerPorts.length} ports`);
workerPorts[value.answerOnPort].postMessage('ok');
}
/*
██╗ ██╗ ██████╗ ██████╗ ██╗ ██╗███████╗██████╗ ███╗ ███╗ █████╗ ██╗███╗ ██╗
██║ ██║██╔═══██╗██╔══██╗██║ ██╔╝██╔════╝██╔══██╗ ████╗ ████║██╔══██╗██║████╗ ██║
██║ █╗ ██║██║ ██║██████╔╝█████╔╝ █████╗ ██████╔╝ ██╔████╔██║███████║██║██╔██╗ ██║
██║███╗██║██║ ██║██╔══██╗██╔═██╗ ██╔══╝ ██╔══██╗ ██║╚██╔╝██║██╔══██║██║██║╚██╗██║
╚███╔███╔╝╚██████╔╝██║ ██║██║ ██╗███████╗██║ ██║ ██║ ╚═╝ ██║██║ ██║██║██║ ╚████║
╚══╝╚══╝ ╚═════╝ ╚═╝ ╚═╝╚═╝ ╚═╝╚══════╝╚═╝ ╚═╝ ╚═╝ ╚═╝╚═╝ ╚═╝╚═╝╚═╝ ╚═══╝
*/
if (value.someArgument !== undefined && value.processID !== undefined && value.workerId !== undefined && lastProcessID !== value.processID) {
lastProcessID = value.processID;
/// Do some heavy lifting here
let result = value.someArgument + ' - done by a worker || on port ' + value.answerOnPort;
workerPorts[value.answerOnPort].postMessage({
result: result,
processID: value.processID,
workerId: value.workerId
});
}
});