Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function subscribeBroadcastChannel(
topic: string,
onMessage: (m: T) => void,
onLeader: () => void): BroadcastChannelHandle {
const channel = new BroadcastChannel(topic);
channel.onmessage = (m) => onMessage(m);
const elector = LeaderElection.create(channel);
elector.awaitLeadership().then(() => onLeader())
return {
topic,
channel,
elector,
onMessage,
onLeader
}
}
function unsubscribeBroadcastChannel(handle: BroadcastChannelHandle) {
function LeaderElector(database) {
this.destroyed = false;
this.isLeader = false;
this.isDead = false;
this.database = database;
this.elector = createLeaderElection(database.broadcastChannel);
}
constructor(database) {
this.destroyed = false;
this.database = database;
this.isLeader = false;
this.isDead = false;
this.elector = LeaderElection.create(database.broadcastChannel);
}