summaryrefslogtreecommitdiff
path: root/ext/broadcast_channel/01_broadcast_channel.js
diff options
context:
space:
mode:
authorLeo Kettmeir <crowlkats@toaxl.com>2023-02-07 20:22:46 +0100
committerGitHub <noreply@github.com>2023-02-07 20:22:46 +0100
commitb4aa1530970f7b9cc4e6f2f27e077852c4e178d3 (patch)
tree3d008912affe8550692183bd2697a386db5e3c79 /ext/broadcast_channel/01_broadcast_channel.js
parent65500f36e870b4ada3996b06aa287e30177d21a3 (diff)
refactor: Use ES modules for internal runtime code (#17648)
This PR refactors all internal js files (except core) to be written as ES modules. `__bootstrap`has been mostly replaced with static imports in form in `internal:[path to file from repo root]`. To specify if files are ESM, an `esm` method has been added to `Extension`, similar to the `js` method. A new ModuleLoader called `InternalModuleLoader` has been added to enable the loading of internal specifiers, which is used in all situations except when a snapshot is only loaded, and not a new one is created from it. --------- Co-authored-by: Bartek IwaƄczuk <biwanczuk@gmail.com>
Diffstat (limited to 'ext/broadcast_channel/01_broadcast_channel.js')
-rw-r--r--ext/broadcast_channel/01_broadcast_channel.js235
1 files changed, 117 insertions, 118 deletions
diff --git a/ext/broadcast_channel/01_broadcast_channel.js b/ext/broadcast_channel/01_broadcast_channel.js
index 82bede8b0..fb23554bf 100644
--- a/ext/broadcast_channel/01_broadcast_channel.js
+++ b/ext/broadcast_channel/01_broadcast_channel.js
@@ -2,150 +2,149 @@
/// <reference path="../../core/internal.d.ts" />
-"use strict";
-
-((window) => {
- const core = window.Deno.core;
- const ops = core.ops;
- const webidl = window.__bootstrap.webidl;
- const { MessageEvent, defineEventHandler, setTarget } =
- window.__bootstrap.event;
- const { EventTarget } = window.__bootstrap.eventTarget;
- const { DOMException } = window.__bootstrap.domException;
- const {
- ArrayPrototypeIndexOf,
- ArrayPrototypeSplice,
- ArrayPrototypePush,
- Symbol,
- Uint8Array,
- } = window.__bootstrap.primordials;
-
- const _name = Symbol("[[name]]");
- const _closed = Symbol("[[closed]]");
-
- const channels = [];
- let rid = null;
-
- async function recv() {
- while (channels.length > 0) {
- const message = await core.opAsync("op_broadcast_recv", rid);
-
- if (message === null) {
- break;
- }
-
- const { 0: name, 1: data } = message;
- dispatch(null, name, new Uint8Array(data));
+const core = globalThis.Deno.core;
+const ops = core.ops;
+import * as webidl from "internal:ext/webidl/00_webidl.js";
+import {
+ defineEventHandler,
+ EventTarget,
+ setTarget,
+} from "internal:ext/web/02_event.js";
+import DOMException from "internal:ext/web/01_dom_exception.js";
+const primordials = globalThis.__bootstrap.primordials;
+const {
+ ArrayPrototypeIndexOf,
+ ArrayPrototypeSplice,
+ ArrayPrototypePush,
+ Symbol,
+ Uint8Array,
+} = primordials;
+
+const _name = Symbol("[[name]]");
+const _closed = Symbol("[[closed]]");
+
+const channels = [];
+let rid = null;
+
+async function recv() {
+ while (channels.length > 0) {
+ const message = await core.opAsync("op_broadcast_recv", rid);
+
+ if (message === null) {
+ break;
}
- core.close(rid);
- rid = null;
+ const { 0: name, 1: data } = message;
+ dispatch(null, name, new Uint8Array(data));
}
- function dispatch(source, name, data) {
- for (let i = 0; i < channels.length; ++i) {
- const channel = channels[i];
-
- if (channel === source) continue; // Don't self-send.
- if (channel[_name] !== name) continue;
- if (channel[_closed]) continue;
-
- const go = () => {
- if (channel[_closed]) return;
- const event = new MessageEvent("message", {
- data: core.deserialize(data), // TODO(bnoordhuis) Cache immutables.
- origin: "http://127.0.0.1",
- });
- setTarget(event, channel);
- channel.dispatchEvent(event);
- };
-
- defer(go);
- }
- }
+ core.close(rid);
+ rid = null;
+}
- // Defer to avoid starving the event loop. Not using queueMicrotask()
- // for that reason: it lets promises make forward progress but can
- // still starve other parts of the event loop.
- function defer(go) {
- setTimeout(go, 1);
- }
+function dispatch(source, name, data) {
+ for (let i = 0; i < channels.length; ++i) {
+ const channel = channels[i];
- class BroadcastChannel extends EventTarget {
- [_name];
- [_closed] = false;
+ if (channel === source) continue; // Don't self-send.
+ if (channel[_name] !== name) continue;
+ if (channel[_closed]) continue;
- get name() {
- return this[_name];
- }
+ const go = () => {
+ if (channel[_closed]) return;
+ const event = new MessageEvent("message", {
+ data: core.deserialize(data), // TODO(bnoordhuis) Cache immutables.
+ origin: "http://127.0.0.1",
+ });
+ setTarget(event, channel);
+ channel.dispatchEvent(event);
+ };
- constructor(name) {
- super();
+ defer(go);
+ }
+}
- const prefix = "Failed to construct 'BroadcastChannel'";
- webidl.requiredArguments(arguments.length, 1, { prefix });
+// Defer to avoid starving the event loop. Not using queueMicrotask()
+// for that reason: it lets promises make forward progress but can
+// still starve other parts of the event loop.
+function defer(go) {
+ setTimeout(go, 1);
+}
- this[_name] = webidl.converters["DOMString"](name, {
- prefix,
- context: "Argument 1",
- });
+class BroadcastChannel extends EventTarget {
+ [_name];
+ [_closed] = false;
- this[webidl.brand] = webidl.brand;
+ get name() {
+ return this[_name];
+ }
- ArrayPrototypePush(channels, this);
+ constructor(name) {
+ super();
- if (rid === null) {
- // Create the rid immediately, otherwise there is a time window (and a
- // race condition) where messages can get lost, because recv() is async.
- rid = ops.op_broadcast_subscribe();
- recv();
- }
- }
+ const prefix = "Failed to construct 'BroadcastChannel'";
+ webidl.requiredArguments(arguments.length, 1, { prefix });
- postMessage(message) {
- webidl.assertBranded(this, BroadcastChannelPrototype);
+ this[_name] = webidl.converters["DOMString"](name, {
+ prefix,
+ context: "Argument 1",
+ });
- const prefix = "Failed to execute 'postMessage' on 'BroadcastChannel'";
- webidl.requiredArguments(arguments.length, 1, { prefix });
+ this[webidl.brand] = webidl.brand;
- if (this[_closed]) {
- throw new DOMException("Already closed", "InvalidStateError");
- }
+ ArrayPrototypePush(channels, this);
- if (typeof message === "function" || typeof message === "symbol") {
- throw new DOMException("Uncloneable value", "DataCloneError");
- }
+ if (rid === null) {
+ // Create the rid immediately, otherwise there is a time window (and a
+ // race condition) where messages can get lost, because recv() is async.
+ rid = ops.op_broadcast_subscribe();
+ recv();
+ }
+ }
- const data = core.serialize(message);
+ postMessage(message) {
+ webidl.assertBranded(this, BroadcastChannelPrototype);
- // Send to other listeners in this VM.
- dispatch(this, this[_name], new Uint8Array(data));
+ const prefix = "Failed to execute 'postMessage' on 'BroadcastChannel'";
+ webidl.requiredArguments(arguments.length, 1, { prefix });
- // Send to listeners in other VMs.
- defer(() => {
- if (!this[_closed]) {
- core.opAsync("op_broadcast_send", rid, this[_name], data);
- }
- });
+ if (this[_closed]) {
+ throw new DOMException("Already closed", "InvalidStateError");
}
- close() {
- webidl.assertBranded(this, BroadcastChannelPrototype);
- this[_closed] = true;
+ if (typeof message === "function" || typeof message === "symbol") {
+ throw new DOMException("Uncloneable value", "DataCloneError");
+ }
- const index = ArrayPrototypeIndexOf(channels, this);
- if (index === -1) return;
+ const data = core.serialize(message);
- ArrayPrototypeSplice(channels, index, 1);
- if (channels.length === 0) {
- ops.op_broadcast_unsubscribe(rid);
+ // Send to other listeners in this VM.
+ dispatch(this, this[_name], new Uint8Array(data));
+
+ // Send to listeners in other VMs.
+ defer(() => {
+ if (!this[_closed]) {
+ core.opAsync("op_broadcast_send", rid, this[_name], data);
}
+ });
+ }
+
+ close() {
+ webidl.assertBranded(this, BroadcastChannelPrototype);
+ this[_closed] = true;
+
+ const index = ArrayPrototypeIndexOf(channels, this);
+ if (index === -1) return;
+
+ ArrayPrototypeSplice(channels, index, 1);
+ if (channels.length === 0) {
+ ops.op_broadcast_unsubscribe(rid);
}
}
+}
- defineEventHandler(BroadcastChannel.prototype, "message");
- defineEventHandler(BroadcastChannel.prototype, "messageerror");
- const BroadcastChannelPrototype = BroadcastChannel.prototype;
+defineEventHandler(BroadcastChannel.prototype, "message");
+defineEventHandler(BroadcastChannel.prototype, "messageerror");
+const BroadcastChannelPrototype = BroadcastChannel.prototype;
- window.__bootstrap.broadcastChannel = { BroadcastChannel };
-})(this);
+export { BroadcastChannel };