From 8f080a81ae3a2d3b6d1b84482348b88969ccae9d Mon Sep 17 00:00:00 2001 From: Viktor Kenyz Date: Mon, 13 May 2024 20:31:51 +0300 Subject: [PATCH 1/5] Keep original isClientTheLeader value on page refresh --- src/libs/ActiveClientManager/index.ts | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/libs/ActiveClientManager/index.ts b/src/libs/ActiveClientManager/index.ts index b364f08508bc..aca0c0267a97 100644 --- a/src/libs/ActiveClientManager/index.ts +++ b/src/libs/ActiveClientManager/index.ts @@ -45,7 +45,10 @@ Onyx.connect({ }, }); +let wasTheLeader = false; + const cleanUpClientId = () => { + wasTheLeader = isClientTheLeader(); activeClients = activeClients.filter((id) => id !== clientID); ActiveClients.setActiveClients(activeClients); }; @@ -66,6 +69,10 @@ const init: Init = () => { * The last GUID is the most recent GUID, so that should be the leader */ const isClientTheLeader: IsClientTheLeader = () => { + if (wasTheLeader) { + return true; + } + const lastActiveClient = activeClients.length && activeClients[activeClients.length - 1]; return lastActiveClient === clientID; From d060f0b080de8b5608d55d1343582683224fb130 Mon Sep 17 00:00:00 2001 From: Viktor Kenyz Date: Mon, 13 May 2024 21:32:17 +0300 Subject: [PATCH 2/5] Linting --- src/libs/ActiveClientManager/index.ts | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/src/libs/ActiveClientManager/index.ts b/src/libs/ActiveClientManager/index.ts index aca0c0267a97..dec78d474476 100644 --- a/src/libs/ActiveClientManager/index.ts +++ b/src/libs/ActiveClientManager/index.ts @@ -47,6 +47,20 @@ Onyx.connect({ let wasTheLeader = false; +/** + * The last GUID is the most recent GUID, so that should be the leader + */ + +const isClientTheLeader: IsClientTheLeader = () => { + if (wasTheLeader) { + return true; + } + + const lastActiveClient = activeClients.length && activeClients[activeClients.length - 1]; + + return lastActiveClient === clientID; +}; + const cleanUpClientId = () => { wasTheLeader = isClientTheLeader(); activeClients = activeClients.filter((id) => id !== clientID); @@ -65,17 +79,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 = () => { - if (wasTheLeader) { - return true; - } - - const lastActiveClient = activeClients.length && activeClients[activeClients.length - 1]; - - return lastActiveClient === clientID; -}; - export {init, isClientTheLeader, isReady}; From 9a6c5d5573d89cde359d0acc97deeca82733f574 Mon Sep 17 00:00:00 2001 From: Viktor Kenyz Date: Mon, 13 May 2024 21:34:40 +0300 Subject: [PATCH 3/5] Linting --- src/libs/ActiveClientManager/index.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/libs/ActiveClientManager/index.ts b/src/libs/ActiveClientManager/index.ts index dec78d474476..120c395c9e3c 100644 --- a/src/libs/ActiveClientManager/index.ts +++ b/src/libs/ActiveClientManager/index.ts @@ -50,7 +50,6 @@ let wasTheLeader = false; /** * The last GUID is the most recent GUID, so that should be the leader */ - const isClientTheLeader: IsClientTheLeader = () => { if (wasTheLeader) { return true; From 850cb0618c62b950483f8446db16eeb21660d8d8 Mon Sep 17 00:00:00 2001 From: Viktor Kenyz Date: Mon, 13 May 2024 22:33:19 +0300 Subject: [PATCH 4/5] Change var name and add comment --- src/libs/ActiveClientManager/index.ts | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/libs/ActiveClientManager/index.ts b/src/libs/ActiveClientManager/index.ts index 120c395c9e3c..9dd7fdcf21a3 100644 --- a/src/libs/ActiveClientManager/index.ts +++ b/src/libs/ActiveClientManager/index.ts @@ -45,13 +45,15 @@ Onyx.connect({ }, }); -let wasTheLeader = false; +let isPromotingNewLeader = false; /** * The last GUID is the most recent GUID, so that should be the leader */ const isClientTheLeader: IsClientTheLeader = () => { - if (wasTheLeader) { + // keep reporting currently leading client on page refresh + // when clientID is already removed from activeClients list + if (isPromotingNewLeader) { return true; } @@ -61,7 +63,7 @@ const isClientTheLeader: IsClientTheLeader = () => { }; const cleanUpClientId = () => { - wasTheLeader = isClientTheLeader(); + isPromotingNewLeader = isClientTheLeader(); activeClients = activeClients.filter((id) => id !== clientID); ActiveClients.setActiveClients(activeClients); }; From c185bdd93b15c4c8f6c122b127ffc5f1afdc254c Mon Sep 17 00:00:00 2001 From: Viktor Kenyz Date: Tue, 14 May 2024 20:08:48 +0300 Subject: [PATCH 5/5] Update comment --- src/libs/ActiveClientManager/index.ts | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/libs/ActiveClientManager/index.ts b/src/libs/ActiveClientManager/index.ts index 9dd7fdcf21a3..69bd3a848e0b 100644 --- a/src/libs/ActiveClientManager/index.ts +++ b/src/libs/ActiveClientManager/index.ts @@ -51,8 +51,13 @@ let isPromotingNewLeader = false; * The last GUID is the most recent GUID, so that should be the leader */ const isClientTheLeader: IsClientTheLeader = () => { - // keep reporting currently leading client on page refresh - // when clientID is already removed from activeClients list + /** + * 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; }