From 3499ea6f55f0333ba48ff1aa86ac0e7207b1082f Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Sat, 27 Feb 2021 18:29:27 +0100 Subject: [PATCH 1/2] 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..c237b9638fc877 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(); +} From 285f57dba1e208c983c9d0f322cb0526c3f4b711 Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Sat, 27 Feb 2021 20:25:34 +0100 Subject: [PATCH 2/2] fixup! worker: allow BroadcastChannel in receiveMessageOnPort --- lib/internal/worker/io.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/internal/worker/io.js b/lib/internal/worker/io.js index c08e4b677b5dca..7a9eb2bdbe1509 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[kHandle] ?? port); + const message = receiveMessageOnPort_(port?.[kHandle] ?? port); if (message === noMessageSymbol) return undefined; return { message }; }