X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=lib%2Ffixed.js;h=676947725bed0bb65fe72fe949be8652b08f39c5;hb=d3537e39561e15aa2f2ff927e50ca5ded0a4d6b2;hp=645528283d80807cbc5374b5184c64b0f4c83c9e;hpb=57df5469cb6f9ffd7c3501f026f2659b3cf88f06;p=poolifier.git diff --git a/lib/fixed.js b/lib/fixed.js index 64552828..67694772 100644 --- a/lib/fixed.js +++ b/lib/fixed.js @@ -4,6 +4,7 @@ const { } = require('worker_threads') function empty () {} +const _void = {} /** * A thread pool with a static number of threads , is possible to execute tasks in sync or async mode as you prefer.
* This pool will select the worker thread in a round robin fashion.
@@ -33,9 +34,9 @@ class FixedThreadPool { } } - destroy () { + async destroy () { for (const worker of this.workers) { - worker.terminate() + await worker.terminate() } } @@ -50,7 +51,7 @@ class FixedThreadPool { this.tasks.set(worker, this.tasks.get(worker) + 1) const id = ++this._id const res = this._execute(worker, id) - worker.postMessage({ data: data, _id: id }) + worker.postMessage({ data: data || _void, _id: id }) return res } @@ -60,7 +61,8 @@ class FixedThreadPool { if (message._id === id) { worker.port2.removeListener('message', listener) this.tasks.set(worker, this.tasks.get(worker) - 1) - resolve(message.data) + if (message.error) reject(message.error) + else resolve(message.data) } } worker.port2.on('message', listener)