Skip to content

Commit f4fd3fb

Browse files
addaleaxdanielleadams
authored andcommitted
worker: allow BroadcastChannel in receiveMessageOnPort
PR-URL: #37535 Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Michaël Zasso <[email protected]>
1 parent a44daff commit f4fd3fb

File tree

3 files changed

+17
-2
lines changed

3 files changed

+17
-2
lines changed

doc/api/worker_threads.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,9 +179,13 @@ if (isMainThread) {
179179
## `worker.receiveMessageOnPort(port)`
180180
<!-- YAML
181181
added: v12.3.0
182+
changes:
183+
- version: REPLACEME
184+
pr-url: https://github.com/nodejs/node/pull/37535
185+
description: The port argument can also refer to a `BroadcastChannel` now.
182186
-->
183187

184-
* `port` {MessagePort}
188+
* `port` {MessagePort|BroadcastChannel}
185189

186190
* Returns: {Object|undefined}
187191

lib/internal/worker/io.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ function createWorkerStdio() {
333333
}
334334

335335
function receiveMessageOnPort(port) {
336-
const message = receiveMessageOnPort_(port);
336+
const message = receiveMessageOnPort_(port?.[kHandle] ?? port);
337337
if (message === noMessageSymbol) return undefined;
338338
return { message };
339339
}

test/parallel/test-worker-broadcastchannel.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const common = require('../common');
44
const {
55
BroadcastChannel,
66
Worker,
7+
receiveMessageOnPort
78
} = require('worker_threads');
89
const assert = require('assert');
910

@@ -140,3 +141,13 @@ assert.throws(() => new BroadcastChannel(), {
140141
message: /BroadcastChannel is closed/
141142
});
142143
}
144+
145+
{
146+
const bc1 = new BroadcastChannel('channel4');
147+
const bc2 = new BroadcastChannel('channel4');
148+
bc1.postMessage('some data');
149+
assert.strictEqual(receiveMessageOnPort(bc2).message, 'some data');
150+
assert.strictEqual(receiveMessageOnPort(bc2), undefined);
151+
bc1.close();
152+
bc2.close();
153+
}

0 commit comments

Comments
 (0)