Skip to content

Commit 1633a70

Browse files
committed
Change architecture
1 parent c31306a commit 1633a70

File tree

3 files changed

+23
-26
lines changed

3 files changed

+23
-26
lines changed

src/workers/fibonacciWorker.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
const fb = require('fibonacci');
2+
const { Worker, isMainThread, parentPort, workerData } = require('worker_threads');
3+
4+
const runFibonacci = workerData => {
5+
return new Promise((resolve, reject) => {
6+
const worker = new Worker(__filename, { workerData });
7+
worker.on('message', resolve);
8+
worker.on('error', reject);
9+
worker.on('exit', code => {
10+
if (code !== 0) reject(new Error(`Worker stopped with exit code ${code}`));
11+
});
12+
});
13+
};
14+
15+
/**
16+
* If it's not the main thread it's one of the Worker threads
17+
*/
18+
if (!isMainThread) {
19+
const result = fb.iterate(workerData.iterations);
20+
parentPort.postMessage(result);
21+
}
22+
23+
module.exports = runFibonacci;

src/workers/fibonacciWorker/index.js

Lines changed: 0 additions & 18 deletions
This file was deleted.

src/workers/fibonacciWorker/worker.js

Lines changed: 0 additions & 8 deletions
This file was deleted.

0 commit comments

Comments
 (0)