-
Notifications
You must be signed in to change notification settings - Fork 5.4k
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
feat(extensions): Broadcast Channel API #10527
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license. | ||
import { assertEquals } from "../../../test_util/std/testing/asserts.ts"; | ||
import { deferred } from "../../../test_util/std/async/deferred.ts"; | ||
|
||
Deno.test("broadcastchannel worker", async () => { | ||
const intercom = new BroadcastChannel("intercom"); | ||
let count = 0; | ||
|
||
const url = new URL("../workers/broadcast_channel.ts", import.meta.url); | ||
const worker = new Worker(url.href, { type: "module", name: "worker" }); | ||
worker.onmessage = () => intercom.postMessage(++count); | ||
|
||
const promise = deferred(); | ||
|
||
intercom.onmessage = function (e) { | ||
assertEquals(count, e.data); | ||
if (count < 42) { | ||
intercom.postMessage(++count); | ||
} else { | ||
worker.terminate(); | ||
intercom.close(); | ||
promise.resolve(); | ||
} | ||
}; | ||
|
||
await promise; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
new BroadcastChannel("intercom").onmessage = function (e) { | ||
this.postMessage(e.data); | ||
}; | ||
|
||
self.postMessage("go"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,167 @@ | ||
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license. | ||
"use strict"; | ||
|
||
((window) => { | ||
const core = window.Deno.core; | ||
const webidl = window.__bootstrap.webidl; | ||
const { setTarget } = window.__bootstrap.event; | ||
|
||
const handlerSymbol = Symbol("eventHandlers"); | ||
function makeWrappedHandler(handler) { | ||
function wrappedHandler(...args) { | ||
if (typeof wrappedHandler.handler !== "function") { | ||
return; | ||
} | ||
return wrappedHandler.handler.call(this, ...args); | ||
} | ||
wrappedHandler.handler = handler; | ||
return wrappedHandler; | ||
} | ||
// TODO(lucacasonato) reuse when we can reuse code between web crates | ||
function defineEventHandler(emitter, name) { | ||
// HTML specification section 8.1.5.1 | ||
Object.defineProperty(emitter, `on${name}`, { | ||
get() { | ||
// TODO(bnoordhuis) The "BroadcastChannel should have an onmessage | ||
// event" WPT test expects that .onmessage !== undefined. Returning | ||
// null makes it pass but is perhaps not exactly in the spirit. | ||
return this[handlerSymbol]?.get(name)?.handler ?? null; | ||
}, | ||
set(value) { | ||
if (!this[handlerSymbol]) { | ||
this[handlerSymbol] = new Map(); | ||
} | ||
let handlerWrapper = this[handlerSymbol]?.get(name); | ||
if (handlerWrapper) { | ||
handlerWrapper.handler = value; | ||
} else { | ||
handlerWrapper = makeWrappedHandler(value); | ||
this.addEventListener(name, handlerWrapper); | ||
} | ||
this[handlerSymbol].set(name, handlerWrapper); | ||
}, | ||
configurable: true, | ||
enumerable: true, | ||
}); | ||
} | ||
|
||
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 [name, data] = message; | ||
dispatch(null, name, new Uint8Array(data)); | ||
} | ||
|
||
core.close(rid); | ||
rid = null; | ||
} | ||
|
||
function dispatch(source, name, data) { | ||
for (const channel of channels) { | ||
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); | ||
} | ||
|
||
class BroadcastChannel extends EventTarget { | ||
[_name]; | ||
[_closed] = false; | ||
|
||
get name() { | ||
return this[_name]; | ||
} | ||
|
||
constructor(name) { | ||
super(); | ||
|
||
const prefix = "Failed to construct 'broadcastChannel'"; | ||
webidl.requiredArguments(arguments.length, 1, { prefix }); | ||
|
||
this[_name] = webidl.converters["DOMString"](name, { | ||
prefix, | ||
context: "Argument 1", | ||
}); | ||
|
||
this[webidl.brand] = webidl.brand; | ||
|
||
channels.push(this); | ||
|
||
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 = core.opSync("op_broadcast_subscribe"); | ||
recv(); | ||
} | ||
} | ||
|
||
postMessage(message) { | ||
webidl.assertBranded(this, BroadcastChannel); | ||
|
||
const prefix = "Failed to execute 'postMessage' on 'BroadcastChannel'"; | ||
webidl.requiredArguments(arguments.length, 1, { prefix }); | ||
|
||
if (this[_closed]) { | ||
throw new DOMException("Already closed", "InvalidStateError"); | ||
} | ||
|
||
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)); | ||
lucacasonato marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// Send to listeners in other VMs. | ||
defer(() => core.opAsync("op_broadcast_send", [rid, this[_name]], data)); | ||
} | ||
|
||
close() { | ||
webidl.assertBranded(this, BroadcastChannel); | ||
this[_closed] = true; | ||
|
||
const index = channels.indexOf(this); | ||
if (index === -1) return; | ||
|
||
channels.splice(index, 1); | ||
if (channels.length === 0) core.opSync("op_broadcast_unsubscribe", rid); | ||
} | ||
} | ||
|
||
defineEventHandler(BroadcastChannel.prototype, "message"); | ||
defineEventHandler(BroadcastChannel.prototype, "messageerror"); | ||
|
||
window.__bootstrap.broadcastChannel = { BroadcastChannel }; | ||
})(this); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# Copyright 2018-2021 the Deno authors. All rights reserved. MIT license. | ||
|
||
[package] | ||
name = "deno_broadcast_channel" | ||
version = "0.1.0" | ||
edition = "2018" | ||
description = "Implementation of BroadcastChannel API for Deno" | ||
authors = ["the Deno authors"] | ||
license = "MIT" | ||
readme = "README.md" | ||
repository = "https://github.com/denoland/deno" | ||
|
||
[lib] | ||
path = "lib.rs" | ||
|
||
[dependencies] | ||
async-trait = "0.1" | ||
deno_core = { version = "0.88.0", path = "../../core" } | ||
tokio = { version = "1.4.0", features = ["full"] } | ||
uuid = { version = "0.8.2", features = ["v4"] } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# deno_broadcast_channel | ||
|
||
This crate implements the BroadcastChannel functions of Deno. | ||
|
||
Spec: https://html.spec.whatwg.org/multipage/web-messaging.html |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I observe that with
queueMicrotask()
e.g.setTimeout()
callbacks don't run when ping-ponging messages over a BroadcastChannel.