From cb179e505f962f23049ad2b0e8c06efd1ea62bd2 Mon Sep 17 00:00:00 2001
From: Jan Nowakowski <janeknowakowski9@gmail.com>
Date: Mon, 16 Dec 2024 11:54:24 +0100
Subject: [PATCH 1/6] Initialise pusher before rendering children

---
 src/libs/Navigation/AppNavigator/AuthScreens.tsx | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)

diff --git a/src/libs/Navigation/AppNavigator/AuthScreens.tsx b/src/libs/Navigation/AppNavigator/AuthScreens.tsx
index e01d0fe3115f..5c9d788f58d7 100644
--- a/src/libs/Navigation/AppNavigator/AuthScreens.tsx
+++ b/src/libs/Navigation/AppNavigator/AuthScreens.tsx
@@ -260,6 +260,16 @@ function AuthScreens({session, lastOpenedPublicRoomID, initialLastUpdateIDApplie
         };
     }, [theme]);
 
+    const pusherInitialized = useRef(false);
+
+    // eslint-disable-next-line react-compiler/react-compiler
+    if (!pusherInitialized.current) {
+        PusherConnectionManager.init();
+        initializePusher();
+        // eslint-disable-next-line react-compiler/react-compiler
+        pusherInitialized.current = true;
+    }
+
     useEffect(() => {
         const shortcutsOverviewShortcutConfig = CONST.KEYBOARD_SHORTCUTS.SHORTCUTS;
         const searchShortcutConfig = CONST.KEYBOARD_SHORTCUTS.SEARCH;
@@ -277,8 +287,6 @@ function AuthScreens({session, lastOpenedPublicRoomID, initialLastUpdateIDApplie
 
         NetworkConnection.listenForReconnect();
         NetworkConnection.onReconnect(handleNetworkReconnect);
-        PusherConnectionManager.init();
-        initializePusher();
 
         // In Hybrid App we decide to call one of those method when booting ND and we don't want to duplicate calls
         if (!NativeModules.HybridAppModule) {

From fe5c385f6a03521b0ae3b389a72d0b4ef2d240fa Mon Sep 17 00:00:00 2001
From: Jan Nowakowski <janeknowakowski9@gmail.com>
Date: Mon, 16 Dec 2024 16:02:13 +0100
Subject: [PATCH 2/6] Always chain pusher subscriptions on the same promise

---
 src/libs/Navigation/AppNavigator/AuthScreens.tsx | 12 ++----------
 src/libs/Pusher/pusher.ts                        | 11 +++++++----
 2 files changed, 9 insertions(+), 14 deletions(-)

diff --git a/src/libs/Navigation/AppNavigator/AuthScreens.tsx b/src/libs/Navigation/AppNavigator/AuthScreens.tsx
index 5c9d788f58d7..e01d0fe3115f 100644
--- a/src/libs/Navigation/AppNavigator/AuthScreens.tsx
+++ b/src/libs/Navigation/AppNavigator/AuthScreens.tsx
@@ -260,16 +260,6 @@ function AuthScreens({session, lastOpenedPublicRoomID, initialLastUpdateIDApplie
         };
     }, [theme]);
 
-    const pusherInitialized = useRef(false);
-
-    // eslint-disable-next-line react-compiler/react-compiler
-    if (!pusherInitialized.current) {
-        PusherConnectionManager.init();
-        initializePusher();
-        // eslint-disable-next-line react-compiler/react-compiler
-        pusherInitialized.current = true;
-    }
-
     useEffect(() => {
         const shortcutsOverviewShortcutConfig = CONST.KEYBOARD_SHORTCUTS.SHORTCUTS;
         const searchShortcutConfig = CONST.KEYBOARD_SHORTCUTS.SEARCH;
@@ -287,6 +277,8 @@ function AuthScreens({session, lastOpenedPublicRoomID, initialLastUpdateIDApplie
 
         NetworkConnection.listenForReconnect();
         NetworkConnection.onReconnect(handleNetworkReconnect);
+        PusherConnectionManager.init();
+        initializePusher();
 
         // In Hybrid App we decide to call one of those method when booting ND and we don't want to duplicate calls
         if (!NativeModules.HybridAppModule) {
diff --git a/src/libs/Pusher/pusher.ts b/src/libs/Pusher/pusher.ts
index c5bda63b8dba..6f58e64cded1 100644
--- a/src/libs/Pusher/pusher.ts
+++ b/src/libs/Pusher/pusher.ts
@@ -74,7 +74,10 @@ let pusherSocketID = '';
 const socketEventCallbacks: SocketEventCallback[] = [];
 let customAuthorizer: ChannelAuthorizerGenerator;
 
-let initPromise: Promise<void>;
+let resolveInitPromise: (value?: unknown) => void;
+const initPromise = new Promise((resolve) => {
+    resolveInitPromise = resolve;
+});
 
 const eventsBoundToChannels = new Map<Channel, Set<PusherEventName>>();
 
@@ -90,8 +93,9 @@ function callSocketEventCallbacks(eventName: SocketEventName, data?: EventCallba
  * @returns resolves when Pusher has connected
  */
 function init(args: Args, params?: unknown): Promise<void> {
-    initPromise = new Promise((resolve) => {
+    return new Promise((resolve) => {
         if (socket) {
+            resolveInitPromise();
             resolve();
             return;
         }
@@ -131,6 +135,7 @@ function init(args: Args, params?: unknown): Promise<void> {
         socket?.connection.bind('connected', () => {
             pusherSocketID = socket?.connection.socket_id ?? '';
             callSocketEventCallbacks('connected');
+            resolveInitPromise();
             resolve();
         });
 
@@ -142,8 +147,6 @@ function init(args: Args, params?: unknown): Promise<void> {
             callSocketEventCallbacks('state_change', states);
         });
     });
-
-    return initPromise;
 }
 
 /**

From e50ab7e21ca7036936014d00935dfa3f309c5da5 Mon Sep 17 00:00:00 2001
From: Jan Nowakowski <janeknowakowski9@gmail.com>
Date: Mon, 16 Dec 2024 16:24:09 +0100
Subject: [PATCH 3/6] Clear pusher initPromise on disconnect

---
 src/libs/Pusher/pusher.ts | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/src/libs/Pusher/pusher.ts b/src/libs/Pusher/pusher.ts
index 6f58e64cded1..36967fe7f181 100644
--- a/src/libs/Pusher/pusher.ts
+++ b/src/libs/Pusher/pusher.ts
@@ -75,7 +75,7 @@ const socketEventCallbacks: SocketEventCallback[] = [];
 let customAuthorizer: ChannelAuthorizerGenerator;
 
 let resolveInitPromise: (value?: unknown) => void;
-const initPromise = new Promise((resolve) => {
+let initPromise = new Promise((resolve) => {
     resolveInitPromise = resolve;
 });
 
@@ -397,6 +397,9 @@ function disconnect() {
     socket.disconnect();
     socket = null;
     pusherSocketID = '';
+    initPromise = new Promise((resolve) => {
+        resolveInitPromise = resolve;
+    });
 }
 
 /**

From ceebb969da7d4494c1751c88b9bda08d84d96770 Mon Sep 17 00:00:00 2001
From: Jan Nowakowski <janeknowakowski9@gmail.com>
Date: Tue, 17 Dec 2024 08:22:18 +0100
Subject: [PATCH 4/6] Promise types

---
 src/libs/Pusher/pusher.ts | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/src/libs/Pusher/pusher.ts b/src/libs/Pusher/pusher.ts
index 36967fe7f181..f18fcf5d1d95 100644
--- a/src/libs/Pusher/pusher.ts
+++ b/src/libs/Pusher/pusher.ts
@@ -74,8 +74,8 @@ let pusherSocketID = '';
 const socketEventCallbacks: SocketEventCallback[] = [];
 let customAuthorizer: ChannelAuthorizerGenerator;
 
-let resolveInitPromise: (value?: unknown) => void;
-let initPromise = new Promise((resolve) => {
+let resolveInitPromise: () => void;
+let initPromise = new Promise<void>((resolve) => {
     resolveInitPromise = resolve;
 });
 
@@ -93,7 +93,7 @@ function callSocketEventCallbacks(eventName: SocketEventName, data?: EventCallba
  * @returns resolves when Pusher has connected
  */
 function init(args: Args, params?: unknown): Promise<void> {
-    return new Promise((resolve) => {
+    return new Promise<void>((resolve) => {
         if (socket) {
             resolveInitPromise();
             resolve();

From ee98fe0c9d5fc6c4ed901f0a4a18cf5f22f39f8f Mon Sep 17 00:00:00 2001
From: Jan Nowakowski <janeknowakowski9@gmail.com>
Date: Tue, 17 Dec 2024 08:27:11 +0100
Subject: [PATCH 5/6] Chain on init Promise to resolve

---
 src/libs/Pusher/pusher.ts | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)

diff --git a/src/libs/Pusher/pusher.ts b/src/libs/Pusher/pusher.ts
index f18fcf5d1d95..8cdc13134015 100644
--- a/src/libs/Pusher/pusher.ts
+++ b/src/libs/Pusher/pusher.ts
@@ -95,7 +95,6 @@ function callSocketEventCallbacks(eventName: SocketEventName, data?: EventCallba
 function init(args: Args, params?: unknown): Promise<void> {
     return new Promise<void>((resolve) => {
         if (socket) {
-            resolveInitPromise();
             resolve();
             return;
         }
@@ -135,7 +134,6 @@ function init(args: Args, params?: unknown): Promise<void> {
         socket?.connection.bind('connected', () => {
             pusherSocketID = socket?.connection.socket_id ?? '';
             callSocketEventCallbacks('connected');
-            resolveInitPromise();
             resolve();
         });
 
@@ -146,7 +144,7 @@ function init(args: Args, params?: unknown): Promise<void> {
         socket?.connection.bind('state_change', (states: States) => {
             callSocketEventCallbacks('state_change', states);
         });
-    });
+    }).then(resolveInitPromise);
 }
 
 /**

From 5393b3703e2647d9d19628b10b2f4165205b5e47 Mon Sep 17 00:00:00 2001
From: Jan Nowakowski <janeknowakowski9@gmail.com>
Date: Tue, 17 Dec 2024 11:21:16 +0100
Subject: [PATCH 6/6] adjust to new eslint rules

---
 src/libs/Pusher/pusher.ts | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/src/libs/Pusher/pusher.ts b/src/libs/Pusher/pusher.ts
index 8cdc13134015..1dd1628e53d2 100644
--- a/src/libs/Pusher/pusher.ts
+++ b/src/libs/Pusher/pusher.ts
@@ -70,7 +70,7 @@ Onyx.connect({
 });
 
 let socket: PusherWithAuthParams | null;
-let pusherSocketID = '';
+let pusherSocketID: string | undefined;
 const socketEventCallbacks: SocketEventCallback[] = [];
 let customAuthorizer: ChannelAuthorizerGenerator;
 
@@ -132,7 +132,7 @@ function init(args: Args, params?: unknown): Promise<void> {
         });
 
         socket?.connection.bind('connected', () => {
-            pusherSocketID = socket?.connection.socket_id ?? '';
+            pusherSocketID = socket?.connection.socket_id;
             callSocketEventCallbacks('connected');
             resolve();
         });
@@ -414,7 +414,7 @@ function reconnect() {
     socket.connect();
 }
 
-function getPusherSocketID(): string {
+function getPusherSocketID(): string | undefined {
     return pusherSocketID;
 }
 
