Skip to content

Commit

Permalink
fix: guide (#56)
Browse files Browse the repository at this point in the history
  • Loading branch information
IsaccoSordo authored Jul 23, 2024
1 parent aafd73f commit 4bd665a
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/docs/getting-started/subscribe-to-active-account.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,9 @@ The main idea is to elect a tab as the Leader so that only this tab will send a
```ts
const channel = new BroadcastChannel("beacon-channel"); // "beacon-channel" is an example, you can choose any name you want
const elector = createLeaderElection(channel);
// Auxiliary variable needed for handling beforeUnload.
// Closing a tab causes the elector to be killed immediately
let wasLeader: boolean = false;
```

Check if a leader already exists, otherwise request leadership.
Expand All @@ -188,13 +191,14 @@ We also need to handle the case in which the Leader tab gets closed and therefor
elector.hasLeader().then(async (hasLeader) => {
if (!hasLeader) {
await elector.awaitLeadership();
wasLeader = elector.isLeader;
}
});

// NOTE: If you are using a JS framework, do not call window.onbeforeunload directly.
// Refer to your framework's guidelines for handling this scenario.
window.onbeforeunload = async () => {
if (elector.isLeader) {
if (wasLeader) {
await elector.die();
channel.postMessage("LEADER_DEAD");
}
Expand Down Expand Up @@ -297,15 +301,17 @@ import { BroadcastChannel, createLeaderElection } from "broadcast-channel";

const channel = new BroadcastChannel("beacon-test");
const elector = createLeaderElection(channel);
let wasLeader: boolean = false;

elector.hasLeader().then(async (hasLeader) => {
if (!hasLeader) {
await elector.awaitLeadership();
wasLeader = elector.isLeader;
}
});

window.onbeforeunload = async () => {
if (elector.isLeader) {
if (wasLeader) {
await elector.die();
channel.postMessage("LEADER_DEAD");
}
Expand Down Expand Up @@ -354,18 +360,21 @@ dAppClient.requestPermissions();
```ts
import { TezosToolkit } from "@taquito/taquito";
import { BeaconWallet } from "@taquito/beacon-wallet";
import { BroadcastChannel, createLeaderElection } from "broadcast-channel";

const channel = new BroadcastChannel("beacon-test");
const elector = createLeaderElection(channel);
let wasLeader: boolean = false;

elector.hasLeader().then(async (hasLeader) => {
if (!hasLeader) {
await elector.awaitLeadership();
wasLeader = elector.isLeader;
}
});

window.onbeforeunload = async () => {
if (elector.isLeader) {
if (wasLeader) {
await elector.die();
channel.postMessage("LEADER_DEAD");
}
Expand Down

0 comments on commit 4bd665a

Please sign in to comment.