From acac210adecb590c1434dccbd505bc3ff0582ebd Mon Sep 17 00:00:00 2001 From: smartDev420 Date: Fri, 28 Jun 2019 11:08:58 -0700 Subject: [PATCH] [change] Remove NetInfo Ref #1352 Ref https://github.com/facebook/react-native/issues/23313 --- README.md | 1 - .../src/moduleMap.js | 1 - .../exports/NetInfo/__tests__/index-test.js | 67 ------- .../src/exports/NetInfo/index.js | 169 ------------------ packages/react-native-web/src/index.js | 2 - .../storybook/2-apis/NetInfo/NetInfoScreen.js | 123 ------------- 6 files changed, 363 deletions(-) delete mode 100644 packages/react-native-web/src/exports/NetInfo/__tests__/index-test.js delete mode 100644 packages/react-native-web/src/exports/NetInfo/index.js delete mode 100644 packages/website/storybook/2-apis/NetInfo/NetInfoScreen.js diff --git a/README.md b/README.md index 40b638b5..baaffac6 100644 --- a/README.md +++ b/README.md @@ -182,7 +182,6 @@ React Native v0.55 | NativeEventEmitter | ✓ | | | NativeMethodsMixin | ✓ | | | NativeModules | (✓) | Mocked. Missing ability to load native modules. | -| NetInfo | ✓ | Missing functionality to detect expensive connections as there are no equivalent web APIs. | | PanResponder | ✓ | | | PixelRatio | ✓ | | | Platform | ✓ | | diff --git a/packages/babel-plugin-react-native-web/src/moduleMap.js b/packages/babel-plugin-react-native-web/src/moduleMap.js index d45ce3d4..bb911d56 100644 --- a/packages/babel-plugin-react-native-web/src/moduleMap.js +++ b/packages/babel-plugin-react-native-web/src/moduleMap.js @@ -30,7 +30,6 @@ module.exports = { Modal: true, NativeEventEmitter: true, NativeModules: true, - NetInfo: true, PanResponder: true, PermissionsAndroid: true, Picker: true, diff --git a/packages/react-native-web/src/exports/NetInfo/__tests__/index-test.js b/packages/react-native-web/src/exports/NetInfo/__tests__/index-test.js deleted file mode 100644 index aa063c62..00000000 --- a/packages/react-native-web/src/exports/NetInfo/__tests__/index-test.js +++ /dev/null @@ -1,67 +0,0 @@ -/* eslint-env jasmine, jest */ - -import NetInfo from '..'; - -const handler = () => {}; - -describe('apis/NetInfo', () => { - describe('getConnectionInfo', () => { - test('fills out basic fields', done => { - NetInfo.getConnectionInfo().then(result => { - expect(result.effectiveType).toBeDefined(); - expect(result.type).toBeDefined(); - done(); - }); - }); - }); - - describe('addEventListener', () => { - test('throws if the provided "eventType" is not supported', () => { - expect(() => NetInfo.addEventListener('foo', handler)).toThrow(); - }); - }); - - describe('removeEventListener', () => { - test('throws if the provided "eventType" is not supported', () => { - expect(() => NetInfo.removeEventListener('foo', handler)).toThrow(); - }); - test('throws if the handler is not registered', () => { - expect(() => NetInfo.removeEventListener('connectionChange', handler)).toThrow(); - }); - }); - - describe('isConnected', () => { - afterEach(() => { - try { - NetInfo.isConnected.removeEventListener('connectionChange', handler); - } catch (e) {} - }); - - describe('fetch', () => { - test('returns a boolean', done => { - NetInfo.isConnected.fetch().then(isConnected => { - expect(isConnected).toBe(true); - done(); - }); - }); - }); - - describe('addEventListener', () => { - test('throws if the provided "eventType" is not supported', () => { - expect(() => NetInfo.isConnected.addEventListener('foo', handler)).toThrow(); - }); - }); - - describe('removeEventListener', () => { - test('throws if the provided "eventType" is not supported', () => { - NetInfo.isConnected.addEventListener('connectionChange', handler); - expect(() => NetInfo.isConnected.removeEventListener('foo', handler)).toThrow(); - }); - test('throws if the handler is not registered', () => { - expect(() => - NetInfo.isConnected.removeEventListener('connectionChange', handler) - ).toThrow(); - }); - }); - }); -}); diff --git a/packages/react-native-web/src/exports/NetInfo/index.js b/packages/react-native-web/src/exports/NetInfo/index.js deleted file mode 100644 index 51eba50d..00000000 --- a/packages/react-native-web/src/exports/NetInfo/index.js +++ /dev/null @@ -1,169 +0,0 @@ -/** - * Copyright (c) Nicolas Gallagher. - * 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. - * - * @flow - */ - -import ExecutionEnvironment from 'fbjs/lib/ExecutionEnvironment'; -import findIndex from 'array-find-index'; -import invariant from 'fbjs/lib/invariant'; - -const connection = - ExecutionEnvironment.canUseDOM && - (window.navigator.connection || - window.navigator.mozConnection || - window.navigator.webkitConnection); - -// Prevent the underlying event handlers from leaking and include additional -// properties available in browsers -const getConnectionInfoObject = () => { - const result = { - effectiveType: 'unknown', - type: 'unknown' - }; - if (!connection) { - return result; - } - for (const prop in connection) { - const value = connection[prop]; - if (typeof value !== 'function' && value != null) { - result[prop] = value; - } - } - return result; -}; - -// Map React Native events to browser equivalents -const eventTypesMap = { - change: 'change', - connectionChange: 'change' -}; -const eventTypes = Object.keys(eventTypesMap); - -const connectionListeners = []; -const netInfoListeners = []; - -/** - * Navigator online: https://developer.mozilla.org/en-US/docs/Web/API/NavigatorOnLine/onLine - * Network Connection API: https://developer.mozilla.org/en-US/docs/Web/API/NetworkInformation - */ -const NetInfo = { - addEventListener(type: string, handler: Function): { remove: () => void } { - invariant(eventTypes.indexOf(type) !== -1, 'Trying to subscribe to unknown event: "%s"', type); - if (type === 'change') { - console.warn('Listening to event `change` is deprecated. Use `connectionChange` instead.'); - } - if (!connection) { - console.error( - 'Network Connection API is not supported. Not listening for connection type changes.' - ); - return { - remove: () => {} - }; - } - - const wrappedHandler = () => handler(getConnectionInfoObject()); - netInfoListeners.push([handler, wrappedHandler]); - connection.addEventListener(eventTypesMap[type], wrappedHandler); - return { - remove: () => NetInfo.removeEventListener(eventTypesMap[type], handler) - }; - }, - - removeEventListener(type: string, handler: Function): void { - invariant( - eventTypes.indexOf(type) !== -1, - 'Trying to unsubscribe from unknown event: "%s"', - type - ); - if (type === 'change') { - console.warn('Listening to event `change` is deprecated. Use `connectionChange` instead.'); - } - - const listenerIndex = findIndex(netInfoListeners, pair => pair[0] === handler); - invariant(listenerIndex !== -1, 'Trying to remove NetInfo listener for unregistered handler'); - const [, wrappedHandler] = netInfoListeners[listenerIndex]; - connection.removeEventListener(eventTypesMap[type], wrappedHandler); - netInfoListeners.splice(listenerIndex, 1); - }, - - fetch(): Promise { - console.warn('`fetch` is deprecated. Use `getConnectionInfo` instead.'); - return new Promise((resolve, reject) => { - try { - resolve(connection.type); - } catch (err) { - resolve('unknown'); - } - }); - }, - - getConnectionInfo(): Promise { - return new Promise((resolve, reject) => { - resolve(getConnectionInfoObject()); - }); - }, - - isConnected: { - addEventListener(type: string, handler: Function): { remove: () => void } { - invariant( - eventTypes.indexOf(type) !== -1, - 'Trying to subscribe to unknown event: "%s"', - type - ); - if (type === 'change') { - console.warn('Listening to event `change` is deprecated. Use `connectionChange` instead.'); - } - - const onlineCallback = () => handler(true); - const offlineCallback = () => handler(false); - connectionListeners.push([handler, onlineCallback, offlineCallback]); - - window.addEventListener('online', onlineCallback, false); - window.addEventListener('offline', offlineCallback, false); - - return { - remove: () => NetInfo.isConnected.removeEventListener(eventTypesMap[type], handler) - }; - }, - - removeEventListener(type: string, handler: Function): void { - invariant( - eventTypes.indexOf(type) !== -1, - 'Trying to subscribe to unknown event: "%s"', - type - ); - if (type === 'change') { - console.warn('Listening to event `change` is deprecated. Use `connectionChange` instead.'); - } - - const listenerIndex = findIndex(connectionListeners, pair => pair[0] === handler); - invariant( - listenerIndex !== -1, - 'Trying to remove NetInfo connection listener for unregistered handler' - ); - const [, onlineCallback, offlineCallback] = connectionListeners[listenerIndex]; - - window.removeEventListener('online', onlineCallback, false); - window.removeEventListener('offline', offlineCallback, false); - - connectionListeners.splice(listenerIndex, 1); - }, - - fetch(): Promise { - return new Promise((resolve, reject) => { - try { - resolve(window.navigator.onLine); - } catch (err) { - resolve(true); - } - }); - } - } -}; - -export default NetInfo; diff --git a/packages/react-native-web/src/index.js b/packages/react-native-web/src/index.js index 9fdc6163..fa166221 100644 --- a/packages/react-native-web/src/index.js +++ b/packages/react-native-web/src/index.js @@ -24,7 +24,6 @@ import InteractionManager from './exports/InteractionManager'; import LayoutAnimation from './exports/LayoutAnimation'; import Linking from './exports/Linking'; import NativeEventEmitter from './exports/NativeEventEmitter'; -import NetInfo from './exports/NetInfo'; import PanResponder from './exports/PanResponder'; import PixelRatio from './exports/PixelRatio'; import Platform from './exports/Platform'; @@ -123,7 +122,6 @@ export { LayoutAnimation, Linking, NativeEventEmitter, - NetInfo, PanResponder, PixelRatio, Platform, diff --git a/packages/website/storybook/2-apis/NetInfo/NetInfoScreen.js b/packages/website/storybook/2-apis/NetInfo/NetInfoScreen.js deleted file mode 100644 index 12b77a6e..00000000 --- a/packages/website/storybook/2-apis/NetInfo/NetInfoScreen.js +++ /dev/null @@ -1,123 +0,0 @@ -/** - * @flow - */ - -import React from 'react'; -import UIExplorer, { - AppText, - Code, - Description, - DocItem, - ExternalLink, - Section, - storiesOf -} from '../../ui-explorer'; - -const NetInfoScreen = () => ( - - - - NetInfo asynchronously determines the online/offline status and additional connection - information (where available) of the application. - - - Note that connection type information is limited to how well the browser supports the{' '} - - NetworkInformation API - . Connection types will be unknown when support is missing. - - - -
- - One of bluebooth, cellular, ethernet,{' '} - mixed, mixed, none, other,{' '} - unknown, wifi, wimax - - } - name="ConnectionType" - /> - - One of slow-2g, 2g, 3g, 4g,{' '} - unknown. - - } - name="EffectiveConnectionType" - /> - - {`{ - effectiveType: EffectiveConnectionType; - type: ConnectionType; - downlink?: number; - downlinkMax?: number; - rtt?: number; -}`} - } - name="ConnectionEventType" - /> -
- -
- - Adds an event handler. The connectionChange event fires when the network - status changes. The argument to the event handler is an object of type{' '} - ConnectionEventType. - - } - example={{ - code: `NetInfo.addEventListener('connectionChange', ({ effectiveType, type }) => { - console.log('Effective connection type:', effectiveType); - console.log('Connection type:', type); -})` - }} - name="static addEventListener" - typeInfo="(eventName, handler) => void" - /> - - - Returns a promise that resolves with an object of type ConnectionEventType. - - } - example={{ - code: `NetInfo.getConnectionInfo().then(({ effectiveType, type }) => { - console.log('Effective connection type:', effectiveType); - console.log('Connection type:', type); -});` - }} - name="static getConnectionInfo" - typeInfo="() => Promise" - /> - - -
- -
- { - console.log('Connection status:', (isConnected ? 'online' : 'offline')); -});` - }} - name="isConnected" - typeInfo="ObjectExpression" - /> -
-
-); - -storiesOf('APIs', module).add('NetInfo', NetInfoScreen);