Skip to content

Commit

Permalink
Merge pull request #42095 from Skakruk/42062-login-title-on-refresh
Browse files Browse the repository at this point in the history
[CP Staging] Keep original isClientTheLeader value on page refresh

(cherry picked from commit 8a5bc9c)
  • Loading branch information
tgolen authored and OSBotify committed May 14, 2024
1 parent e1ed8f1 commit b5f3804
Showing 1 changed file with 23 additions and 9 deletions.
32 changes: 23 additions & 9 deletions src/libs/ActiveClientManager/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,30 @@ Onyx.connect({
},
});

let isPromotingNewLeader = false;

/**
* The last GUID is the most recent GUID, so that should be the leader
*/
const isClientTheLeader: IsClientTheLeader = () => {
/**
* When a new leader is being promoted, there is a brief period during which the current leader's clientID
* is removed from the activeClients list due to asynchronous operations, but the new leader has not officially
* taken over yet. This can result in a situation where, upon page refresh, multiple leaders are being reported.
* This early return statement here will prevent that from happening by maintaining the current leader as
* the 'active leader' until the other leader is fully promoted.
*/
if (isPromotingNewLeader) {
return true;
}

const lastActiveClient = activeClients.length && activeClients[activeClients.length - 1];

return lastActiveClient === clientID;
};

const cleanUpClientId = () => {
isPromotingNewLeader = isClientTheLeader();
activeClients = activeClients.filter((id) => id !== clientID);
ActiveClients.setActiveClients(activeClients);
};
Expand All @@ -62,13 +85,4 @@ const init: Init = () => {
window.addEventListener('beforeunload', cleanUpClientId);
};

/**
* The last GUID is the most recent GUID, so that should be the leader
*/
const isClientTheLeader: IsClientTheLeader = () => {
const lastActiveClient = activeClients.length && activeClients[activeClients.length - 1];

return lastActiveClient === clientID;
};

export {init, isClientTheLeader, isReady};

0 comments on commit b5f3804

Please sign in to comment.