Skip to content

Commit

Permalink
fix: examples
Browse files Browse the repository at this point in the history
  • Loading branch information
IsaccoSordo committed May 22, 2024
1 parent 5f1c2a4 commit 64802df
Showing 1 changed file with 49 additions and 9 deletions.
58 changes: 49 additions & 9 deletions src/docs/getting-started/subscribe-to-active-account.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ Sometimes `requestPermissions` may not be enough, and you want to ensure the use
dAppClient.subscribeToEvent(BeaconEvent.ACTIVE_ACCOUNT_SET, async (account) => {
console.log(
`${BeaconEvent.ACTIVE_ACCOUNT_SET} triggered: `,
account?.address
account?.address,
);

if (!account) {
Expand Down Expand Up @@ -108,7 +108,7 @@ wallet.client.subscribeToEvent(
async (account) => {
console.log(
`${BeaconEvent.ACTIVE_ACCOUNT_SET} triggered: `,
account?.address
account?.address,
);

if (!account) {
Expand All @@ -124,7 +124,7 @@ wallet.client.subscribeToEvent(
// The request was rejected
// handle disconnection
}
}
},
);
```

Expand Down Expand Up @@ -182,13 +182,27 @@ const elector = createLeaderElection(channel);
```

Check if a leader already exists, otherwise request leadership.
We also need to handle the case in which the Leader tab gets closed and therefore we need to transfer the leadership to another tab.

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

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

channel.onmessage = async (message: any) => {
if (message === "LEADER_DEAD") {
await elector.awaitLeadership();
}
};
```

### Step 3: Update the Handler
Expand All @@ -209,7 +223,7 @@ Now, inside the handler, check whether the current tab has the leadership. If no
dAppClient.subscribeToEvent(BeaconEvent.ACTIVE_ACCOUNT_SET, async (account) => {
console.log(
`${BeaconEvent.ACTIVE_ACCOUNT_SET} triggered: `,
account?.address
account?.address,
);

if (!account || !elector.isLeader) {
Expand Down Expand Up @@ -237,7 +251,7 @@ wallet.client.subscribeToEvent(
async (account) => {
console.log(
`${BeaconEvent.ACTIVE_ACCOUNT_SET} triggered: `,
account?.address
account?.address,
);

if (!account || !elector.isLeader) {
Expand All @@ -253,7 +267,7 @@ wallet.client.subscribeToEvent(
// The request was rejected
// handle disconnection
}
}
},
);
```

Expand Down Expand Up @@ -288,6 +302,19 @@ elector.hasLeader().then(async (hasLeader) => {
}
});

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

channel.onmessage = async (message: any) => {
if (message === "LEADER_DEAD") {
await elector.awaitLeadership();
}
};

const dAppClient = new DAppClient({
name: "Beacon Docs",
network: {
Expand All @@ -298,7 +325,7 @@ const dAppClient = new DAppClient({
dAppClient.subscribeToEvent(BeaconEvent.ACTIVE_ACCOUNT_SET, async (account) => {
console.log(
`${BeaconEvent.ACTIVE_ACCOUNT_SET} triggered: `,
account?.address
account?.address,
);

if (!account || !elector.isLeader) {
Expand Down Expand Up @@ -333,6 +360,19 @@ elector.hasLeader().then(async (hasLeader) => {
}
});

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

channel.onmessage = async (message: any) => {
if (message === "LEADER_DEAD") {
await elector.awaitLeadership();
}
};

const Tezos = new TezosToolkit("https://mainnet.api.tez.ie");
const wallet = new BeaconWallet({ name: "Beacon Docs" });

Expand All @@ -343,7 +383,7 @@ wallet.client.subscribeToEvent(
async (account) => {
console.log(
`${BeaconEvent.ACTIVE_ACCOUNT_SET} triggered: `,
account?.address
account?.address,
);

if (!account || !elector.isLeader) {
Expand All @@ -359,7 +399,7 @@ wallet.client.subscribeToEvent(
// The request was rejected
// handle disconnection
}
}
},
);
```

Expand Down

0 comments on commit 64802df

Please sign in to comment.