Skip to content

Commit

Permalink
worker: allow BroadcastChannel in receiveMessageOnPort
Browse files Browse the repository at this point in the history
PR-URL: #37535
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
  • Loading branch information
addaleax authored and danielleadams committed Mar 16, 2021
1 parent a44daff commit f4fd3fb
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
6 changes: 5 additions & 1 deletion doc/api/worker_threads.md
Original file line number Diff line number Diff line change
Expand Up @@ -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}

Expand Down
2 changes: 1 addition & 1 deletion lib/internal/worker/io.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
}
Expand Down
11 changes: 11 additions & 0 deletions test/parallel/test-worker-broadcastchannel.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const common = require('../common');
const {
BroadcastChannel,
Worker,
receiveMessageOnPort
} = require('worker_threads');
const assert = require('assert');

Expand Down Expand Up @@ -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();
}

0 comments on commit f4fd3fb

Please sign in to comment.