thread-stream
Advanced tools
Comparing version 2.0.1 to 2.1.0
@@ -166,2 +166,9 @@ 'use strict' | ||
break | ||
case 'EVENT': | ||
if (Array.isArray(msg.args)) { | ||
stream.emit(msg.name, ...msg.args) | ||
} else { | ||
stream.emit(msg.name, msg.args) | ||
} | ||
break | ||
default: | ||
@@ -168,0 +175,0 @@ destroy(stream, new Error('this should not happen: ' + msg.code)) |
{ | ||
"name": "thread-stream", | ||
"version": "2.0.1", | ||
"version": "2.1.0", | ||
"description": "A streaming way to send data to a Node.js Worker Thread", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -87,4 +87,29 @@ # thread-stream | ||
### Emit events | ||
You can emit events on the ThreadStream from your worker using [`worker.parentPort.postMessage()`](https://nodejs.org/api/worker_threads.html#workerparentport). | ||
The message (JSON object) must have the following data structure: | ||
```js | ||
parentPort.postMessage({ | ||
code: 'EVENT', | ||
name: 'eventName', | ||
args: ['list', 'of', 'args', 123, new Error('Boom')] | ||
}) | ||
``` | ||
On your ThreadStream, you can add a listener function for this event name: | ||
```js | ||
const stream = new ThreadStream({ | ||
filename: join(__dirname, 'worker.js'), | ||
workerData: {}, | ||
}) | ||
stream.on('eventName', function (a, b, c, n, err) { | ||
console.log('received:', a, b, c, n, err) // received: list of args 123 Error: Boom | ||
}) | ||
``` | ||
## License | ||
MIT |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
61670
47
1756
115