diff --git a/Libraries/Network/NetInfo.js b/Libraries/Network/NetInfo.js index 3b15cf541..d98b997ca 100644 --- a/Libraries/Network/NetInfo.js +++ b/Libraries/Network/NetInfo.js @@ -21,18 +21,25 @@ type ChangeEventName = $Enum<{ change: string; }>; +type ReachabilityStateIOS = $Enum<{ + cell: string; + none: string; + unknown: string; + wifi: string; +}>; + /** * NetInfo exposes info about online/offline status * - * == iOS Reachability + * ### reachabilityIOS * * Asyncronously determine if the device is online and on a cellular network. * - * - "none" - device is offline - * - "wifi" - device is online and connected via wifi, or is the iOS simulator - * - "cell" - device is connected via Edge, 3G, WiMax, or LTE - * - "unknown" - error case and the network status is unknown + * - `none` - device is offline + * - `wifi` - device is online and connected via wifi, or is the iOS simulator + * - `cell` - device is connected via Edge, 3G, WiMax, or LTE + * - `unknown` - error case and the network status is unknown * * ``` * NetInfo.reachabilityIOS.fetch().done((reach) => { @@ -50,11 +57,37 @@ type ChangeEventName = $Enum<{ * handleFirstReachabilityChange * ); * ``` + * + * ### isConnected + * + * Available on all platforms. Asyncronously fetch a boolean to determine + * internet connectivity. + * + * ``` + * NetInfo.isConnected.fetch().done((isConnected) => { + * console.log('First, is ' + (isConnected ? 'online' : 'offline')); + * }); + * function handleFirstConnectivityChange(isConnected) { + * console.log('Then, is ' + (isConnected ? 'online' : 'offline')); + * NetInfo.isConnected.removeEventListener( + * 'change', + * handleFirstConnectivityChange + * ); + * } + * NetInfo.isConnected.addEventListener( + * 'change', + * handleFirstConnectivityChange + * ); + * ``` */ var NetInfo = {}; if (RCTReachability) { + + // RCTReachability is exposed, so this is an iOS-like environment and we will + // expose reachabilityIOS + var _reachabilitySubscriptions = {}; NetInfo.reachabilityIOS = { @@ -84,7 +117,7 @@ if (RCTReachability) { fetch: function(): Promise { return new Promise((resolve, reject) => { RCTReachability.getCurrentReachability( - (resp) => { + function(resp) { resolve(resp.network_reachability); }, reject @@ -93,53 +126,42 @@ if (RCTReachability) { }, }; - /** - * - * == NetInfo.isConnected - * - * Available on all platforms. Asyncronously fetch a boolean to determine - * internet connectivity. - * - * ``` - * NetInfo.isConnected.fetch().done((isConnected) => { - * console.log('First, is ' + (isConnected ? 'online' : 'offline')); - * }); - * function handleFirstConnectivityChange(isConnected) { - * console.log('Then, is ' + (isConnected ? 'online' : 'offline')); - * NetInfo.isConnected.removeEventListener( - * 'change', - * handleFirstConnectivityChange - * ); - * } - * NetInfo.isConnected.addEventListener( - * 'change', - * handleFirstConnectivityChange - * ); - * ``` - * - */ var _isConnectedSubscriptions = {}; + + var _iosReachabilityIsConnected = function( + reachability: ReachabilityStateIOS + ): bool { + return reachability !== 'none' && + reachability !== 'unknown'; + }; + NetInfo.isConnected = { addEventListener: function ( eventName: ChangeEventName, handler: Function ): void { _isConnectedSubscriptions[handler] = (reachability) => { - handler(reachability !== 'none'); + handler(_iosReachabilityIsConnected(reachability)); }; - NetInfo.reachabilityIOS.addEventListener(eventName, _isConnectedSubscriptions[handler]); + NetInfo.reachabilityIOS.addEventListener( + eventName, + _isConnectedSubscriptions[handler] + ); }, removeEventListener: function( eventName: ChangeEventName, handler: Function ): void { - NetInfo.reachabilityIOS.removeEventListener(eventName, _isConnectedSubscriptions[handler]); + NetInfo.reachabilityIOS.removeEventListener( + eventName, + _isConnectedSubscriptions[handler] + ); }, fetch: function(): Promise { return NetInfo.reachabilityIOS.fetch().then( - (reachability) => reachability !== 'none' + (reachability) => _iosReachabilityIsConnected(reachability) ); }, };