Goal#
Implement the BroadcastChannel interface so documents and workers within the same origin can communicate via a named channel, reusing the Phase 19 structured clone and worker message-channel infrastructure.
Depends on: Phase 19 (Web Workers — already complete).
Scope#
BroadcastChannelinterfacenew BroadcastChannel(name: string)— creates or joins the named channelpostMessage(message)— structured-clones the message and dispatchesMessageEventon every otherBroadcastChannelwith the same name in the same originclose()— removes this channel instance from the registry- Properties:
name - Event handlers:
onmessage,onmessageerrorplusaddEventListenerequivalents
- Origin scoping
- Two
BroadcastChannelinstances communicate only when they are in browsing contexts (documents or workers) with the same origin - Closed channels do not receive or emit
- Two
- Cross-thread delivery
- When the document and a
Workeropen the same channel, messages cross thread boundaries via the existing worker message-channel infrastructure - Use the Phase 19 structured clone implementation (
crates/js/src/structured_clone.rs) for serialization - Non-cloneable values fire
messageerroron receivers (do not abort the sender)
- When the document and a
- Registry
- Per-event-loop registry keyed by
(origin, name) - Workers and documents register/unregister on construction/close (and on context destruction)
- Per-event-loop registry keyed by
- Lifecycle
- GC root channels with non-empty
onmessage/onmessageerrorhandlers or pending listeners - Closing a context closes all its channels
- GC root channels with non-empty
Acceptance Criteria#
- Two documents in the same origin can exchange messages via the same channel name
- A document and a
Workerexchange messages over aBroadcastChannel - The sender does not receive its own
postMessage(per spec) nameis the value passed to the constructor (or coerced to string)- After
close(), no further messages are received andpostMessagethrowsInvalidStateError - Non-cloneable values fire
messageerroron listening channels, not on the sender - Origin isolation: a different-origin channel with the same name does not see the messages
- Unit tests covering: same-context dispatch, cross-worker dispatch, sender exclusion, close semantics, origin isolation, structured-clone error path
- Conventions
cargo fmt --all --checkcargo clippy --workspace -- -D warningscargo test --workspace