MCPcopy Create free account
hub / github.com/MultithreadedJSBook/code-samples / constructor

Method constructor

ch6-thread-pool/rpc-worker.js:6–24  ·  view source on GitHub ↗
(path, size = 0, strategy = 'roundrobin')

Source from the content-addressed store, hash-verified

4
5module.exports = class RpcWorkerPool {
6 constructor(path, size = 0, strategy = 'roundrobin') {
7 if (size === 0) this.size = CORES; // <1>
8 else if (size < 0) this.size = Math.max(CORES + size, 1);
9 else this.size = size;
10
11 if (!STRATEGIES.has(strategy)) throw new TypeError('invalid strategy');
12 this.strategy = strategy; // <2>
13 this.rr_index = -1;
14
15 this.next_command_id = 0;
16 this.workers = []; // <3>
17 for (let i = 0; i < this.size; i++) {
18 const worker = new Worker(path);
19 this.workers.push({ worker, in_flight_commands: new Map() }); // <4>
20 worker.on('message', (msg) => {
21 this.onMessageHandler(msg, i);
22 });
23 }
24 }
25// THIS LINE SHOULD NOT APPEAR IN PRINT
26 onMessageHandler(msg, worker_id) {
27 const worker = this.workers[worker_id];

Callers

nothing calls this directly

Calls 1

onMessageHandlerMethod · 0.95

Tested by

no test coverage detected