react-native-macos/Libraries/Network/NetInfo.js

335 строки
9.9 KiB
JavaScript
Исходник Обычный вид История

/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
* @providesModule NetInfo
* @flow
*/
'use strict';
const Map = require('Map');
const NativeEventEmitter = require('NativeEventEmitter');
const NativeModules = require('NativeModules');
const Platform = require('Platform');
const RCTNetInfo = NativeModules.NetInfo;
const NetInfoEventEmitter = new NativeEventEmitter(RCTNetInfo);
const DEVICE_CONNECTIVITY_EVENT = 'networkStatusDidChange';
type ChangeEventName = $Enum<{
DEPRECATION: Make NetInfo API cross platform and expose whether connection is 2g/3g/4g Summary: This change intends to fix 2 issues with the NetInfo API: - The NetInfo API is currently platform-specific. It returns completely different values on iOS and Android. - The NetInfo API currently doesn't expose a way to determine whether the connection is 2g, 3g, or 4g. The NetInfo API currently just exposes a string-based enum representing the connectivity type. The string values are different between iOS and Andorid. Because of this design, it's not obvious how to achieve the goals of this change without making a breaking change. Consequently, this change deprecates the old NetInfo APIs and introduces new ones. Specifically, these are the API changes: - The `fetch` method is deprecated in favor of `getConnection` - The `change` event is deprecated in favor of the `connectionchange` event. - `getConnection`/`connectionchange` use a new set of enum values compared to `fetch`/`change`. See the documentation for the new values. - On iOS, `cell` is now known as `cellular`. It's worth pointing out this one in particular because the old and new names are so similar. The rest of the iOS values have remained the same. - Some of the Android enum values have been removed without a replacement (e.g. `DUMMY`, `MOBILE_DUN`, `MOBILE_HIPRI`, `MOBILE_MMS`, `MOBILE_SUPL`, `VPN`). If desirable, we could find a way to expose these in the new API. For example, we could have a `platformValue` key that exposes the platform's enum values directly (like the old `fetch` API did). `getConnection` and `connectionchange` each expose an object which has 2 keys conveying a `ConnectionType` (e.g. wifi, cellular) and an `EffectiveConnectionType` (e.g. 2g, 3g). These enums and their values are taken directly from the W3C's Network Information API spec (https://wicg.github.io/netinfo/). Copying the W3C's API will make it easy to expose a `navigation.connection` polyfill, if we want, in the future. Additionally, because the new APIs expose an object instead of a string, it's easier to extend the APIs in the future by adding keys to the object without causing a breaking change. Note that the W3C's spec doesn't have an "unknown" value for `EffectiveConnectionType`. I chose to introduce this non-standard value because it's possible for the current implementation to not have an `effectiveConnectionType` and I figured it was worth representing this possibility explicitly with "unknown" instead of implicitly with `null`. **Test Plan (required)** Verified that the methods (`fetch` and `getConnection`) and the events (`change` and `connectionchange`) return the correct data on iOS and Android when connected to a wifi network and a 4G cellular network. Verified that switching networks causes the event to fire with the correct information. Verified that the old APIs (`fetch' and 'change') emit a deprecation warning when used. My team is using a similar patch in our app. Adam Comella Microsoft Corp. Closes https://github.com/facebook/react-native/pull/14618 Differential Revision: D5459593 Pulled By: shergin fbshipit-source-id: f1e6c5d572bb3e2669fbd4ba7d0fbb106525280e
2017-07-25 01:42:27 +03:00
connectionChange: string,
change: string,
}>;
type ReachabilityStateIOS = $Enum<{
cell: string,
none: string,
unknown: string,
wifi: string,
}>;
type ConnectivityStateAndroid = $Enum<{
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 _isConnected;
if (Platform.OS === 'ios') {
_isConnected = function(
reachability: ReachabilityStateIOS,
): bool {
return reachability !== 'none' && reachability !== 'unknown';
};
} else if (Platform.OS === 'android') {
_isConnected = function(
connectionType: ConnectivityStateAndroid,
): bool {
return connectionType !== 'NONE' && connectionType !== 'UNKNOWN';
};
}
const _isConnectedSubscriptions = new Map();
/**
* NetInfo exposes info about online/offline status
*
* ```
DEPRECATION: Make NetInfo API cross platform and expose whether connection is 2g/3g/4g Summary: This change intends to fix 2 issues with the NetInfo API: - The NetInfo API is currently platform-specific. It returns completely different values on iOS and Android. - The NetInfo API currently doesn't expose a way to determine whether the connection is 2g, 3g, or 4g. The NetInfo API currently just exposes a string-based enum representing the connectivity type. The string values are different between iOS and Andorid. Because of this design, it's not obvious how to achieve the goals of this change without making a breaking change. Consequently, this change deprecates the old NetInfo APIs and introduces new ones. Specifically, these are the API changes: - The `fetch` method is deprecated in favor of `getConnection` - The `change` event is deprecated in favor of the `connectionchange` event. - `getConnection`/`connectionchange` use a new set of enum values compared to `fetch`/`change`. See the documentation for the new values. - On iOS, `cell` is now known as `cellular`. It's worth pointing out this one in particular because the old and new names are so similar. The rest of the iOS values have remained the same. - Some of the Android enum values have been removed without a replacement (e.g. `DUMMY`, `MOBILE_DUN`, `MOBILE_HIPRI`, `MOBILE_MMS`, `MOBILE_SUPL`, `VPN`). If desirable, we could find a way to expose these in the new API. For example, we could have a `platformValue` key that exposes the platform's enum values directly (like the old `fetch` API did). `getConnection` and `connectionchange` each expose an object which has 2 keys conveying a `ConnectionType` (e.g. wifi, cellular) and an `EffectiveConnectionType` (e.g. 2g, 3g). These enums and their values are taken directly from the W3C's Network Information API spec (https://wicg.github.io/netinfo/). Copying the W3C's API will make it easy to expose a `navigation.connection` polyfill, if we want, in the future. Additionally, because the new APIs expose an object instead of a string, it's easier to extend the APIs in the future by adding keys to the object without causing a breaking change. Note that the W3C's spec doesn't have an "unknown" value for `EffectiveConnectionType`. I chose to introduce this non-standard value because it's possible for the current implementation to not have an `effectiveConnectionType` and I figured it was worth representing this possibility explicitly with "unknown" instead of implicitly with `null`. **Test Plan (required)** Verified that the methods (`fetch` and `getConnection`) and the events (`change` and `connectionchange`) return the correct data on iOS and Android when connected to a wifi network and a 4G cellular network. Verified that switching networks causes the event to fire with the correct information. Verified that the old APIs (`fetch' and 'change') emit a deprecation warning when used. My team is using a similar patch in our app. Adam Comella Microsoft Corp. Closes https://github.com/facebook/react-native/pull/14618 Differential Revision: D5459593 Pulled By: shergin fbshipit-source-id: f1e6c5d572bb3e2669fbd4ba7d0fbb106525280e
2017-07-25 01:42:27 +03:00
* NetInfo.getConnectionInfo().then((connectionInfo) => {
* console.log('Initial, type: ' + connectionInfo.type + ', effectiveType: ' + connectionInfo.effectiveType);
* });
DEPRECATION: Make NetInfo API cross platform and expose whether connection is 2g/3g/4g Summary: This change intends to fix 2 issues with the NetInfo API: - The NetInfo API is currently platform-specific. It returns completely different values on iOS and Android. - The NetInfo API currently doesn't expose a way to determine whether the connection is 2g, 3g, or 4g. The NetInfo API currently just exposes a string-based enum representing the connectivity type. The string values are different between iOS and Andorid. Because of this design, it's not obvious how to achieve the goals of this change without making a breaking change. Consequently, this change deprecates the old NetInfo APIs and introduces new ones. Specifically, these are the API changes: - The `fetch` method is deprecated in favor of `getConnection` - The `change` event is deprecated in favor of the `connectionchange` event. - `getConnection`/`connectionchange` use a new set of enum values compared to `fetch`/`change`. See the documentation for the new values. - On iOS, `cell` is now known as `cellular`. It's worth pointing out this one in particular because the old and new names are so similar. The rest of the iOS values have remained the same. - Some of the Android enum values have been removed without a replacement (e.g. `DUMMY`, `MOBILE_DUN`, `MOBILE_HIPRI`, `MOBILE_MMS`, `MOBILE_SUPL`, `VPN`). If desirable, we could find a way to expose these in the new API. For example, we could have a `platformValue` key that exposes the platform's enum values directly (like the old `fetch` API did). `getConnection` and `connectionchange` each expose an object which has 2 keys conveying a `ConnectionType` (e.g. wifi, cellular) and an `EffectiveConnectionType` (e.g. 2g, 3g). These enums and their values are taken directly from the W3C's Network Information API spec (https://wicg.github.io/netinfo/). Copying the W3C's API will make it easy to expose a `navigation.connection` polyfill, if we want, in the future. Additionally, because the new APIs expose an object instead of a string, it's easier to extend the APIs in the future by adding keys to the object without causing a breaking change. Note that the W3C's spec doesn't have an "unknown" value for `EffectiveConnectionType`. I chose to introduce this non-standard value because it's possible for the current implementation to not have an `effectiveConnectionType` and I figured it was worth representing this possibility explicitly with "unknown" instead of implicitly with `null`. **Test Plan (required)** Verified that the methods (`fetch` and `getConnection`) and the events (`change` and `connectionchange`) return the correct data on iOS and Android when connected to a wifi network and a 4G cellular network. Verified that switching networks causes the event to fire with the correct information. Verified that the old APIs (`fetch' and 'change') emit a deprecation warning when used. My team is using a similar patch in our app. Adam Comella Microsoft Corp. Closes https://github.com/facebook/react-native/pull/14618 Differential Revision: D5459593 Pulled By: shergin fbshipit-source-id: f1e6c5d572bb3e2669fbd4ba7d0fbb106525280e
2017-07-25 01:42:27 +03:00
* function handleFirstConnectivityChange(connectionInfo) {
* console.log('First change, type: ' + connectionInfo.type + ', effectiveType: ' + connectionInfo.effectiveType);
* NetInfo.removeEventListener(
DEPRECATION: Make NetInfo API cross platform and expose whether connection is 2g/3g/4g Summary: This change intends to fix 2 issues with the NetInfo API: - The NetInfo API is currently platform-specific. It returns completely different values on iOS and Android. - The NetInfo API currently doesn't expose a way to determine whether the connection is 2g, 3g, or 4g. The NetInfo API currently just exposes a string-based enum representing the connectivity type. The string values are different between iOS and Andorid. Because of this design, it's not obvious how to achieve the goals of this change without making a breaking change. Consequently, this change deprecates the old NetInfo APIs and introduces new ones. Specifically, these are the API changes: - The `fetch` method is deprecated in favor of `getConnection` - The `change` event is deprecated in favor of the `connectionchange` event. - `getConnection`/`connectionchange` use a new set of enum values compared to `fetch`/`change`. See the documentation for the new values. - On iOS, `cell` is now known as `cellular`. It's worth pointing out this one in particular because the old and new names are so similar. The rest of the iOS values have remained the same. - Some of the Android enum values have been removed without a replacement (e.g. `DUMMY`, `MOBILE_DUN`, `MOBILE_HIPRI`, `MOBILE_MMS`, `MOBILE_SUPL`, `VPN`). If desirable, we could find a way to expose these in the new API. For example, we could have a `platformValue` key that exposes the platform's enum values directly (like the old `fetch` API did). `getConnection` and `connectionchange` each expose an object which has 2 keys conveying a `ConnectionType` (e.g. wifi, cellular) and an `EffectiveConnectionType` (e.g. 2g, 3g). These enums and their values are taken directly from the W3C's Network Information API spec (https://wicg.github.io/netinfo/). Copying the W3C's API will make it easy to expose a `navigation.connection` polyfill, if we want, in the future. Additionally, because the new APIs expose an object instead of a string, it's easier to extend the APIs in the future by adding keys to the object without causing a breaking change. Note that the W3C's spec doesn't have an "unknown" value for `EffectiveConnectionType`. I chose to introduce this non-standard value because it's possible for the current implementation to not have an `effectiveConnectionType` and I figured it was worth representing this possibility explicitly with "unknown" instead of implicitly with `null`. **Test Plan (required)** Verified that the methods (`fetch` and `getConnection`) and the events (`change` and `connectionchange`) return the correct data on iOS and Android when connected to a wifi network and a 4G cellular network. Verified that switching networks causes the event to fire with the correct information. Verified that the old APIs (`fetch' and 'change') emit a deprecation warning when used. My team is using a similar patch in our app. Adam Comella Microsoft Corp. Closes https://github.com/facebook/react-native/pull/14618 Differential Revision: D5459593 Pulled By: shergin fbshipit-source-id: f1e6c5d572bb3e2669fbd4ba7d0fbb106525280e
2017-07-25 01:42:27 +03:00
* 'connectionChange',
* handleFirstConnectivityChange
* );
* }
* NetInfo.addEventListener(
DEPRECATION: Make NetInfo API cross platform and expose whether connection is 2g/3g/4g Summary: This change intends to fix 2 issues with the NetInfo API: - The NetInfo API is currently platform-specific. It returns completely different values on iOS and Android. - The NetInfo API currently doesn't expose a way to determine whether the connection is 2g, 3g, or 4g. The NetInfo API currently just exposes a string-based enum representing the connectivity type. The string values are different between iOS and Andorid. Because of this design, it's not obvious how to achieve the goals of this change without making a breaking change. Consequently, this change deprecates the old NetInfo APIs and introduces new ones. Specifically, these are the API changes: - The `fetch` method is deprecated in favor of `getConnection` - The `change` event is deprecated in favor of the `connectionchange` event. - `getConnection`/`connectionchange` use a new set of enum values compared to `fetch`/`change`. See the documentation for the new values. - On iOS, `cell` is now known as `cellular`. It's worth pointing out this one in particular because the old and new names are so similar. The rest of the iOS values have remained the same. - Some of the Android enum values have been removed without a replacement (e.g. `DUMMY`, `MOBILE_DUN`, `MOBILE_HIPRI`, `MOBILE_MMS`, `MOBILE_SUPL`, `VPN`). If desirable, we could find a way to expose these in the new API. For example, we could have a `platformValue` key that exposes the platform's enum values directly (like the old `fetch` API did). `getConnection` and `connectionchange` each expose an object which has 2 keys conveying a `ConnectionType` (e.g. wifi, cellular) and an `EffectiveConnectionType` (e.g. 2g, 3g). These enums and their values are taken directly from the W3C's Network Information API spec (https://wicg.github.io/netinfo/). Copying the W3C's API will make it easy to expose a `navigation.connection` polyfill, if we want, in the future. Additionally, because the new APIs expose an object instead of a string, it's easier to extend the APIs in the future by adding keys to the object without causing a breaking change. Note that the W3C's spec doesn't have an "unknown" value for `EffectiveConnectionType`. I chose to introduce this non-standard value because it's possible for the current implementation to not have an `effectiveConnectionType` and I figured it was worth representing this possibility explicitly with "unknown" instead of implicitly with `null`. **Test Plan (required)** Verified that the methods (`fetch` and `getConnection`) and the events (`change` and `connectionchange`) return the correct data on iOS and Android when connected to a wifi network and a 4G cellular network. Verified that switching networks causes the event to fire with the correct information. Verified that the old APIs (`fetch' and 'change') emit a deprecation warning when used. My team is using a similar patch in our app. Adam Comella Microsoft Corp. Closes https://github.com/facebook/react-native/pull/14618 Differential Revision: D5459593 Pulled By: shergin fbshipit-source-id: f1e6c5d572bb3e2669fbd4ba7d0fbb106525280e
2017-07-25 01:42:27 +03:00
* 'connectionChange',
* handleFirstConnectivityChange
* );
* ```
*
DEPRECATION: Make NetInfo API cross platform and expose whether connection is 2g/3g/4g Summary: This change intends to fix 2 issues with the NetInfo API: - The NetInfo API is currently platform-specific. It returns completely different values on iOS and Android. - The NetInfo API currently doesn't expose a way to determine whether the connection is 2g, 3g, or 4g. The NetInfo API currently just exposes a string-based enum representing the connectivity type. The string values are different between iOS and Andorid. Because of this design, it's not obvious how to achieve the goals of this change without making a breaking change. Consequently, this change deprecates the old NetInfo APIs and introduces new ones. Specifically, these are the API changes: - The `fetch` method is deprecated in favor of `getConnection` - The `change` event is deprecated in favor of the `connectionchange` event. - `getConnection`/`connectionchange` use a new set of enum values compared to `fetch`/`change`. See the documentation for the new values. - On iOS, `cell` is now known as `cellular`. It's worth pointing out this one in particular because the old and new names are so similar. The rest of the iOS values have remained the same. - Some of the Android enum values have been removed without a replacement (e.g. `DUMMY`, `MOBILE_DUN`, `MOBILE_HIPRI`, `MOBILE_MMS`, `MOBILE_SUPL`, `VPN`). If desirable, we could find a way to expose these in the new API. For example, we could have a `platformValue` key that exposes the platform's enum values directly (like the old `fetch` API did). `getConnection` and `connectionchange` each expose an object which has 2 keys conveying a `ConnectionType` (e.g. wifi, cellular) and an `EffectiveConnectionType` (e.g. 2g, 3g). These enums and their values are taken directly from the W3C's Network Information API spec (https://wicg.github.io/netinfo/). Copying the W3C's API will make it easy to expose a `navigation.connection` polyfill, if we want, in the future. Additionally, because the new APIs expose an object instead of a string, it's easier to extend the APIs in the future by adding keys to the object without causing a breaking change. Note that the W3C's spec doesn't have an "unknown" value for `EffectiveConnectionType`. I chose to introduce this non-standard value because it's possible for the current implementation to not have an `effectiveConnectionType` and I figured it was worth representing this possibility explicitly with "unknown" instead of implicitly with `null`. **Test Plan (required)** Verified that the methods (`fetch` and `getConnection`) and the events (`change` and `connectionchange`) return the correct data on iOS and Android when connected to a wifi network and a 4G cellular network. Verified that switching networks causes the event to fire with the correct information. Verified that the old APIs (`fetch' and 'change') emit a deprecation warning when used. My team is using a similar patch in our app. Adam Comella Microsoft Corp. Closes https://github.com/facebook/react-native/pull/14618 Differential Revision: D5459593 Pulled By: shergin fbshipit-source-id: f1e6c5d572bb3e2669fbd4ba7d0fbb106525280e
2017-07-25 01:42:27 +03:00
* ### ConnectionType enum
*
DEPRECATION: Make NetInfo API cross platform and expose whether connection is 2g/3g/4g Summary: This change intends to fix 2 issues with the NetInfo API: - The NetInfo API is currently platform-specific. It returns completely different values on iOS and Android. - The NetInfo API currently doesn't expose a way to determine whether the connection is 2g, 3g, or 4g. The NetInfo API currently just exposes a string-based enum representing the connectivity type. The string values are different between iOS and Andorid. Because of this design, it's not obvious how to achieve the goals of this change without making a breaking change. Consequently, this change deprecates the old NetInfo APIs and introduces new ones. Specifically, these are the API changes: - The `fetch` method is deprecated in favor of `getConnection` - The `change` event is deprecated in favor of the `connectionchange` event. - `getConnection`/`connectionchange` use a new set of enum values compared to `fetch`/`change`. See the documentation for the new values. - On iOS, `cell` is now known as `cellular`. It's worth pointing out this one in particular because the old and new names are so similar. The rest of the iOS values have remained the same. - Some of the Android enum values have been removed without a replacement (e.g. `DUMMY`, `MOBILE_DUN`, `MOBILE_HIPRI`, `MOBILE_MMS`, `MOBILE_SUPL`, `VPN`). If desirable, we could find a way to expose these in the new API. For example, we could have a `platformValue` key that exposes the platform's enum values directly (like the old `fetch` API did). `getConnection` and `connectionchange` each expose an object which has 2 keys conveying a `ConnectionType` (e.g. wifi, cellular) and an `EffectiveConnectionType` (e.g. 2g, 3g). These enums and their values are taken directly from the W3C's Network Information API spec (https://wicg.github.io/netinfo/). Copying the W3C's API will make it easy to expose a `navigation.connection` polyfill, if we want, in the future. Additionally, because the new APIs expose an object instead of a string, it's easier to extend the APIs in the future by adding keys to the object without causing a breaking change. Note that the W3C's spec doesn't have an "unknown" value for `EffectiveConnectionType`. I chose to introduce this non-standard value because it's possible for the current implementation to not have an `effectiveConnectionType` and I figured it was worth representing this possibility explicitly with "unknown" instead of implicitly with `null`. **Test Plan (required)** Verified that the methods (`fetch` and `getConnection`) and the events (`change` and `connectionchange`) return the correct data on iOS and Android when connected to a wifi network and a 4G cellular network. Verified that switching networks causes the event to fire with the correct information. Verified that the old APIs (`fetch' and 'change') emit a deprecation warning when used. My team is using a similar patch in our app. Adam Comella Microsoft Corp. Closes https://github.com/facebook/react-native/pull/14618 Differential Revision: D5459593 Pulled By: shergin fbshipit-source-id: f1e6c5d572bb3e2669fbd4ba7d0fbb106525280e
2017-07-25 01:42:27 +03:00
* `ConnectionType` describes the type of connection the device is using to communicate with the network.
*
DEPRECATION: Make NetInfo API cross platform and expose whether connection is 2g/3g/4g Summary: This change intends to fix 2 issues with the NetInfo API: - The NetInfo API is currently platform-specific. It returns completely different values on iOS and Android. - The NetInfo API currently doesn't expose a way to determine whether the connection is 2g, 3g, or 4g. The NetInfo API currently just exposes a string-based enum representing the connectivity type. The string values are different between iOS and Andorid. Because of this design, it's not obvious how to achieve the goals of this change without making a breaking change. Consequently, this change deprecates the old NetInfo APIs and introduces new ones. Specifically, these are the API changes: - The `fetch` method is deprecated in favor of `getConnection` - The `change` event is deprecated in favor of the `connectionchange` event. - `getConnection`/`connectionchange` use a new set of enum values compared to `fetch`/`change`. See the documentation for the new values. - On iOS, `cell` is now known as `cellular`. It's worth pointing out this one in particular because the old and new names are so similar. The rest of the iOS values have remained the same. - Some of the Android enum values have been removed without a replacement (e.g. `DUMMY`, `MOBILE_DUN`, `MOBILE_HIPRI`, `MOBILE_MMS`, `MOBILE_SUPL`, `VPN`). If desirable, we could find a way to expose these in the new API. For example, we could have a `platformValue` key that exposes the platform's enum values directly (like the old `fetch` API did). `getConnection` and `connectionchange` each expose an object which has 2 keys conveying a `ConnectionType` (e.g. wifi, cellular) and an `EffectiveConnectionType` (e.g. 2g, 3g). These enums and their values are taken directly from the W3C's Network Information API spec (https://wicg.github.io/netinfo/). Copying the W3C's API will make it easy to expose a `navigation.connection` polyfill, if we want, in the future. Additionally, because the new APIs expose an object instead of a string, it's easier to extend the APIs in the future by adding keys to the object without causing a breaking change. Note that the W3C's spec doesn't have an "unknown" value for `EffectiveConnectionType`. I chose to introduce this non-standard value because it's possible for the current implementation to not have an `effectiveConnectionType` and I figured it was worth representing this possibility explicitly with "unknown" instead of implicitly with `null`. **Test Plan (required)** Verified that the methods (`fetch` and `getConnection`) and the events (`change` and `connectionchange`) return the correct data on iOS and Android when connected to a wifi network and a 4G cellular network. Verified that switching networks causes the event to fire with the correct information. Verified that the old APIs (`fetch' and 'change') emit a deprecation warning when used. My team is using a similar patch in our app. Adam Comella Microsoft Corp. Closes https://github.com/facebook/react-native/pull/14618 Differential Revision: D5459593 Pulled By: shergin fbshipit-source-id: f1e6c5d572bb3e2669fbd4ba7d0fbb106525280e
2017-07-25 01:42:27 +03:00
* Cross platform values for `ConnectionType`:
* - `none` - device is offline
* - `wifi` - device is online and connected via wifi, or is the iOS simulator
DEPRECATION: Make NetInfo API cross platform and expose whether connection is 2g/3g/4g Summary: This change intends to fix 2 issues with the NetInfo API: - The NetInfo API is currently platform-specific. It returns completely different values on iOS and Android. - The NetInfo API currently doesn't expose a way to determine whether the connection is 2g, 3g, or 4g. The NetInfo API currently just exposes a string-based enum representing the connectivity type. The string values are different between iOS and Andorid. Because of this design, it's not obvious how to achieve the goals of this change without making a breaking change. Consequently, this change deprecates the old NetInfo APIs and introduces new ones. Specifically, these are the API changes: - The `fetch` method is deprecated in favor of `getConnection` - The `change` event is deprecated in favor of the `connectionchange` event. - `getConnection`/`connectionchange` use a new set of enum values compared to `fetch`/`change`. See the documentation for the new values. - On iOS, `cell` is now known as `cellular`. It's worth pointing out this one in particular because the old and new names are so similar. The rest of the iOS values have remained the same. - Some of the Android enum values have been removed without a replacement (e.g. `DUMMY`, `MOBILE_DUN`, `MOBILE_HIPRI`, `MOBILE_MMS`, `MOBILE_SUPL`, `VPN`). If desirable, we could find a way to expose these in the new API. For example, we could have a `platformValue` key that exposes the platform's enum values directly (like the old `fetch` API did). `getConnection` and `connectionchange` each expose an object which has 2 keys conveying a `ConnectionType` (e.g. wifi, cellular) and an `EffectiveConnectionType` (e.g. 2g, 3g). These enums and their values are taken directly from the W3C's Network Information API spec (https://wicg.github.io/netinfo/). Copying the W3C's API will make it easy to expose a `navigation.connection` polyfill, if we want, in the future. Additionally, because the new APIs expose an object instead of a string, it's easier to extend the APIs in the future by adding keys to the object without causing a breaking change. Note that the W3C's spec doesn't have an "unknown" value for `EffectiveConnectionType`. I chose to introduce this non-standard value because it's possible for the current implementation to not have an `effectiveConnectionType` and I figured it was worth representing this possibility explicitly with "unknown" instead of implicitly with `null`. **Test Plan (required)** Verified that the methods (`fetch` and `getConnection`) and the events (`change` and `connectionchange`) return the correct data on iOS and Android when connected to a wifi network and a 4G cellular network. Verified that switching networks causes the event to fire with the correct information. Verified that the old APIs (`fetch' and 'change') emit a deprecation warning when used. My team is using a similar patch in our app. Adam Comella Microsoft Corp. Closes https://github.com/facebook/react-native/pull/14618 Differential Revision: D5459593 Pulled By: shergin fbshipit-source-id: f1e6c5d572bb3e2669fbd4ba7d0fbb106525280e
2017-07-25 01:42:27 +03:00
* - `cellular` - device is connected via Edge, 3G, WiMax, or LTE
* - `unknown` - error case and the network status is unknown
*
DEPRECATION: Make NetInfo API cross platform and expose whether connection is 2g/3g/4g Summary: This change intends to fix 2 issues with the NetInfo API: - The NetInfo API is currently platform-specific. It returns completely different values on iOS and Android. - The NetInfo API currently doesn't expose a way to determine whether the connection is 2g, 3g, or 4g. The NetInfo API currently just exposes a string-based enum representing the connectivity type. The string values are different between iOS and Andorid. Because of this design, it's not obvious how to achieve the goals of this change without making a breaking change. Consequently, this change deprecates the old NetInfo APIs and introduces new ones. Specifically, these are the API changes: - The `fetch` method is deprecated in favor of `getConnection` - The `change` event is deprecated in favor of the `connectionchange` event. - `getConnection`/`connectionchange` use a new set of enum values compared to `fetch`/`change`. See the documentation for the new values. - On iOS, `cell` is now known as `cellular`. It's worth pointing out this one in particular because the old and new names are so similar. The rest of the iOS values have remained the same. - Some of the Android enum values have been removed without a replacement (e.g. `DUMMY`, `MOBILE_DUN`, `MOBILE_HIPRI`, `MOBILE_MMS`, `MOBILE_SUPL`, `VPN`). If desirable, we could find a way to expose these in the new API. For example, we could have a `platformValue` key that exposes the platform's enum values directly (like the old `fetch` API did). `getConnection` and `connectionchange` each expose an object which has 2 keys conveying a `ConnectionType` (e.g. wifi, cellular) and an `EffectiveConnectionType` (e.g. 2g, 3g). These enums and their values are taken directly from the W3C's Network Information API spec (https://wicg.github.io/netinfo/). Copying the W3C's API will make it easy to expose a `navigation.connection` polyfill, if we want, in the future. Additionally, because the new APIs expose an object instead of a string, it's easier to extend the APIs in the future by adding keys to the object without causing a breaking change. Note that the W3C's spec doesn't have an "unknown" value for `EffectiveConnectionType`. I chose to introduce this non-standard value because it's possible for the current implementation to not have an `effectiveConnectionType` and I figured it was worth representing this possibility explicitly with "unknown" instead of implicitly with `null`. **Test Plan (required)** Verified that the methods (`fetch` and `getConnection`) and the events (`change` and `connectionchange`) return the correct data on iOS and Android when connected to a wifi network and a 4G cellular network. Verified that switching networks causes the event to fire with the correct information. Verified that the old APIs (`fetch' and 'change') emit a deprecation warning when used. My team is using a similar patch in our app. Adam Comella Microsoft Corp. Closes https://github.com/facebook/react-native/pull/14618 Differential Revision: D5459593 Pulled By: shergin fbshipit-source-id: f1e6c5d572bb3e2669fbd4ba7d0fbb106525280e
2017-07-25 01:42:27 +03:00
* 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
*
* Cross platform values for `EffectiveConnectionType`:
* - `2g`
* - `3g`
* - `4g`
* - `unknown`
*
* ### 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" />`
*
* ### isConnectionExpensive
*
* Available on Android. Detect if the current active connection is metered or not. A network is
* classified as metered when the user is sensitive to heavy data usage on that connection due to
* monetary costs, data limitations or battery/performance issues.
*
* ```
* NetInfo.isConnectionExpensive()
* .then(isConnectionExpensive => {
* console.log('Connection is ' + (isConnectionExpensive ? 'Expensive' : 'Not Expensive'));
* })
* .catch(error => {
* console.error(error);
* });
* ```
*
* ### isConnected
*
* Available on all platforms. Asynchronously fetch a boolean to determine
* internet connectivity.
*
* ```
* NetInfo.isConnected.fetch().then(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
* );
* ```
DEPRECATION: Make NetInfo API cross platform and expose whether connection is 2g/3g/4g Summary: This change intends to fix 2 issues with the NetInfo API: - The NetInfo API is currently platform-specific. It returns completely different values on iOS and Android. - The NetInfo API currently doesn't expose a way to determine whether the connection is 2g, 3g, or 4g. The NetInfo API currently just exposes a string-based enum representing the connectivity type. The string values are different between iOS and Andorid. Because of this design, it's not obvious how to achieve the goals of this change without making a breaking change. Consequently, this change deprecates the old NetInfo APIs and introduces new ones. Specifically, these are the API changes: - The `fetch` method is deprecated in favor of `getConnection` - The `change` event is deprecated in favor of the `connectionchange` event. - `getConnection`/`connectionchange` use a new set of enum values compared to `fetch`/`change`. See the documentation for the new values. - On iOS, `cell` is now known as `cellular`. It's worth pointing out this one in particular because the old and new names are so similar. The rest of the iOS values have remained the same. - Some of the Android enum values have been removed without a replacement (e.g. `DUMMY`, `MOBILE_DUN`, `MOBILE_HIPRI`, `MOBILE_MMS`, `MOBILE_SUPL`, `VPN`). If desirable, we could find a way to expose these in the new API. For example, we could have a `platformValue` key that exposes the platform's enum values directly (like the old `fetch` API did). `getConnection` and `connectionchange` each expose an object which has 2 keys conveying a `ConnectionType` (e.g. wifi, cellular) and an `EffectiveConnectionType` (e.g. 2g, 3g). These enums and their values are taken directly from the W3C's Network Information API spec (https://wicg.github.io/netinfo/). Copying the W3C's API will make it easy to expose a `navigation.connection` polyfill, if we want, in the future. Additionally, because the new APIs expose an object instead of a string, it's easier to extend the APIs in the future by adding keys to the object without causing a breaking change. Note that the W3C's spec doesn't have an "unknown" value for `EffectiveConnectionType`. I chose to introduce this non-standard value because it's possible for the current implementation to not have an `effectiveConnectionType` and I figured it was worth representing this possibility explicitly with "unknown" instead of implicitly with `null`. **Test Plan (required)** Verified that the methods (`fetch` and `getConnection`) and the events (`change` and `connectionchange`) return the correct data on iOS and Android when connected to a wifi network and a 4G cellular network. Verified that switching networks causes the event to fire with the correct information. Verified that the old APIs (`fetch' and 'change') emit a deprecation warning when used. My team is using a similar patch in our app. Adam Comella Microsoft Corp. Closes https://github.com/facebook/react-native/pull/14618 Differential Revision: D5459593 Pulled By: shergin fbshipit-source-id: f1e6c5d572bb3e2669fbd4ba7d0fbb106525280e
2017-07-25 01:42:27 +03:00
*
* ### Connectivity Types (deprecated)
*
* 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.
*/
const NetInfo = {
/**
DEPRECATION: Make NetInfo API cross platform and expose whether connection is 2g/3g/4g Summary: This change intends to fix 2 issues with the NetInfo API: - The NetInfo API is currently platform-specific. It returns completely different values on iOS and Android. - The NetInfo API currently doesn't expose a way to determine whether the connection is 2g, 3g, or 4g. The NetInfo API currently just exposes a string-based enum representing the connectivity type. The string values are different between iOS and Andorid. Because of this design, it's not obvious how to achieve the goals of this change without making a breaking change. Consequently, this change deprecates the old NetInfo APIs and introduces new ones. Specifically, these are the API changes: - The `fetch` method is deprecated in favor of `getConnection` - The `change` event is deprecated in favor of the `connectionchange` event. - `getConnection`/`connectionchange` use a new set of enum values compared to `fetch`/`change`. See the documentation for the new values. - On iOS, `cell` is now known as `cellular`. It's worth pointing out this one in particular because the old and new names are so similar. The rest of the iOS values have remained the same. - Some of the Android enum values have been removed without a replacement (e.g. `DUMMY`, `MOBILE_DUN`, `MOBILE_HIPRI`, `MOBILE_MMS`, `MOBILE_SUPL`, `VPN`). If desirable, we could find a way to expose these in the new API. For example, we could have a `platformValue` key that exposes the platform's enum values directly (like the old `fetch` API did). `getConnection` and `connectionchange` each expose an object which has 2 keys conveying a `ConnectionType` (e.g. wifi, cellular) and an `EffectiveConnectionType` (e.g. 2g, 3g). These enums and their values are taken directly from the W3C's Network Information API spec (https://wicg.github.io/netinfo/). Copying the W3C's API will make it easy to expose a `navigation.connection` polyfill, if we want, in the future. Additionally, because the new APIs expose an object instead of a string, it's easier to extend the APIs in the future by adding keys to the object without causing a breaking change. Note that the W3C's spec doesn't have an "unknown" value for `EffectiveConnectionType`. I chose to introduce this non-standard value because it's possible for the current implementation to not have an `effectiveConnectionType` and I figured it was worth representing this possibility explicitly with "unknown" instead of implicitly with `null`. **Test Plan (required)** Verified that the methods (`fetch` and `getConnection`) and the events (`change` and `connectionchange`) return the correct data on iOS and Android when connected to a wifi network and a 4G cellular network. Verified that switching networks causes the event to fire with the correct information. Verified that the old APIs (`fetch' and 'change') emit a deprecation warning when used. My team is using a similar patch in our app. Adam Comella Microsoft Corp. Closes https://github.com/facebook/react-native/pull/14618 Differential Revision: D5459593 Pulled By: shergin fbshipit-source-id: f1e6c5d572bb3e2669fbd4ba7d0fbb106525280e
2017-07-25 01:42:27 +03:00
* 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} {
DEPRECATION: Make NetInfo API cross platform and expose whether connection is 2g/3g/4g Summary: This change intends to fix 2 issues with the NetInfo API: - The NetInfo API is currently platform-specific. It returns completely different values on iOS and Android. - The NetInfo API currently doesn't expose a way to determine whether the connection is 2g, 3g, or 4g. The NetInfo API currently just exposes a string-based enum representing the connectivity type. The string values are different between iOS and Andorid. Because of this design, it's not obvious how to achieve the goals of this change without making a breaking change. Consequently, this change deprecates the old NetInfo APIs and introduces new ones. Specifically, these are the API changes: - The `fetch` method is deprecated in favor of `getConnection` - The `change` event is deprecated in favor of the `connectionchange` event. - `getConnection`/`connectionchange` use a new set of enum values compared to `fetch`/`change`. See the documentation for the new values. - On iOS, `cell` is now known as `cellular`. It's worth pointing out this one in particular because the old and new names are so similar. The rest of the iOS values have remained the same. - Some of the Android enum values have been removed without a replacement (e.g. `DUMMY`, `MOBILE_DUN`, `MOBILE_HIPRI`, `MOBILE_MMS`, `MOBILE_SUPL`, `VPN`). If desirable, we could find a way to expose these in the new API. For example, we could have a `platformValue` key that exposes the platform's enum values directly (like the old `fetch` API did). `getConnection` and `connectionchange` each expose an object which has 2 keys conveying a `ConnectionType` (e.g. wifi, cellular) and an `EffectiveConnectionType` (e.g. 2g, 3g). These enums and their values are taken directly from the W3C's Network Information API spec (https://wicg.github.io/netinfo/). Copying the W3C's API will make it easy to expose a `navigation.connection` polyfill, if we want, in the future. Additionally, because the new APIs expose an object instead of a string, it's easier to extend the APIs in the future by adding keys to the object without causing a breaking change. Note that the W3C's spec doesn't have an "unknown" value for `EffectiveConnectionType`. I chose to introduce this non-standard value because it's possible for the current implementation to not have an `effectiveConnectionType` and I figured it was worth representing this possibility explicitly with "unknown" instead of implicitly with `null`. **Test Plan (required)** Verified that the methods (`fetch` and `getConnection`) and the events (`change` and `connectionchange`) return the correct data on iOS and Android when connected to a wifi network and a 4G cellular network. Verified that switching networks causes the event to fire with the correct information. Verified that the old APIs (`fetch' and 'change') emit a deprecation warning when used. My team is using a similar patch in our app. Adam Comella Microsoft Corp. Closes https://github.com/facebook/react-native/pull/14618 Differential Revision: D5459593 Pulled By: shergin fbshipit-source-id: f1e6c5d572bb3e2669fbd4ba7d0fbb106525280e
2017-07-25 01:42:27 +03:00
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: () => {}
};
}
2015-06-13 00:57:23 +03:00
_subscriptions.set(handler, listener);
return {
remove: () => NetInfo.removeEventListener(eventName, handler)
};
},
/**
* Removes the listener for network status changes.
*/
removeEventListener(
eventName: ChangeEventName,
handler: Function
): void {
const listener = _subscriptions.get(handler);
2015-06-13 00:57:23 +03:00
if (!listener) {
return;
}
2015-06-13 00:57:23 +03:00
listener.remove();
_subscriptions.delete(handler);
},
/**
DEPRECATION: Make NetInfo API cross platform and expose whether connection is 2g/3g/4g Summary: This change intends to fix 2 issues with the NetInfo API: - The NetInfo API is currently platform-specific. It returns completely different values on iOS and Android. - The NetInfo API currently doesn't expose a way to determine whether the connection is 2g, 3g, or 4g. The NetInfo API currently just exposes a string-based enum representing the connectivity type. The string values are different between iOS and Andorid. Because of this design, it's not obvious how to achieve the goals of this change without making a breaking change. Consequently, this change deprecates the old NetInfo APIs and introduces new ones. Specifically, these are the API changes: - The `fetch` method is deprecated in favor of `getConnection` - The `change` event is deprecated in favor of the `connectionchange` event. - `getConnection`/`connectionchange` use a new set of enum values compared to `fetch`/`change`. See the documentation for the new values. - On iOS, `cell` is now known as `cellular`. It's worth pointing out this one in particular because the old and new names are so similar. The rest of the iOS values have remained the same. - Some of the Android enum values have been removed without a replacement (e.g. `DUMMY`, `MOBILE_DUN`, `MOBILE_HIPRI`, `MOBILE_MMS`, `MOBILE_SUPL`, `VPN`). If desirable, we could find a way to expose these in the new API. For example, we could have a `platformValue` key that exposes the platform's enum values directly (like the old `fetch` API did). `getConnection` and `connectionchange` each expose an object which has 2 keys conveying a `ConnectionType` (e.g. wifi, cellular) and an `EffectiveConnectionType` (e.g. 2g, 3g). These enums and their values are taken directly from the W3C's Network Information API spec (https://wicg.github.io/netinfo/). Copying the W3C's API will make it easy to expose a `navigation.connection` polyfill, if we want, in the future. Additionally, because the new APIs expose an object instead of a string, it's easier to extend the APIs in the future by adding keys to the object without causing a breaking change. Note that the W3C's spec doesn't have an "unknown" value for `EffectiveConnectionType`. I chose to introduce this non-standard value because it's possible for the current implementation to not have an `effectiveConnectionType` and I figured it was worth representing this possibility explicitly with "unknown" instead of implicitly with `null`. **Test Plan (required)** Verified that the methods (`fetch` and `getConnection`) and the events (`change` and `connectionchange`) return the correct data on iOS and Android when connected to a wifi network and a 4G cellular network. Verified that switching networks causes the event to fire with the correct information. Verified that the old APIs (`fetch' and 'change') emit a deprecation warning when used. My team is using a similar patch in our app. Adam Comella Microsoft Corp. Closes https://github.com/facebook/react-native/pull/14618 Differential Revision: D5459593 Pulled By: shergin fbshipit-source-id: f1e6c5d572bb3e2669fbd4ba7d0fbb106525280e
2017-07-25 01:42:27 +03:00
* This function is deprecated. Use `getConnectionInfo` instead. Returns a promise that
* resolves with one of the deprecated connectivity types listed above.
*/
fetch(): Promise<any> {
DEPRECATION: Make NetInfo API cross platform and expose whether connection is 2g/3g/4g Summary: This change intends to fix 2 issues with the NetInfo API: - The NetInfo API is currently platform-specific. It returns completely different values on iOS and Android. - The NetInfo API currently doesn't expose a way to determine whether the connection is 2g, 3g, or 4g. The NetInfo API currently just exposes a string-based enum representing the connectivity type. The string values are different between iOS and Andorid. Because of this design, it's not obvious how to achieve the goals of this change without making a breaking change. Consequently, this change deprecates the old NetInfo APIs and introduces new ones. Specifically, these are the API changes: - The `fetch` method is deprecated in favor of `getConnection` - The `change` event is deprecated in favor of the `connectionchange` event. - `getConnection`/`connectionchange` use a new set of enum values compared to `fetch`/`change`. See the documentation for the new values. - On iOS, `cell` is now known as `cellular`. It's worth pointing out this one in particular because the old and new names are so similar. The rest of the iOS values have remained the same. - Some of the Android enum values have been removed without a replacement (e.g. `DUMMY`, `MOBILE_DUN`, `MOBILE_HIPRI`, `MOBILE_MMS`, `MOBILE_SUPL`, `VPN`). If desirable, we could find a way to expose these in the new API. For example, we could have a `platformValue` key that exposes the platform's enum values directly (like the old `fetch` API did). `getConnection` and `connectionchange` each expose an object which has 2 keys conveying a `ConnectionType` (e.g. wifi, cellular) and an `EffectiveConnectionType` (e.g. 2g, 3g). These enums and their values are taken directly from the W3C's Network Information API spec (https://wicg.github.io/netinfo/). Copying the W3C's API will make it easy to expose a `navigation.connection` polyfill, if we want, in the future. Additionally, because the new APIs expose an object instead of a string, it's easier to extend the APIs in the future by adding keys to the object without causing a breaking change. Note that the W3C's spec doesn't have an "unknown" value for `EffectiveConnectionType`. I chose to introduce this non-standard value because it's possible for the current implementation to not have an `effectiveConnectionType` and I figured it was worth representing this possibility explicitly with "unknown" instead of implicitly with `null`. **Test Plan (required)** Verified that the methods (`fetch` and `getConnection`) and the events (`change` and `connectionchange`) return the correct data on iOS and Android when connected to a wifi network and a 4G cellular network. Verified that switching networks causes the event to fire with the correct information. Verified that the old APIs (`fetch' and 'change') emit a deprecation warning when used. My team is using a similar patch in our app. Adam Comella Microsoft Corp. Closes https://github.com/facebook/react-native/pull/14618 Differential Revision: D5459593 Pulled By: shergin fbshipit-source-id: f1e6c5d572bb3e2669fbd4ba7d0fbb106525280e
2017-07-25 01:42:27 +03:00
console.warn('NetInfo.fetch() is deprecated. Use NetInfo.getConnectionInfo() instead.');
return RCTNetInfo.getCurrentConnectivity().then(resp => resp.network_info);
},
DEPRECATION: Make NetInfo API cross platform and expose whether connection is 2g/3g/4g Summary: This change intends to fix 2 issues with the NetInfo API: - The NetInfo API is currently platform-specific. It returns completely different values on iOS and Android. - The NetInfo API currently doesn't expose a way to determine whether the connection is 2g, 3g, or 4g. The NetInfo API currently just exposes a string-based enum representing the connectivity type. The string values are different between iOS and Andorid. Because of this design, it's not obvious how to achieve the goals of this change without making a breaking change. Consequently, this change deprecates the old NetInfo APIs and introduces new ones. Specifically, these are the API changes: - The `fetch` method is deprecated in favor of `getConnection` - The `change` event is deprecated in favor of the `connectionchange` event. - `getConnection`/`connectionchange` use a new set of enum values compared to `fetch`/`change`. See the documentation for the new values. - On iOS, `cell` is now known as `cellular`. It's worth pointing out this one in particular because the old and new names are so similar. The rest of the iOS values have remained the same. - Some of the Android enum values have been removed without a replacement (e.g. `DUMMY`, `MOBILE_DUN`, `MOBILE_HIPRI`, `MOBILE_MMS`, `MOBILE_SUPL`, `VPN`). If desirable, we could find a way to expose these in the new API. For example, we could have a `platformValue` key that exposes the platform's enum values directly (like the old `fetch` API did). `getConnection` and `connectionchange` each expose an object which has 2 keys conveying a `ConnectionType` (e.g. wifi, cellular) and an `EffectiveConnectionType` (e.g. 2g, 3g). These enums and their values are taken directly from the W3C's Network Information API spec (https://wicg.github.io/netinfo/). Copying the W3C's API will make it easy to expose a `navigation.connection` polyfill, if we want, in the future. Additionally, because the new APIs expose an object instead of a string, it's easier to extend the APIs in the future by adding keys to the object without causing a breaking change. Note that the W3C's spec doesn't have an "unknown" value for `EffectiveConnectionType`. I chose to introduce this non-standard value because it's possible for the current implementation to not have an `effectiveConnectionType` and I figured it was worth representing this possibility explicitly with "unknown" instead of implicitly with `null`. **Test Plan (required)** Verified that the methods (`fetch` and `getConnection`) and the events (`change` and `connectionchange`) return the correct data on iOS and Android when connected to a wifi network and a 4G cellular network. Verified that switching networks causes the event to fire with the correct information. Verified that the old APIs (`fetch' and 'change') emit a deprecation warning when used. My team is using a similar patch in our app. Adam Comella Microsoft Corp. Closes https://github.com/facebook/react-native/pull/14618 Differential Revision: D5459593 Pulled By: shergin fbshipit-source-id: f1e6c5d572bb3e2669fbd4ba7d0fbb106525280e
2017-07-25 01:42:27 +03:00
/**
* Returns a promise that resolves to an object with `type` and `effectiveType` keys
* whose values are a `ConnectionType` and an `EffectiveConnectionType`, (described above),
* respectively.
*/
getConnectionInfo(): Promise<any> {
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.
* Use this if you are only interested with whether the device has internet
* connectivity.
*/
isConnected: {
addEventListener(
eventName: ChangeEventName,
handler: Function
): {remove: () => void} {
const listener = (connection) => {
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,
/* $FlowFixMe(>=0.36.0 site=react_native_fb,react_native_oss) Flow error
* detected during the deploy of Flow v0.36.0. To see the error, remove
* this comment and run Flow */
listener
);
_isConnectedSubscriptions.delete(handler);
},
fetch(): Promise<any> {
return NetInfo.fetch().then(
(connection) => _isConnected(connection)
);
},
},
isConnectionExpensive(): Promise<boolean> {
return (
Platform.OS === 'android' ? RCTNetInfo.isConnectionMetered() : Promise.reject(new Error('Currently not supported on iOS'))
);
},
};
module.exports = NetInfo;