Skip to content

Commit

Permalink
fix: leader unload
Browse files Browse the repository at this point in the history
  • Loading branch information
IsaccoSordo committed Sep 19, 2024
1 parent ffc252b commit e2cbbf6
Showing 1 changed file with 31 additions and 11 deletions.
42 changes: 31 additions & 11 deletions packages/beacon-core/src/utils/multi-tab-channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import { BeaconMessageType } from '@airgap/beacon-types'
type BCMessageType =
| 'REQUEST_LEADERSHIP'
| 'LEADER_EXISTS'
| 'LEADER_DEAD'
| 'LEADER_UNLOAD'
| 'LEADER_STILL_ALIVE'
| 'IS_LEADER_ALIVE'
| 'CHILD_UNLOAD'
| 'CHILD_STILL_ALIVE'
| 'IS_CHILD_ALIVE'
Expand Down Expand Up @@ -54,12 +56,10 @@ export class MultiTabChannel {
private onBeforeUnloadHandler() {
if (this.isLeader) {
this.postMessage({
type: 'LEADER_DEAD',
type: 'LEADER_UNLOAD',
recipient: this.neighborhood[this.chooseNextLeader()],
data: this.neighborhood
})
this.neighborhood.splice(0)
this.isLeader = false
} else {
this.postMessage({ type: 'CHILD_UNLOAD' })
}
Expand Down Expand Up @@ -93,22 +93,42 @@ export class MultiTabChannel {
return
}

if (data.type === 'CHILD_STILL_ALIVE' && this.isLeader) {
clearTimeout(this.pendingACKs.get(data.sender))
this.pendingACKs.delete(data.sender)
if (data.type === 'CHILD_STILL_ALIVE') {
if (this.isLeader) {
clearTimeout(this.pendingACKs.get(data.sender))
this.pendingACKs.delete(data.sender)
}
}

if (data.type === 'IS_CHILD_ALIVE') {
data.recipient === this.id && this.postMessage({ type: 'CHILD_STILL_ALIVE' })
return
}

if (data.type === 'LEADER_DEAD') {
if (data.type === 'LEADER_UNLOAD') {
if (data.recipient === this.id) {
this.isLeader = true
this.neighborhood = data.data.filter((id: string) => this.id !== id)
this.onElectedLeaderHandler()
this.pendingACKs.set(
data.sender,
setTimeout(() => {
this.isLeader = true
this.neighborhood = data.data.filter((id: string) => this.id !== id)
this.onElectedLeaderHandler()
}, 1000)
)
}
this.postMessage({ type: 'IS_LEADER_ALIVE', recipient: data.sender })
return
}

if (data.type === 'LEADER_STILL_ALIVE') {
if (this.isLeader) {
clearTimeout(this.pendingACKs.get(data.sender))
this.pendingACKs.delete(data.sender)
}
}

if (data.type === 'IS_LEADER_ALIVE') {
data.recipient === this.id && this.postMessage({ type: 'CHILD_STILL_ALIVE' })
return
}

Expand Down

0 comments on commit e2cbbf6

Please sign in to comment.