-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: post message to worker when message event is emitted (#145)
* feat: post message to worker when message event is emitted * test: added missing MessageChannel import
- Loading branch information
1 parent
8f7930d
commit f7516e1
Showing
5 changed files
with
86 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
'use strict' | ||
|
||
const { parentPort } = require('worker_threads') | ||
const { Writable } = require('stream') | ||
|
||
function run () { | ||
parentPort.once('message', function ({ text, takeThisPortPlease }) { | ||
takeThisPortPlease.postMessage(`received: ${text}`) | ||
}) | ||
return new Writable({ | ||
autoDestroy: true, | ||
write (chunk, enc, cb) { | ||
cb() | ||
} | ||
}) | ||
} | ||
|
||
module.exports = run |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
'use strict' | ||
|
||
const { test } = require('tap') | ||
const { join } = require('path') | ||
const { once } = require('events') | ||
const { MessageChannel } = require('worker_threads') | ||
const ThreadStream = require('..') | ||
|
||
test('message events emitted on the stream are posted to the worker', async function (t) { | ||
t.plan(1) | ||
|
||
const { port1, port2 } = new MessageChannel() | ||
const stream = new ThreadStream({ | ||
filename: join(__dirname, 'on-message.js'), | ||
sync: false | ||
}) | ||
t.teardown(() => { | ||
stream.end() | ||
}) | ||
|
||
stream.emit('message', { text: 'hello', takeThisPortPlease: port1 }, [port1]) | ||
const [confirmation] = await once(port2, 'message') | ||
t.equal(confirmation, 'received: hello') | ||
}) |