Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Register the AppState listener handler correctly #609

Merged
merged 1 commit into from
Oct 7, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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