From 87517aeecbb8e47d8f66a567423ae03d9cb6dca4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20Norte?= Date: Mon, 12 Apr 2021 06:23:53 -0700 Subject: [PATCH] Codemod usages of NativeEventEmitter in react-native-github to only pass a native module on iOS Summary: Changelog: [Internal] Reviewed By: yungsters Differential Revision: D27500993 fbshipit-source-id: 99b33e25daf46b7d1debc6435dd32505ad2cf855 --- Libraries/Animated/NativeAnimatedHelper.js | 6 +++++- Libraries/AppState/AppState.js | 5 ++++- Libraries/Components/Keyboard/Keyboard.js | 5 ++++- Libraries/Modal/Modal.js | 6 +++++- Libraries/PushNotificationIOS/PushNotificationIOS.js | 5 ++++- Libraries/Utilities/Appearance.js | 5 ++++- Libraries/Utilities/DevSettings.js | 5 ++++- Libraries/WebSocket/WebSocket.js | 7 ++++++- Libraries/WebSocket/__tests__/WebSocket-test.js | 1 + 9 files changed, 37 insertions(+), 8 deletions(-) diff --git a/Libraries/Animated/NativeAnimatedHelper.js b/Libraries/Animated/NativeAnimatedHelper.js index 8b69b4cd80a..dd1fc6f2609 100644 --- a/Libraries/Animated/NativeAnimatedHelper.js +++ b/Libraries/Animated/NativeAnimatedHelper.js @@ -397,7 +397,11 @@ module.exports = { // $FlowExpectedError[missing-type-arg] - unsafe getter lint suppresion get nativeEventEmitter(): NativeEventEmitter { if (!nativeEventEmitter) { - nativeEventEmitter = new NativeEventEmitter(NativeAnimatedModule); + nativeEventEmitter = new NativeEventEmitter( + // T88715063: NativeEventEmitter only used this parameter on iOS. Now it uses it on all platforms, so this code was modified automatically to preserve its behavior + // If you want to use the native module on other platforms, please remove this condition and test its behavior + Platform.OS !== 'ios' ? null : NativeAnimatedModule, + ); } return nativeEventEmitter; }, diff --git a/Libraries/AppState/AppState.js b/Libraries/AppState/AppState.js index c6b6a948641..303c97ed28e 100644 --- a/Libraries/AppState/AppState.js +++ b/Libraries/AppState/AppState.js @@ -12,6 +12,7 @@ import {type EventSubscription} from '../vendor/emitter/EventEmitter'; import NativeEventEmitter from '../EventEmitter/NativeEventEmitter'; import logError from '../Utilities/logError'; import NativeAppState from './NativeAppState'; +import Platform from '../Utilities/Platform'; export type AppStateValues = 'inactive' | 'background' | 'active'; @@ -47,7 +48,9 @@ class AppState { this.isAvailable = true; const emitter: NativeEventEmitter = new NativeEventEmitter( - NativeAppState, + // T88715063: NativeEventEmitter only used this parameter on iOS. Now it uses it on all platforms, so this code was modified automatically to preserve its behavior + // If you want to use the native module on other platforms, please remove this condition and test its behavior + Platform.OS !== 'ios' ? null : NativeAppState, ); this._emitter = emitter; diff --git a/Libraries/Components/Keyboard/Keyboard.js b/Libraries/Components/Keyboard/Keyboard.js index 46f95c771c2..6227b975adf 100644 --- a/Libraries/Components/Keyboard/Keyboard.js +++ b/Libraries/Components/Keyboard/Keyboard.js @@ -11,6 +11,7 @@ import NativeEventEmitter from '../../EventEmitter/NativeEventEmitter'; import LayoutAnimation from '../../LayoutAnimation/LayoutAnimation'; import dismissKeyboard from '../../Utilities/dismissKeyboard'; +import Platform from '../../Utilities/Platform'; import NativeKeyboardObserver from './NativeKeyboardObserver'; import {type EventSubscription} from '../../vendor/emitter/EventEmitter'; @@ -103,7 +104,9 @@ type KeyboardEventDefinitions = { class Keyboard { _emitter: NativeEventEmitter = new NativeEventEmitter( - NativeKeyboardObserver, + // T88715063: NativeEventEmitter only used this parameter on iOS. Now it uses it on all platforms, so this code was modified automatically to preserve its behavior + // If you want to use the native module on other platforms, please remove this condition and test its behavior + Platform.OS !== 'ios' ? null : NativeKeyboardObserver, ); /** diff --git a/Libraries/Modal/Modal.js b/Libraries/Modal/Modal.js index 6b6688cf5b0..9a8eebfaf93 100644 --- a/Libraries/Modal/Modal.js +++ b/Libraries/Modal/Modal.js @@ -33,7 +33,11 @@ type ModalEventDefinitions = { const ModalEventEmitter = Platform.OS === 'ios' && NativeModalManager != null - ? new NativeEventEmitter(NativeModalManager) + ? new NativeEventEmitter( + // T88715063: NativeEventEmitter only used this parameter on iOS. Now it uses it on all platforms, so this code was modified automatically to preserve its behavior + // If you want to use the native module on other platforms, please remove this condition and test its behavior + Platform.OS !== 'ios' ? null : NativeModalManager, + ) : null; /** diff --git a/Libraries/PushNotificationIOS/PushNotificationIOS.js b/Libraries/PushNotificationIOS/PushNotificationIOS.js index efe9b6132e7..288d06d9a7b 100644 --- a/Libraries/PushNotificationIOS/PushNotificationIOS.js +++ b/Libraries/PushNotificationIOS/PushNotificationIOS.js @@ -11,6 +11,7 @@ import NativeEventEmitter from '../EventEmitter/NativeEventEmitter'; import NativePushNotificationManagerIOS from './NativePushNotificationManagerIOS'; import invariant from 'invariant'; +import Platform from '../Utilities/Platform'; type NativePushNotificationIOSEventDefinitions = { remoteNotificationReceived: [ @@ -37,7 +38,9 @@ type NativePushNotificationIOSEventDefinitions = { }; const PushNotificationEmitter = new NativeEventEmitter( - NativePushNotificationManagerIOS, + // T88715063: NativeEventEmitter only used this parameter on iOS. Now it uses it on all platforms, so this code was modified automatically to preserve its behavior + // If you want to use the native module on other platforms, please remove this condition and test its behavior + Platform.OS !== 'ios' ? null : NativePushNotificationManagerIOS, ); const _notifHandlers = new Map(); diff --git a/Libraries/Utilities/Appearance.js b/Libraries/Utilities/Appearance.js index 1d633fef098..ea4c26ac7b3 100644 --- a/Libraries/Utilities/Appearance.js +++ b/Libraries/Utilities/Appearance.js @@ -18,6 +18,7 @@ import NativeAppearance, { } from './NativeAppearance'; import invariant from 'invariant'; import {isAsyncDebugging} from './DebugEnvironment'; +import Platform from '../Utilities/Platform'; type AppearanceListener = (preferences: AppearancePreferences) => void; const eventEmitter = new EventEmitter<{ @@ -30,7 +31,9 @@ type NativeAppearanceEventDefinitions = { if (NativeAppearance) { const nativeEventEmitter = new NativeEventEmitter( - NativeAppearance, + // T88715063: NativeEventEmitter only used this parameter on iOS. Now it uses it on all platforms, so this code was modified automatically to preserve its behavior + // If you want to use the native module on other platforms, please remove this condition and test its behavior + Platform.OS !== 'ios' ? null : NativeAppearance, ); nativeEventEmitter.addListener( 'appearanceChanged', diff --git a/Libraries/Utilities/DevSettings.js b/Libraries/Utilities/DevSettings.js index b0c59d4e425..651016a9b34 100644 --- a/Libraries/Utilities/DevSettings.js +++ b/Libraries/Utilities/DevSettings.js @@ -10,6 +10,7 @@ import NativeDevSettings from '../NativeModules/specs/NativeDevSettings'; import NativeEventEmitter from '../EventEmitter/NativeEventEmitter'; +import Platform from '../Utilities/Platform'; let DevSettings: { addMenuItem(title: string, handler: () => mixed): void, @@ -27,7 +28,9 @@ type DevSettingsEventDefinitions = { if (__DEV__) { const emitter = new NativeEventEmitter( - NativeDevSettings, + // T88715063: NativeEventEmitter only used this parameter on iOS. Now it uses it on all platforms, so this code was modified automatically to preserve its behavior + // If you want to use the native module on other platforms, please remove this condition and test its behavior + Platform.OS !== 'ios' ? null : NativeDevSettings, ); const subscriptions = new Map(); diff --git a/Libraries/WebSocket/WebSocket.js b/Libraries/WebSocket/WebSocket.js index 66ae07ff6ca..70cc24729e5 100644 --- a/Libraries/WebSocket/WebSocket.js +++ b/Libraries/WebSocket/WebSocket.js @@ -13,6 +13,7 @@ import type {BlobData} from '../Blob/BlobTypes'; import BlobManager from '../Blob/BlobManager'; import NativeEventEmitter from '../EventEmitter/NativeEventEmitter'; import binaryToBase64 from '../Utilities/binaryToBase64'; +import Platform from '../Utilities/Platform'; import type {EventSubscription} from '../vendor/emitter/EventEmitter'; import NativeWebSocketModule from './NativeWebSocketModule'; import WebSocketEvent from './WebSocketEvent'; @@ -131,7 +132,11 @@ class WebSocket extends (EventTarget(...WEBSOCKET_EVENTS): any) { protocols = null; } - this._eventEmitter = new NativeEventEmitter(NativeWebSocketModule); + this._eventEmitter = new NativeEventEmitter( + // T88715063: NativeEventEmitter only used this parameter on iOS. Now it uses it on all platforms, so this code was modified automatically to preserve its behavior + // If you want to use the native module on other platforms, please remove this condition and test its behavior + Platform.OS !== 'ios' ? null : NativeWebSocketModule, + ); this._socketId = nextWebSocketId++; this._registerEvents(); NativeWebSocketModule.connect(url, protocols, {headers}, this._socketId); diff --git a/Libraries/WebSocket/__tests__/WebSocket-test.js b/Libraries/WebSocket/__tests__/WebSocket-test.js index 5b50783cdab..52d8fb5b375 100644 --- a/Libraries/WebSocket/__tests__/WebSocket-test.js +++ b/Libraries/WebSocket/__tests__/WebSocket-test.js @@ -15,6 +15,7 @@ jest.setMock('../../BatchedBridge/NativeModules', { WebSocketModule: { connect: () => {}, }, + PlatformConstants: {}, }); const WebSocket = require('../WebSocket');