From f902b9774135f3d7230a42fa659acbc510141e29 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 --- 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 f113bb2d967805..ae0264311baca4 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 aae2dc8a39e585..c08e4b677b5dca 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 b26fbc3769d3f3..4212e5bf2f7afb 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(); +}