Skip to content

Commit

Permalink
worker: allow BroadcastChannel in receiveMessageOnPort
Browse files Browse the repository at this point in the history
  • Loading branch information
addaleax committed Feb 27, 2021
1 parent 5968c54 commit 3499ea6
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 3499ea6

Please sign in to comment.