Skip to content

Commit

Permalink
Merge pull request #609 from Expensify/marcaaron-fixLoggingOut
Browse files Browse the repository at this point in the history
Register the AppState listener handler correctly
  • Loading branch information
tgolen authored Oct 7, 2020
2 parents 0b99e2e + cd58050 commit b3d7481
Showing 1 changed file with 17 additions and 12 deletions.
29 changes: 17 additions & 12 deletions src/lib/NetworkConnection.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,21 @@ function setOfflineStatus(isCurrentlyOffline) {
isOffline = isCurrentlyOffline;
}

/**
* @param {String} state
*/
function setAppState(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;
}

/**
* Set up the event listener for NetInfo to tell whether the user has
* internet connectivity or not. This is more reliable than the Pusher
Expand All @@ -57,17 +72,7 @@ function listenForReconnect() {
// 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', setAppState);

// 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
Expand All @@ -91,7 +96,7 @@ function stopListeningForReconnect() {
if (unsubscribeFromNetInfo) {
unsubscribeFromNetInfo();
}
AppState.removeEventListener('change', () => {});
AppState.removeEventListener('change', setAppState);
}

/**
Expand Down

0 comments on commit b3d7481

Please sign in to comment.