diff --git a/Libraries/Network/NetInfo.js b/Libraries/Network/NetInfo.js deleted file mode 100644 index b913d527be..0000000000 --- a/Libraries/Network/NetInfo.js +++ /dev/null @@ -1,240 +0,0 @@ -/** - * Copyright (c) Facebook, Inc. and its affiliates. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - * - * @format - * @flow - */ - -'use strict'; - -const NativeEventEmitter = require('../EventEmitter/NativeEventEmitter'); -const NativeModules = require('../BatchedBridge/NativeModules'); -const Platform = require('../Utilities/Platform'); -const RCTNetInfo = NativeModules.NetInfo; - -const NetInfoEventEmitter = new NativeEventEmitter(RCTNetInfo); - -const DEVICE_CONNECTIVITY_EVENT = 'networkStatusDidChange'; - -type ChangeEventName = $Keys<{ - connectionChange: string, - change: string, -}>; - -type ReachabilityStateIOS = $Keys<{ - cell: string, - none: string, - unknown: string, - wifi: string, -}>; - -type ConnectivityStateAndroid = $Keys<{ - NONE: string, - MOBILE: string, - WIFI: string, - MOBILE_MMS: string, - MOBILE_SUPL: string, - MOBILE_DUN: string, - MOBILE_HIPRI: string, - WIMAX: string, - BLUETOOTH: string, - DUMMY: string, - ETHERNET: string, - MOBILE_FOTA: string, - MOBILE_IMS: string, - MOBILE_CBS: string, - WIFI_P2P: string, - MOBILE_IA: string, - MOBILE_EMERGENCY: string, - PROXY: string, - VPN: string, - UNKNOWN: string, -}>; - -const _subscriptions = new Map(); - -let _isConnectedDeprecated; -if (Platform.OS === 'ios') { - _isConnectedDeprecated = function( - reachability: ReachabilityStateIOS, - ): boolean { - return reachability !== 'none' && reachability !== 'unknown'; - }; -} else if (Platform.OS === 'android') { - _isConnectedDeprecated = function( - connectionType: ConnectivityStateAndroid, - ): boolean { - return connectionType !== 'NONE' && connectionType !== 'UNKNOWN'; - }; -} - -function _isConnected(connection) { - return connection.type !== 'none' && connection.type !== 'unknown'; -} - -const _isConnectedSubscriptions = new Map(); - -/** - * NetInfo exposes info about online/offline status. - * - * See https://facebook.github.io/react-native/docs/netinfo.html - */ -const NetInfo = { - /** - * Adds an event handler. - * - * See https://facebook.github.io/react-native/docs/netinfo.html#addeventlistener - */ - addEventListener( - eventName: ChangeEventName, - handler: Function, - ): {remove: () => void} { - let listener; - if (eventName === 'connectionChange') { - listener = NetInfoEventEmitter.addListener( - DEVICE_CONNECTIVITY_EVENT, - appStateData => { - handler({ - type: appStateData.connectionType, - effectiveType: appStateData.effectiveConnectionType, - }); - }, - ); - } else if (eventName === 'change') { - console.warn( - 'NetInfo\'s "change" event is deprecated. Listen to the "connectionChange" event instead.', - ); - - listener = NetInfoEventEmitter.addListener( - DEVICE_CONNECTIVITY_EVENT, - appStateData => { - handler(appStateData.network_info); - }, - ); - } else { - console.warn('Trying to subscribe to unknown event: "' + eventName + '"'); - return { - remove: () => {}, - }; - } - - _subscriptions.set(handler, listener); - return { - remove: () => NetInfo.removeEventListener(eventName, handler), - }; - }, - - /** - * Removes the listener for network status changes. - * - * See https://facebook.github.io/react-native/docs/netinfo.html#removeeventlistener - */ - removeEventListener(eventName: ChangeEventName, handler: Function): void { - const listener = _subscriptions.get(handler); - if (!listener) { - return; - } - listener.remove(); - _subscriptions.delete(handler); - }, - - /** - * This function is deprecated. Use `getConnectionInfo` instead. - * Returns a promise that resolves with one of the deprecated connectivity - * types: - * - * The following connectivity types are deprecated. They're used by the - * deprecated APIs `fetch` and the `change` event. - * - * iOS connectivity types (deprecated): - * - `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 - * - * Android connectivity types (deprecated). - * - `NONE` - device is offline - * - `BLUETOOTH` - The Bluetooth data connection. - * - `DUMMY` - Dummy data connection. - * - `ETHERNET` - The Ethernet data connection. - * - `MOBILE` - The Mobile data connection. - * - `MOBILE_DUN` - A DUN-specific Mobile data connection. - * - `MOBILE_HIPRI` - A High Priority Mobile data connection. - * - `MOBILE_MMS` - An MMS-specific Mobile data connection. - * - `MOBILE_SUPL` - A SUPL-specific Mobile data connection. - * - `VPN` - A virtual network using one or more native bearers. Requires - * API Level 21 - * - `WIFI` - The WIFI data connection. - * - `WIMAX` - The WiMAX data connection. - * - `UNKNOWN` - Unknown data connection. - * - * The rest of the connectivity types are hidden by the Android API, but can - * be used if necessary. - */ - fetch(): Promise { - console.warn( - 'NetInfo.fetch() is deprecated. Use NetInfo.getConnectionInfo() instead.', - ); - return RCTNetInfo.getCurrentConnectivity().then(resp => resp.network_info); - }, - - /** - * See https://facebook.github.io/react-native/docs/netinfo.html#getconnectioninfo - */ - getConnectionInfo(): Promise { - return RCTNetInfo.getCurrentConnectivity().then(resp => { - return { - type: resp.connectionType, - effectiveType: resp.effectiveConnectionType, - }; - }); - }, - - /** - * An object with the same methods as above but the listener receives a - * boolean which represents the internet connectivity. - * - * See https://facebook.github.io/react-native/docs/netinfo.html#isconnected - */ - isConnected: { - addEventListener( - eventName: ChangeEventName, - handler: Function, - ): {remove: () => void} { - const listener = connection => { - if (eventName === 'change') { - handler(_isConnectedDeprecated(connection)); - } else if (eventName === 'connectionChange') { - handler(_isConnected(connection)); - } - }; - _isConnectedSubscriptions.set(handler, listener); - NetInfo.addEventListener(eventName, listener); - return { - remove: () => - NetInfo.isConnected.removeEventListener(eventName, handler), - }; - }, - - removeEventListener(eventName: ChangeEventName, handler: Function): void { - const listener = _isConnectedSubscriptions.get(handler); - NetInfo.removeEventListener(eventName, listener); - _isConnectedSubscriptions.delete(handler); - }, - - fetch(): Promise { - return NetInfo.getConnectionInfo().then(_isConnected); - }, - }, - - isConnectionExpensive(): Promise { - return Platform.OS === 'android' - ? RCTNetInfo.isConnectionMetered() - : Promise.reject(new Error('Currently not supported on iOS')); - }, -}; - -module.exports = NetInfo; diff --git a/Libraries/react-native/react-native-implementation.js b/Libraries/react-native/react-native-implementation.js index e4d0a49162..8a4acfc1df 100644 --- a/Libraries/react-native/react-native-implementation.js +++ b/Libraries/react-native/react-native-implementation.js @@ -250,15 +250,6 @@ module.exports = { get NativeEventEmitter() { return require('NativeEventEmitter'); }, - get NetInfo() { - warnOnce( - 'netinfo-moved', - 'NetInfo has been extracted from react-native core and will be removed in a future release. ' + - "It can now be installed and imported from '@react-native-community/netinfo' instead of 'react-native'. " + - 'See https://github.com/react-native-community/react-native-netinfo', - ); - return require('NetInfo'); - }, get PanResponder() { return require('PanResponder'); }, diff --git a/RNTester/js/NetInfoExample.js b/RNTester/js/NetInfoExample.js deleted file mode 100644 index ad825b72c2..0000000000 --- a/RNTester/js/NetInfoExample.js +++ /dev/null @@ -1,177 +0,0 @@ -/** - * Copyright (c) Facebook, Inc. and its affiliates. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - * - * @format - * @flow - */ - -'use strict'; - -const React = require('react'); -const {NetInfo, Text, View, TouchableWithoutFeedback} = require('react-native'); - -class ConnectionInfoSubscription extends React.Component<{}, $FlowFixMeState> { - state = { - connectionInfoHistory: [], - }; - - componentDidMount() { - NetInfo.addEventListener('change', this._handleConnectionInfoChange); - } - - componentWillUnmount() { - NetInfo.removeEventListener('change', this._handleConnectionInfoChange); - } - - _handleConnectionInfoChange = connectionInfo => { - const connectionInfoHistory = this.state.connectionInfoHistory.slice(); - connectionInfoHistory.push(connectionInfo); - this.setState({ - connectionInfoHistory, - }); - }; - - render() { - return ( - - {JSON.stringify(this.state.connectionInfoHistory)} - - ); - } -} - -class ConnectionInfoCurrent extends React.Component<{}, $FlowFixMeState> { - state = { - connectionInfo: null, - }; - - componentDidMount() { - NetInfo.addEventListener('change', this._handleConnectionInfoChange); - NetInfo.fetch().done(connectionInfo => { - this.setState({connectionInfo}); - }); - } - - componentWillUnmount() { - NetInfo.removeEventListener('change', this._handleConnectionInfoChange); - } - - _handleConnectionInfoChange = connectionInfo => { - this.setState({ - connectionInfo, - }); - }; - - render() { - return ( - - {this.state.connectionInfo} - - ); - } -} - -class IsConnected extends React.Component<{}, $FlowFixMeState> { - state = { - isConnected: null, - }; - - componentDidMount() { - NetInfo.isConnected.addEventListener( - 'change', - this._handleConnectivityChange, - ); - NetInfo.isConnected.fetch().done(isConnected => { - this.setState({isConnected}); - }); - } - - componentWillUnmount() { - NetInfo.isConnected.removeEventListener( - 'change', - this._handleConnectivityChange, - ); - } - - _handleConnectivityChange = isConnected => { - this.setState({ - isConnected, - }); - }; - - render() { - return ( - - {this.state.isConnected ? 'Online' : 'Offline'} - - ); - } -} - -class IsConnectionExpensive extends React.Component<{}, $FlowFixMeState> { - state = { - isConnectionExpensive: (null: ?boolean), - }; - - _checkIfExpensive = () => { - NetInfo.isConnectionExpensive().then(isConnectionExpensive => { - this.setState({isConnectionExpensive}); - }); - }; - - render() { - return ( - - - - - Click to see if connection is expensive: - {this.state.isConnectionExpensive === true - ? 'Expensive' - : this.state.isConnectionExpensive === false - ? 'Not expensive' - : 'Unknown'} - - - - - ); - } -} - -exports.title = 'NetInfo'; -exports.description = 'Monitor network status'; -exports.examples = [ - { - title: 'NetInfo.isConnected', - description: 'Asynchronously load and observe connectivity', - render(): React.Element { - return ; - }, - }, - { - title: 'NetInfo.update', - description: 'Asynchronously load and observe connectionInfo', - render(): React.Element { - return ; - }, - }, - { - title: 'NetInfo.updateHistory', - description: 'Observed updates to connectionInfo', - render(): React.Element { - return ; - }, - }, - { - platform: 'android', - title: 'NetInfo.isConnectionExpensive (Android)', - description: 'Asynchronously check isConnectionExpensive', - render(): React.Element { - return ; - }, - }, -]; diff --git a/RNTester/js/RNTesterList.android.js b/RNTester/js/RNTesterList.android.js index db60069b1e..a721932ddf 100644 --- a/RNTester/js/RNTesterList.android.js +++ b/RNTester/js/RNTesterList.android.js @@ -184,10 +184,6 @@ const APIExamples: Array = [ key: 'NativeAnimationsExample', module: require('./NativeAnimationsExample'), }, - { - key: 'NetInfoExample', - module: require('./NetInfoExample'), - }, { key: 'OrientationChangeExample', module: require('./OrientationChangeExample'), diff --git a/RNTester/js/RNTesterList.ios.js b/RNTester/js/RNTesterList.ios.js index a221ed9dcc..c0ff9a2f5b 100644 --- a/RNTester/js/RNTesterList.ios.js +++ b/RNTester/js/RNTesterList.ios.js @@ -261,11 +261,6 @@ const APIExamples: Array = [ module: require('./NativeAnimationsExample'), supportsTVOS: true, }, - { - key: 'NetInfoExample', - module: require('./NetInfoExample'), - supportsTVOS: true, - }, { key: 'OrientationChangeExample', module: require('./OrientationChangeExample'),