From 16933a4627d5531b755abed61f0bdfa06241b9a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Birger=20Na=C3=9F?= Date: Mon, 30 Oct 2017 15:52:16 +0100 Subject: [PATCH] Add a flag to prevent outdated connectivity info from updating the online status --- src/defaults/detectNetwork.native.js | 24 ++++++++++++++++++--- src/defaults/detectNetwork.native.legacy.js | 24 ++++++++++++++++++--- 2 files changed, 42 insertions(+), 6 deletions(-) diff --git a/src/defaults/detectNetwork.native.js b/src/defaults/detectNetwork.native.js index 40e2bab77..e51e5ab7a 100644 --- a/src/defaults/detectNetwork.native.js +++ b/src/defaults/detectNetwork.native.js @@ -8,6 +8,7 @@ class DetectNetwork { this._isConnected = null; this._isConnectionExpensive = null; this._callback = callback; + this._shouldInitUpdateReach = true; this._init(); this._addListeners(); @@ -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.} 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 @@ -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); } /** diff --git a/src/defaults/detectNetwork.native.legacy.js b/src/defaults/detectNetwork.native.legacy.js index 2d88f3b58..0733da3c0 100644 --- a/src/defaults/detectNetwork.native.legacy.js +++ b/src/defaults/detectNetwork.native.legacy.js @@ -7,6 +7,7 @@ class LegacyDetectNetwork { this._isConnected = null; this._isConnectionExpensive = null; this._callback = callback; + this._shouldInitUpdateReach = true; this._init(); this._addListeners(); @@ -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.} 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 @@ -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); } /**