From aefb5035432167069acfe26cebe9cd5cb3515195 Mon Sep 17 00:00:00 2001 From: jin Date: Sat, 30 Dec 2023 00:22:50 +0300 Subject: [PATCH] $mol_bus - broadcast channel --- bus/bus.ts | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 bus/bus.ts diff --git a/bus/bus.ts b/bus/bus.ts new file mode 100644 index 00000000000..83d7c020202 --- /dev/null +++ b/bus/bus.ts @@ -0,0 +1,25 @@ +namespace $ { + export class $mol_bus< Data > extends $mol_object { + + readonly channel: BroadcastChannel + + constructor( + readonly name: string, + readonly handle: ( data: Data )=> void + ) { + super() + const channel = new BroadcastChannel( name ) + channel.onmessage = ( event: MessageEvent< Data > )=> this.handle( event.data ) + this.channel = channel + } + + destructor() { + this.channel.close() + } + + send( data: Data ) { + this.channel.postMessage( data ) + } + + } +}