X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=lib%2Ffixed.js;h=8d899d57bb4ed00a54e4d1a1f2093641d8bd3f77;hb=a3c8691eb5bd772a43746fd5860d54a786463039;hp=645528283d80807cbc5374b5184c64b0f4c83c9e;hpb=57df5469cb6f9ffd7c3501f026f2659b3cf88f06;p=poolifier.git diff --git a/lib/fixed.js b/lib/fixed.js index 64552828..8d899d57 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.
@@ -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)