-
Notifications
You must be signed in to change notification settings - Fork 24.3k
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
DEPRECATION: Make NetInfo API cross platform and expose whether connection is 2g/3g/4g #14618
Changes from 1 commit
aae5254
6e9f46d
d7d4a01
a27f950
87e20c5
9b23163
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,6 +22,7 @@ const NetInfoEventEmitter = new NativeEventEmitter(RCTNetInfo); | |
const DEVICE_CONNECTIVITY_EVENT = 'networkStatusDidChange'; | ||
|
||
type ChangeEventName = $Enum<{ | ||
connectionchange: string, | ||
change: string, | ||
}>; | ||
|
||
|
@@ -79,56 +80,51 @@ const _isConnectedSubscriptions = new Map(); | |
* NetInfo exposes info about online/offline status | ||
* | ||
* ``` | ||
* NetInfo.fetch().then((reach) => { | ||
* console.log('Initial: ' + reach); | ||
* NetInfo.getConnection().then((connection) => { | ||
* console.log('Initial, type: ' + connection.type + ', effectiveType: ' + connection.effectiveType); | ||
* }); | ||
* function handleFirstConnectivityChange(reach) { | ||
* console.log('First change: ' + reach); | ||
* function handleFirstConnectivityChange(connection) { | ||
* console.log('First change, type: ' + connection.type + ', effectiveType: ' + connection.effectiveType); | ||
* NetInfo.removeEventListener( | ||
* 'change', | ||
* 'connectionchange', | ||
* handleFirstConnectivityChange | ||
* ); | ||
* } | ||
* NetInfo.addEventListener( | ||
* 'change', | ||
* 'connectionchange', | ||
* handleFirstConnectivityChange | ||
* ); | ||
* ``` | ||
* | ||
* ### IOS | ||
* | ||
* Asynchronously determine if the device is online and on a cellular network. | ||
* | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no-trailing-spaces: Trailing spaces not allowed. |
||
* | ||
* ### ConnectionType enum | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no-trailing-spaces: Trailing spaces not allowed. |
||
* | ||
* `ConnectionType` describes the type of connection the device is using to communicate with the network. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no-trailing-spaces: Trailing spaces not allowed. |
||
* | ||
* Cross platform values for `ConnectionType`: | ||
* - `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 | ||
* - `cellular` - device is connected via Edge, 3G, WiMax, or LTE | ||
* - `unknown` - error case and the network status is unknown | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no-trailing-spaces: Trailing spaces not allowed. |
||
* | ||
* Android-only values for `ConnectionType`: | ||
* - `bluetooth` - device is connected via Bluetooth | ||
* - `ethernet` - device is connected via Ethernet | ||
* - `wimax` - device is connected via WiMAX | ||
* | ||
* ### EffectiveConnectionType enum | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no-trailing-spaces: Trailing spaces not allowed. |
||
* | ||
* Cross platform values for `EffectiveConnectionType`: | ||
* - `2g` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no-trailing-spaces: Trailing spaces not allowed. |
||
* - `3g` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no-trailing-spaces: Trailing spaces not allowed. |
||
* - `4g` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no-trailing-spaces: Trailing spaces not allowed. |
||
* - `unknown` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no-trailing-spaces: Trailing spaces not allowed. |
||
* | ||
* ### Android | ||
* | ||
* To request network info, you need to add the following line to your | ||
* app's `AndroidManifest.xml`: | ||
* | ||
* `<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />` | ||
* Asynchronously determine if the device is connected and details about that connection. | ||
* | ||
* Android Connectivity Types. | ||
* | ||
* - `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 ConnectivityStates are hidden by the Android API, but can be used if necessary. | ||
* | ||
* ### isConnectionExpensive | ||
* | ||
|
@@ -167,22 +163,77 @@ const _isConnectedSubscriptions = new Map(); | |
* handleFirstConnectivityChange | ||
* ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no-trailing-spaces: Trailing spaces not allowed. |
||
* ``` | ||
* | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no-trailing-spaces: Trailing spaces not allowed. |
||
* ### Connectivity Types (deprecated) | ||
* | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no-trailing-spaces: Trailing spaces not allowed. |
||
* 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no-trailing-spaces: Trailing spaces not allowed. |
||
* - `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. | ||
*/ | ||
const NetInfo = { | ||
/** | ||
* Invokes the listener whenever network status changes. | ||
* The listener receives one of the connectivity types listed above. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no-trailing-spaces: Trailing spaces not allowed. |
||
* Adds an event handler. Supported events: | ||
* | ||
* - `connectionchange`: Fires when the network status changes. The argument to the event | ||
* handler is an object with keys: | ||
* - `type`: A `ConnectionType` (listed above) | ||
* - `effectiveType`: An `EffectiveConnectionType` (listed above) | ||
* - `change`: This event is deprecated. Listen to `connectionchange` instead. Fires when | ||
* the network status changes. The argument to the event handler is one of the deprecated | ||
* connectivity types listed above. | ||
*/ | ||
addEventListener( | ||
eventName: ChangeEventName, | ||
handler: Function | ||
): {remove: () => void} { | ||
const listener = NetInfoEventEmitter.addListener( | ||
DEVICE_CONNECTIVITY_EVENT, | ||
(appStateData) => { | ||
handler(appStateData.network_info); | ||
} | ||
); | ||
let listener; | ||
if (eventName === 'connectionchange') { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why the event name is all lower case? And There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll switch it to |
||
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: "${type}"`); | ||
return { | ||
remove: () => {} | ||
}; | ||
} | ||
|
||
_subscriptions.set(handler, listener); | ||
return { | ||
remove: () => NetInfo.removeEventListener(eventName, handler) | ||
|
@@ -205,13 +256,28 @@ const NetInfo = { | |
}, | ||
|
||
/** | ||
* Returns a promise that resolves with one of the connectivity types listed | ||
* above. | ||
* This function is deprecated. Use `getConnection` instead. Returns a promise that | ||
* resolves with one of the deprecated connectivity types listed above. | ||
*/ | ||
fetch(): Promise<any> { | ||
console.warn('NetInfo.fetch() is deprecated. Use NetInfo.getConnection() instead.'); | ||
return RCTNetInfo.getCurrentConnectivity().then(resp => resp.network_info); | ||
}, | ||
|
||
/** | ||
* Returns a promise that resolves to an object with `type` and `effectiveType` keys | ||
* whose values are a `ConnectionType` and an `EffectiveConnectionType`, (described above), | ||
* respectively. | ||
*/ | ||
getConnection(): Promise<any> { | ||
return RCTNetInfo.getCurrentConnectivity().then(resp => { | ||
return { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. semi: Missing semicolon. |
||
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. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This name is quite confusing (because it is not connection), can/should we rename it to something like
getConnectionInfo
orgetConnectionStatus
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@shergin I modeled it after the W3C Network Information API which offers this information off of a
connection
property (https://wicg.github.io/netinfo/#-dfn-connection-dfn-attribute). I can switch it togetConnectionInfo
if you'd prefer.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see.
The connection attribute, when getting, returns an object that implements the NetworkInformation interface.
Yeah, I think usinggetConnectionInfo
orgetConnectionInformation
would be much better.