From aa4fa79c657c34c842452b1201302dab6139fb04 Mon Sep 17 00:00:00 2001 From: Jules Rosser Date: Wed, 7 Oct 2020 14:57:19 +0100 Subject: [PATCH 1/2] fix logout, by passing reference when removing event --- src/lib/NetworkConnection.js | 36 ++++++++++++++++++++---------------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/src/lib/NetworkConnection.js b/src/lib/NetworkConnection.js index 0e20f2f76a756..d593af4887b4c 100644 --- a/src/lib/NetworkConnection.js +++ b/src/lib/NetworkConnection.js @@ -22,6 +22,24 @@ const triggerReconnectionCallbacks = _.throttle(() => { _.each(reconnectionCallbacks, callback => callback()); }, 5000, {trailing: false}); +/** + * When the app is in the background Pusher can still receive realtime updates + * for a few minutes, but eventually disconnects causing a delay when the app + * returns from the background. So, if we are returning from the background + * and we are online we should trigger our reconnection callbacks. + */ +const reconnectListener = (state) => { + console.debug('[AppState] state changed:', state); + const nextStateIsActive = state === 'active'; + + // We are moving from not active to active and we are online so fire callbacks + if (!isOffline && nextStateIsActive && !isActive) { + triggerReconnectionCallbacks(); + } + + isActive = nextStateIsActive; +}; + /** * Called when the offline status of the app changes and if the network is "reconnecting" (going from offline to online) * then all of the reconnection callbacks are triggered @@ -53,21 +71,7 @@ function listenForReconnect() { setOfflineStatus(!state.isConnected); }); - // When the app is in the background Pusher can still receive realtime updates - // for a few minutes, but eventually disconnects causing a delay when the app - // returns from the background. So, if we are returning from the background - // and we are online we should trigger our reconnection callbacks. - AppState.addEventListener('change', (state) => { - console.debug('[AppState] state changed:', state); - const nextStateIsActive = state === 'active'; - - // We are moving from not active to active and we are online so fire callbacks - if (!isOffline && nextStateIsActive && !isActive) { - triggerReconnectionCallbacks(); - } - - isActive = nextStateIsActive; - }); + AppState.addEventListener('change', reconnectListener); // When a device is put to sleep, NetInfo is not always able to detect // when connectivity has been lost. As a failsafe we will capture the time @@ -91,7 +95,7 @@ function stopListeningForReconnect() { if (unsubscribeFromNetInfo) { unsubscribeFromNetInfo(); } - AppState.removeEventListener('change', () => {}); + AppState.removeEventListener('change', reconnectListener); } /** From cea01bff7a658b0f17907c339a30db9e10c094e7 Mon Sep 17 00:00:00 2001 From: Jules Rosser Date: Wed, 7 Oct 2020 15:24:40 +0100 Subject: [PATCH 2/2] add JSDoc for param --- src/lib/NetworkConnection.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/lib/NetworkConnection.js b/src/lib/NetworkConnection.js index d593af4887b4c..99502ef229e17 100644 --- a/src/lib/NetworkConnection.js +++ b/src/lib/NetworkConnection.js @@ -27,6 +27,8 @@ const triggerReconnectionCallbacks = _.throttle(() => { * for a few minutes, but eventually disconnects causing a delay when the app * returns from the background. So, if we are returning from the background * and we are online we should trigger our reconnection callbacks. + * + * @param {AppState} state */ const reconnectListener = (state) => { console.debug('[AppState] state changed:', state);