Skip to content

Commit

Permalink
$mol_bus - broadcast channel
Browse files Browse the repository at this point in the history
  • Loading branch information
jin committed Dec 29, 2023
1 parent 1a1f913 commit aefb503
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions bus/bus.ts
Original file line number Diff line number Diff line change
@@ -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 )
}

}
}

0 comments on commit aefb503

Please sign in to comment.