2023-01-02 16:00:42 -05:00
|
|
|
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
2021-07-03 05:51:53 -04:00
|
|
|
|
|
|
|
/// <reference path="../../core/internal.d.ts" />
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
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;
|
2021-05-22 12:08:24 -04:00
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
const { 0: name, 1: data } = message;
|
|
|
|
dispatch(null, name, new Uint8Array(data));
|
2021-05-22 12:08:24 -04:00
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
core.close(rid);
|
|
|
|
rid = null;
|
|
|
|
}
|
2021-05-22 12:08:24 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
function dispatch(source, name, data) {
|
|
|
|
for (let i = 0; i < channels.length; ++i) {
|
|
|
|
const channel = channels[i];
|
2021-05-22 12:08:24 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
if (channel === source) continue; // Don't self-send.
|
|
|
|
if (channel[_name] !== name) continue;
|
|
|
|
if (channel[_closed]) continue;
|
2021-05-22 12:08:24 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
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);
|
|
|
|
};
|
2021-05-22 12:08:24 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
defer(go);
|
|
|
|
}
|
|
|
|
}
|
2021-05-22 12:08:24 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 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);
|
|
|
|
}
|
2021-05-22 12:08:24 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
class BroadcastChannel extends EventTarget {
|
|
|
|
[_name];
|
|
|
|
[_closed] = false;
|
2021-05-22 12:08:24 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
get name() {
|
|
|
|
return this[_name];
|
|
|
|
}
|
2021-05-22 12:08:24 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
constructor(name) {
|
|
|
|
super();
|
2021-05-22 12:08:24 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
const prefix = "Failed to construct 'BroadcastChannel'";
|
|
|
|
webidl.requiredArguments(arguments.length, 1, { prefix });
|
2021-05-22 12:08:24 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
this[_name] = webidl.converters["DOMString"](name, {
|
|
|
|
prefix,
|
|
|
|
context: "Argument 1",
|
|
|
|
});
|
2021-05-22 12:08:24 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
this[webidl.brand] = webidl.brand;
|
2021-05-22 12:08:24 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
ArrayPrototypePush(channels, this);
|
2021-05-22 12:08:24 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
2021-05-22 12:08:24 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
postMessage(message) {
|
|
|
|
webidl.assertBranded(this, BroadcastChannelPrototype);
|
2021-05-22 12:08:24 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
const prefix = "Failed to execute 'postMessage' on 'BroadcastChannel'";
|
|
|
|
webidl.requiredArguments(arguments.length, 1, { prefix });
|
2021-05-22 12:08:24 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
if (this[_closed]) {
|
|
|
|
throw new DOMException("Already closed", "InvalidStateError");
|
2021-05-22 12:08:24 -04:00
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
if (typeof message === "function" || typeof message === "symbol") {
|
|
|
|
throw new DOMException("Uncloneable value", "DataCloneError");
|
|
|
|
}
|
2021-05-22 12:08:24 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
const data = core.serialize(message);
|
2021-05-22 12:08:24 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 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);
|
2022-08-11 09:56:56 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2021-05-22 12:08:24 -04:00
|
|
|
}
|
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
2021-05-22 12:08:24 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
defineEventHandler(BroadcastChannel.prototype, "message");
|
|
|
|
defineEventHandler(BroadcastChannel.prototype, "messageerror");
|
|
|
|
const BroadcastChannelPrototype = BroadcastChannel.prototype;
|
2021-05-22 12:08:24 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
export { BroadcastChannel };
|