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:
Anna Henningsen committed 2021-03-03 23:22:26 +01:00
1 parent 0c11f2ee06
commit 9dd2f9f19d
3 files changed
+17 -2

No files matched your search

+5 -1
View File
@@ -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}
+1 -1
View File
@@ -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();
}