-
Notifications
You must be signed in to change notification settings - Fork 30.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
worker: support MessagePort to workers data #32278
Closed
+69
−1
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
'use strict'; | ||
|
||
require('../common'); | ||
const assert = require('assert'); | ||
|
||
const { | ||
Worker, MessageChannel | ||
} = require('worker_threads'); | ||
|
||
const channel = new MessageChannel(); | ||
const workerData = { mesage: channel.port1 }; | ||
const transferList = [channel.port1]; | ||
const meowScript = () => 'meow'; | ||
|
||
{ | ||
// Should receive the transferList param. | ||
new Worker(`${meowScript}`, { eval: true, workerData, transferList }); | ||
} | ||
|
||
{ | ||
// Should work with more than one MessagePort. | ||
const channel1 = new MessageChannel(); | ||
const channel2 = new MessageChannel(); | ||
const workerData = { message: channel1.port1, message2: channel2.port1 }; | ||
const transferList = [channel1.port1, channel2.port1]; | ||
new Worker(`${meowScript}`, { eval: true, workerData, transferList }); | ||
} | ||
|
||
{ | ||
const uint8Array = new Uint8Array([ 1, 2, 3, 4 ]); | ||
assert.deepStrictEqual(uint8Array.length, 4); | ||
new Worker(` | ||
const { parentPort, workerData } = require('worker_threads'); | ||
parentPort.postMessage(workerData); | ||
`, { | ||
eval: true, | ||
workerData: uint8Array, | ||
transferList: [uint8Array.buffer] | ||
}).on( | ||
'message', | ||
(message) => | ||
assert.deepStrictEqual(message, Uint8Array.of(1, 2, 3, 4)) | ||
); | ||
assert.deepStrictEqual(uint8Array.length, 0); | ||
} | ||
|
||
{ | ||
// Should throw on non valid transferList input. | ||
const channel1 = new MessageChannel(); | ||
const channel2 = new MessageChannel(); | ||
const workerData = { message: channel1.port1, message2: channel2.port1 }; | ||
assert.throws(() => new Worker(`${meowScript}`, { | ||
eval: true, | ||
workerData, | ||
transferList: [] | ||
}), { | ||
code: 'ERR_MISSING_MESSAGE_PORT_IN_TRANSFER_LIST', | ||
message: 'MessagePort was found in message but not listed in transferList' | ||
}); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
semicolon, excuse me.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nope, thanks for the the new test and semicolon is not required (
make lint-js
passed on my machine)