diff --git a/src/lib/NetworkConnection.js b/src/lib/NetworkConnection.js index 0e20f2f76a756..99502ef229e17 100644 --- a/src/lib/NetworkConnection.js +++ b/src/lib/NetworkConnection.js @@ -22,6 +22,26 @@ 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. + * + * @param {AppState} state + */ +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 +73,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 +97,7 @@ function stopListeningForReconnect() { if (unsubscribeFromNetInfo) { unsubscribeFromNetInfo(); } - AppState.removeEventListener('change', () => {}); + AppState.removeEventListener('change', reconnectListener); } /**