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

Prevent outdated connectivity info from updating the online status #58

Merged
Show file tree
Hide file tree
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
24 changes: 21 additions & 3 deletions src/defaults/detectNetwork.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class DetectNetwork {
this._isConnected = null;
this._isConnectionExpensive = null;
this._callback = callback;
this._shouldInitUpdateReach = true;

this._init();
this._addListeners();
Expand Down Expand Up @@ -66,14 +67,26 @@ class DetectNetwork {
}
};
/**
* Fetches and sets the connection reachability and the isConnected props
* Sets the shouldInitUpdateReach flag
* @param {boolean} shouldUpdate - Whether the init method should update the reach prop
* @returns {void}
* @private
*/
_setShouldInitUpdateReach = shouldUpdate => {
this._shouldInitUpdateReach = shouldUpdate;
};
/**
* Fetches and sets the connection reachability and the isConnected props,
* if neither of the AppState and NetInfo event listeners have been called
* @returns {Promise.<void>} Resolves when the props have been
* initialized and update.
* @private
*/
_init = async () => {
const connectionInfo = await NetInfo.getConnectionInfo();
this._update(connectionInfo.type);
if (this._shouldInitUpdateReach) {
this._update(connectionInfo.type);
}
};
/**
* Check changes on props and store and dispatch if neccesary
Expand All @@ -97,9 +110,14 @@ class DetectNetwork {
*/
_addListeners() {
NetInfo.addEventListener('connectionChange', connectionInfo => {
this._setShouldInitUpdateReach(false);
this._update(connectionInfo.type);
});
AppState.addEventListener('change', async () => {
this._setShouldInitUpdateReach(false);
const connectionInfo = await NetInfo.getConnectionInfo();
this._update(connectionInfo.type);
});
AppState.addEventListener('change', this._init);
}

/**
Expand Down
24 changes: 21 additions & 3 deletions src/defaults/detectNetwork.native.legacy.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class LegacyDetectNetwork {
this._isConnected = null;
this._isConnectionExpensive = null;
this._callback = callback;
this._shouldInitUpdateReach = true;

this._init();
this._addListeners();
Expand Down Expand Up @@ -68,14 +69,26 @@ class LegacyDetectNetwork {
}
};
/**
* Fetches and sets the connection reachability and the isConnected props
* Sets the shouldInitUpdateReach flag
* @param {boolean} shouldUpdate - Whether the init method should update the reach prop
* @returns {void}
* @private
*/
_setShouldInitUpdateReach = shouldUpdate => {
this._shouldInitUpdateReach = shouldUpdate;
};
/**
* Fetches and sets the connection reachability and the isConnected props,
* if neither of the AppState and NetInfo event listeners have been called
* @returns {Promise.<void>} Resolves when the props have been
* initialized and update.
* @private
*/
_init = async () => {
const reach = await NetInfo.fetch();
this._update(reach);
if (this._shouldInitUpdateReach) {
this._update(reach);
}
};
/**
* Check changes on props and store and dispatch if neccesary
Expand All @@ -101,9 +114,14 @@ class LegacyDetectNetwork {
*/
_addListeners() {
NetInfo.addEventListener('change', reach => {
this._setShouldInitUpdateReach(false);
this._update(reach);
});
AppState.addEventListener('change', async () => {
this._setShouldInitUpdateReach(false);
const reach = await NetInfo.fetch();
this._update(reach);
});
AppState.addEventListener('change', this._init);
}

/**
Expand Down