Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

worker: graduate BroadcastChannel to supported #41271

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,7 @@ module.exports = {
Event: 'readable',
EventTarget: 'readable',
MessageChannel: 'readable',
BroadcastChannel: 'readable',
MessageEvent: 'readable',
MessagePort: 'readable',
TextEncoder: 'readable',
Expand Down
8 changes: 8 additions & 0 deletions doc/api/globals.md
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,14 @@ added: v16.0.0

Global alias for [`buffer.atob()`][].

## `BroadcastChannel`

<!-- YAML
added: REPLACEME
-->

See {BroadcastChannel}.

## `btoa(data)`

<!-- YAML
Expand Down
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/41271
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
2 changes: 2 additions & 0 deletions lib/.eslintrc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ rules:
# disabled with --without-intl build flag.
- name: Intl
message: "Use `const { Intl } = globalThis;` instead of the global."
- name: BroadcastChannel
message: "Use `const { BroadcastChannel } = require('internal/worker/io');` instead of the global."
- name: MessageChannel
message: "Use `const { MessageChannel } = require('internal/worker/io');` instead of the global."
- name: MessageEvent
Expand Down
2 changes: 2 additions & 0 deletions lib/internal/bootstrap/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -251,10 +251,12 @@ if (!config.noBrowserGlobals) {
MessageChannel,
MessagePort,
MessageEvent,
BroadcastChannel,
} = require('internal/worker/io');
exposeInterface(globalThis, 'MessageChannel', MessageChannel);
exposeInterface(globalThis, 'MessagePort', MessagePort);
exposeInterface(globalThis, 'MessageEvent', MessageEvent);
exposeInterface(globalThis, 'BroadcastChannel', BroadcastChannel);

// https://html.spec.whatwg.org/multipage/webappapis.html#windoworworkerglobalscope
const timers = require('timers');
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 {any} 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