From 9dd2f9f19dd35a91eabd0aa5acff9011db59e238 Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Sat, 27 Feb 2021 18:29:27 +0100 Subject: [PATCH] worker: allow BroadcastChannel in receiveMessageOnPort MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PR-URL: https://github.com/nodejs/node/pull/37535 Reviewed-By: Colin Ihrig Reviewed-By: James M Snell Reviewed-By: Michaƫl Zasso --- doc/api/worker_threads.md | 6 +++++- lib/internal/worker/io.js | 2 +- test/parallel/test-worker-broadcastchannel.js | 11 +++++++++++ 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/doc/api/worker_threads.md b/doc/api/worker_threads.md index 3f184d5ffe2..7942788df58 100644 --- a/doc/api/worker_threads.md +++ b/doc/api/worker_threads.md @@ -179,9 +179,13 @@ if (isMainThread) { ## `worker.receiveMessageOnPort(port)` -* `port` {MessagePort} +* `port` {MessagePort|BroadcastChannel} * Returns: {Object|undefined} diff --git a/lib/internal/worker/io.js b/lib/internal/worker/io.js index aae2dc8a39e..7a9eb2bdbe1 100644 --- a/lib/internal/worker/io.js +++ b/lib/internal/worker/io.js @@ -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 }; } diff --git a/test/parallel/test-worker-broadcastchannel.js b/test/parallel/test-worker-broadcastchannel.js index b26fbc3769d..4212e5bf2f7 100644 --- a/test/parallel/test-worker-broadcastchannel.js +++ b/test/parallel/test-worker-broadcastchannel.js @@ -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(); +}