Skip to content

Commit

Permalink
worker: graduate BroadcastChannel to supported
Browse files Browse the repository at this point in the history
Signed-off-by: James M Snell <jasnell@gmail.com>

PR-URL: #41271
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
  • Loading branch information
jasnell committed Dec 24, 2021
1 parent 1e34969 commit 6486a30
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
6 changes: 4 additions & 2 deletions doc/api/worker_threads.md
Original file line number Diff line number Diff line change
Expand Up @@ -347,10 +347,12 @@ if (isMainThread) {

<!-- YAML
added: v15.4.0
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/00000
description: No longer experimental.
-->

> Stability: 1 - Experimental
Instances of `BroadcastChannel` allow asynchronous one-to-many communication
with all other `BroadcastChannel` instances bound to the same channel name.

Expand Down
20 changes: 20 additions & 0 deletions lib/internal/worker/io.js
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,9 @@ function isBroadcastChannel(value) {
}

class BroadcastChannel extends EventTarget {
/**
* @param {string} name
*/
constructor(name) {
if (arguments.length === 0)
throw new ERR_MISSING_ARGS('name');
Expand Down Expand Up @@ -426,12 +429,18 @@ class BroadcastChannel extends EventTarget {
}, opts)}`;
}

/**
* @type {string}
*/
get name() {
if (!isBroadcastChannel(this))
throw new ERR_INVALID_THIS('BroadcastChannel');
return this[kName];
}

/**
* @returns {void}
*/
close() {
if (!isBroadcastChannel(this))
throw new ERR_INVALID_THIS('BroadcastChannel');
Expand All @@ -445,6 +454,11 @@ class BroadcastChannel extends EventTarget {
this[kHandle] = undefined;
}

/**
*
* @param {*} message
* @returns {void}
*/
postMessage(message) {
if (!isBroadcastChannel(this))
throw new ERR_INVALID_THIS('BroadcastChannel');
Expand All @@ -460,6 +474,9 @@ class BroadcastChannel extends EventTarget {
// BroadcastChannel API definition. Typically we shouldn't extend Web
// Platform APIs with Node.js specific methods but ref and unref
// are a bit special.
/**
* @returns {BroadcastChannel}
*/
ref() {
if (!isBroadcastChannel(this))
throw new ERR_INVALID_THIS('BroadcastChannel');
Expand All @@ -472,6 +489,9 @@ class BroadcastChannel extends EventTarget {
// BroadcastChannel API definition. Typically we shouldn't extend Web
// Platform APIs with Node.js specific methods but ref and unref
// are a bit special.
/**
* @returns {BroadcastChannel}
*/
unref() {
if (!isBroadcastChannel(this))
throw new ERR_INVALID_THIS('BroadcastChannel');
Expand Down

0 comments on commit 6486a30

Please sign in to comment.