mirror of
https://github.com/nodejs/node.git
synced 2026-12-03 18:03:02 -05:00
worker: allow BroadcastChannel in receiveMessageOnPort
PR-URL: https://github.com/nodejs/node/pull/37535 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Michaël Zasso <targos@protonmail.com>
This commit is contained in:
3 files changed
+17
-2
No files matched your search
@@ -179,9 +179,13 @@ if (isMainThread) {
|
||||
## `worker.receiveMessageOnPort(port)`
|
||||
<!-- YAML
|
||||
added: v12.3.0
|
||||
changes:
|
||||
- version: REPLACEME
|
||||
pr-url: https://github.com/nodejs/node/pull/37535
|
||||
description: The port argument can also refer to a `BroadcastChannel` now.
|
||||
-->
|
||||
|
||||
* `port` {MessagePort}
|
||||
* `port` {MessagePort|BroadcastChannel}
|
||||
|
||||
* Returns: {Object|undefined}
|
||||
|
||||
|
||||
@@ -333,7 +333,7 @@ function createWorkerStdio() {
|
||||
}
|
||||
|
||||
function receiveMessageOnPort(port) {
|
||||
const message = receiveMessageOnPort_(port);
|
||||
const message = receiveMessageOnPort_(port?.[kHandle] ?? port);
|
||||
if (message === noMessageSymbol) return undefined;
|
||||
return { message };
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ const common = require('../common');
|
||||
const {
|
||||
BroadcastChannel,
|
||||
Worker,
|
||||
receiveMessageOnPort
|
||||
} = require('worker_threads');
|
||||
const assert = require('assert');
|
||||
|
||||
@@ -140,3 +141,13 @@ assert.throws(() => new BroadcastChannel(), {
|
||||
message: /BroadcastChannel is closed/
|
||||
});
|
||||
}
|
||||
|
||||
{
|
||||
const bc1 = new BroadcastChannel('channel4');
|
||||
const bc2 = new BroadcastChannel('channel4');
|
||||
bc1.postMessage('some data');
|
||||
assert.strictEqual(receiveMessageOnPort(bc2).message, 'some data');
|
||||
assert.strictEqual(receiveMessageOnPort(bc2), undefined);
|
||||
bc1.close();
|
||||
bc2.close();
|
||||
}
|
||||
Reference in new issue
Block a user