UNPKG

664 BPlain TextView Raw
1import type { EventBasedChannel } from 'async-call-rpc'
2
3/**
4 * BroadcastChannel support for AsyncCall.
5 * Please make sure your serializer can convert JSON RPC payload into one of the following data types:
6 * - Data that can be [structure cloned](http://mdn.io/structure-clone)
7 */
8export class BroadcastMessageChannel extends BroadcastChannel implements EventBasedChannel {
9 on(eventListener: (data: unknown) => void) {
10 const f = (e: MessageEvent): void => eventListener(e.data)
11 this.addEventListener('message', f)
12 return () => this.removeEventListener('message', f)
13 }
14 send(data: any) {
15 super.postMessage(data)
16 }
17}