2022-01-07 22:09:52 -05:00
|
|
|
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
|
2021-07-03 05:51:53 -04:00
|
|
|
|
|
|
|
/// <reference path="../../core/internal.d.ts" />
|
|
|
|
|
2021-05-22 12:08:24 -04:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
((window) => {
|
|
|
|
const core = window.Deno.core;
|
2022-08-11 09:56:56 -04:00
|
|
|
const ops = core.ops;
|
2021-05-22 12:08:24 -04:00
|
|
|
const webidl = window.__bootstrap.webidl;
|
2022-10-24 10:14:17 -04:00
|
|
|
const { MessageEvent, defineEventHandler, setTarget } =
|
|
|
|
window.__bootstrap.event;
|
|
|
|
const { EventTarget } = window.__bootstrap.eventTarget;
|
2021-07-03 15:32:28 -04:00
|
|
|
const { DOMException } = window.__bootstrap.domException;
|
2021-07-03 05:51:53 -04:00
|
|
|
const {
|
|
|
|
ArrayPrototypeIndexOf,
|
|
|
|
ArrayPrototypeSplice,
|
|
|
|
ArrayPrototypePush,
|
2022-12-19 21:37:50 -05:00
|
|
|
SafeArrayIterator,
|
2021-07-03 05:51:53 -04:00
|
|
|
Symbol,
|
|
|
|
Uint8Array,
|
|
|
|
} = window.__bootstrap.primordials;
|
2021-05-22 12:08:24 -04:00
|
|
|
|
|
|
|
const _name = Symbol("[[name]]");
|
|
|
|
const _closed = Symbol("[[closed]]");
|
2021-05-22 12:08:24 -04:00
|
|
|
|
|
|
|
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 [name, data] = message;
|
|
|
|
dispatch(null, name, new Uint8Array(data));
|
|
|
|
}
|
|
|
|
|
|
|
|
core.close(rid);
|
|
|
|
rid = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
function dispatch(source, name, data) {
|
2022-12-19 21:37:50 -05:00
|
|
|
for (const channel of new SafeArrayIterator(channels)) {
|
2021-05-22 12:08:24 -04:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
|
|
class BroadcastChannel extends EventTarget {
|
|
|
|
[_name];
|
|
|
|
[_closed] = false;
|
|
|
|
|
|
|
|
get name() {
|
|
|
|
return this[_name];
|
|
|
|
}
|
|
|
|
|
|
|
|
constructor(name) {
|
|
|
|
super();
|
|
|
|
|
2021-06-07 08:19:33 -04:00
|
|
|
const prefix = "Failed to construct 'BroadcastChannel'";
|
2021-05-22 12:08:24 -04:00
|
|
|
webidl.requiredArguments(arguments.length, 1, { prefix });
|
|
|
|
|
|
|
|
this[_name] = webidl.converters["DOMString"](name, {
|
|
|
|
prefix,
|
|
|
|
context: "Argument 1",
|
|
|
|
});
|
|
|
|
|
|
|
|
this[webidl.brand] = webidl.brand;
|
|
|
|
|
2021-07-03 05:51:53 -04:00
|
|
|
ArrayPrototypePush(channels, this);
|
2021-05-22 12:08:24 -04: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.
|
2022-08-11 09:56:56 -04:00
|
|
|
rid = ops.op_broadcast_subscribe();
|
2021-05-22 12:08:24 -04:00
|
|
|
recv();
|
|
|
|
}
|
2021-05-22 12:08:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
postMessage(message) {
|
2022-02-01 12:06:11 -05:00
|
|
|
webidl.assertBranded(this, BroadcastChannelPrototype);
|
2021-05-22 12:08:24 -04:00
|
|
|
|
2021-05-22 12:08:24 -04:00
|
|
|
const prefix = "Failed to execute 'postMessage' on 'BroadcastChannel'";
|
|
|
|
webidl.requiredArguments(arguments.length, 1, { prefix });
|
|
|
|
|
2021-05-22 12:08:24 -04:00
|
|
|
if (this[_closed]) {
|
|
|
|
throw new DOMException("Already closed", "InvalidStateError");
|
|
|
|
}
|
|
|
|
|
2021-05-22 12:08:24 -04:00
|
|
|
if (typeof message === "function" || typeof message === "symbol") {
|
|
|
|
throw new DOMException("Uncloneable value", "DataCloneError");
|
|
|
|
}
|
|
|
|
|
|
|
|
const data = core.serialize(message);
|
|
|
|
|
|
|
|
// Send to other listeners in this VM.
|
|
|
|
dispatch(this, this[_name], new Uint8Array(data));
|
|
|
|
|
|
|
|
// Send to listeners in other VMs.
|
2022-04-04 05:48:29 -04:00
|
|
|
defer(() => core.opAsync("op_broadcast_send", rid, this[_name], data));
|
2021-05-22 12:08:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
close() {
|
2022-02-01 12:06:11 -05:00
|
|
|
webidl.assertBranded(this, BroadcastChannelPrototype);
|
2021-05-22 12:08:24 -04:00
|
|
|
this[_closed] = true;
|
|
|
|
|
2021-07-03 05:51:53 -04:00
|
|
|
const index = ArrayPrototypeIndexOf(channels, this);
|
2021-05-22 12:08:24 -04:00
|
|
|
if (index === -1) return;
|
|
|
|
|
2021-07-03 05:51:53 -04:00
|
|
|
ArrayPrototypeSplice(channels, index, 1);
|
2022-08-11 09:56:56 -04:00
|
|
|
if (channels.length === 0) {
|
|
|
|
ops.op_broadcast_unsubscribe(rid);
|
|
|
|
}
|
2021-05-22 12:08:24 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
defineEventHandler(BroadcastChannel.prototype, "message");
|
|
|
|
defineEventHandler(BroadcastChannel.prototype, "messageerror");
|
2022-02-01 12:06:11 -05:00
|
|
|
const BroadcastChannelPrototype = BroadcastChannel.prototype;
|
2021-05-22 12:08:24 -04:00
|
|
|
|
|
|
|
window.__bootstrap.broadcastChannel = { BroadcastChannel };
|
|
|
|
})(this);
|