From 2596b2f6954362d2cd34a1be870810ab90cbb916 Mon Sep 17 00:00:00 2001 From: Matin Zadeh Dolatabad Date: Wed, 4 May 2022 11:32:51 -0700 Subject: [PATCH] fix: remove deprecated removeListener methods (#33580) Summary: Remove old deprecated modules that cause annoying warnings. This can be a breaking change for some third-party modules. ## Changelog [General] [Removed] - Remove deprecated removeListener methods Pull Request resolved: https://github.com/facebook/react-native/pull/33580 Test Plan: See `flow-check` and `build-arvr-js-flow` succeed in Sandcastle. Reviewed By: cortinico Differential Revision: D35549719 Pulled By: yungsters fbshipit-source-id: 0495e36de19db434362d5de56463d9c1ad6edd73 --- Libraries/AppState/AppState.js | 32 ------------------- .../AccessibilityInfo/AccessibilityInfo.js | 20 ------------ Libraries/Components/Keyboard/Keyboard.js | 11 ------- Libraries/EventEmitter/NativeEventEmitter.js | 13 -------- Libraries/Linking/Linking.js | 11 ------- Libraries/Utilities/Dimensions.js | 13 -------- Libraries/vendor/emitter/_EventEmitter.js | 27 ---------------- .../Accessibility/AccessibilityExample.js | 7 ++-- 8 files changed, 5 insertions(+), 129 deletions(-) diff --git a/Libraries/AppState/AppState.js b/Libraries/AppState/AppState.js index 4c091c00e8..99e731bd43 100644 --- a/Libraries/AppState/AppState.js +++ b/Libraries/AppState/AppState.js @@ -127,38 +127,6 @@ class AppState { } throw new Error('Trying to subscribe to unknown event: ' + type); } - - /** - * @deprecated Use `remove` on the EventSubscription from `addEventListener`. - */ - removeEventListener>( - type: K, - listener: (...$ElementType) => mixed, - ): void { - const emitter = this._emitter; - if (emitter == null) { - throw new Error('Cannot use AppState when `isAvailable` is false.'); - } - // NOTE: This will report a deprecation notice via `console.error`. - switch (type) { - case 'change': - // $FlowIssue[invalid-tuple-arity] Flow cannot refine handler based on the event type - // $FlowIssue[incompatible-call] - emitter.removeListener('appStateDidChange', listener); - return; - case 'memoryWarning': - // $FlowIssue[invalid-tuple-arity] Flow cannot refine handler based on the event type - emitter.removeListener('memoryWarning', listener); - return; - case 'blur': - case 'focus': - // $FlowIssue[invalid-tuple-arity] Flow cannot refine handler based on the event type - // $FlowIssue[incompatible-call] - emitter.removeListener('appStateFocusChange', listener); - return; - } - throw new Error('Trying to unsubscribe from unknown event: ' + type); - } } module.exports = (new AppState(): AppState); diff --git a/Libraries/Components/AccessibilityInfo/AccessibilityInfo.js b/Libraries/Components/AccessibilityInfo/AccessibilityInfo.js index 53b0db33f8..150635d853 100644 --- a/Libraries/Components/AccessibilityInfo/AccessibilityInfo.js +++ b/Libraries/Components/AccessibilityInfo/AccessibilityInfo.js @@ -12,7 +12,6 @@ import RCTDeviceEventEmitter from '../../EventEmitter/RCTDeviceEventEmitter'; import {sendAccessibilityEvent} from '../../Renderer/shims/ReactNative'; import type {HostComponent} from '../../Renderer/shims/ReactNativeTypes'; import Platform from '../../Utilities/Platform'; -import type EventEmitter from '../../vendor/emitter/EventEmitter'; import type {EventSubscription} from '../../vendor/emitter/EventEmitter'; import NativeAccessibilityInfoAndroid from './NativeAccessibilityInfo'; import NativeAccessibilityManagerIOS from './NativeAccessibilityManager'; @@ -365,25 +364,6 @@ const AccessibilityInfo = { } }, - /** - * @deprecated Use `remove` on the EventSubscription from `addEventListener`. - */ - removeEventListener>( - eventName: K, - handler: (...$ElementType) => void, - ): void { - // NOTE: This will report a deprecation notice via `console.error`. - const deviceEventName = EventNames.get(eventName); - if (deviceEventName != null) { - // $FlowIgnore[incompatible-cast] - (RCTDeviceEventEmitter: EventEmitter<$FlowFixMe>).removeListener( - 'deviceEventName', - // $FlowFixMe[invalid-tuple-arity] - handler, - ); - } - }, - /** * Get the recommended timeout for changes to the UI needed by this user. * diff --git a/Libraries/Components/Keyboard/Keyboard.js b/Libraries/Components/Keyboard/Keyboard.js index 84d8a33513..e7a7881742 100644 --- a/Libraries/Components/Keyboard/Keyboard.js +++ b/Libraries/Components/Keyboard/Keyboard.js @@ -141,17 +141,6 @@ class Keyboard { return this._emitter.addListener(eventType, listener); } - /** - * @deprecated Use `remove` on the EventSubscription from `addListener`. - */ - removeListener>( - eventType: K, - listener: (...$ElementType) => mixed, - ): void { - // NOTE: This will report a deprecation notice via `console.error`. - this._emitter.removeListener(eventType, listener); - } - /** * Removes all listeners for a specific event type. * diff --git a/Libraries/EventEmitter/NativeEventEmitter.js b/Libraries/EventEmitter/NativeEventEmitter.js index 1c1d5366da..402faec528 100644 --- a/Libraries/EventEmitter/NativeEventEmitter.js +++ b/Libraries/EventEmitter/NativeEventEmitter.js @@ -95,19 +95,6 @@ export default class NativeEventEmitter }; } - /** - * @deprecated Use `remove` on the EventSubscription from `addListener`. - */ - removeListener>( - eventType: TEvent, - listener: (...args: $ElementType) => mixed, - ): void { - this._nativeModule?.removeListeners(1); - // NOTE: This will report a deprecation notice via `console.error`. - // $FlowFixMe[prop-missing] - `removeListener` exists but is deprecated. - RCTDeviceEventEmitter.removeListener(eventType, listener); - } - emit>( eventType: TEvent, ...args: $ElementType diff --git a/Libraries/Linking/Linking.js b/Libraries/Linking/Linking.js index 66124e637e..63575220f5 100644 --- a/Libraries/Linking/Linking.js +++ b/Libraries/Linking/Linking.js @@ -46,17 +46,6 @@ class Linking extends NativeEventEmitter { return this.addListener(eventType, listener); } - /** - * @deprecated Use `remove` on the EventSubscription from `addEventListener`. - */ - removeEventListener>( - eventType: K, - listener: (...$ElementType) => mixed, - ): void { - // NOTE: This will report a deprecation notice via `console.error`. - this.removeListener(eventType, listener); - } - /** * Try to open the given `url` with any of the installed apps. * diff --git a/Libraries/Utilities/Dimensions.js b/Libraries/Utilities/Dimensions.js index e0e0a427ba..6f499cc94f 100644 --- a/Libraries/Utilities/Dimensions.js +++ b/Libraries/Utilities/Dimensions.js @@ -108,19 +108,6 @@ class Dimensions { ); return eventEmitter.addListener(type, handler); } - - /** - * @deprecated Use `remove` on the EventSubscription from `addEventListener`. - */ - static removeEventListener(type: 'change', handler: Function) { - invariant( - type === 'change', - 'Trying to remove listener for unknown event: "%s"', - type, - ); - // NOTE: This will report a deprecation notice via `console.error`. - eventEmitter.removeListener(type, handler); - } } let initialDims: ?$ReadOnly = diff --git a/Libraries/vendor/emitter/_EventEmitter.js b/Libraries/vendor/emitter/_EventEmitter.js index b5e5a05b75..38addf8b7b 100644 --- a/Libraries/vendor/emitter/_EventEmitter.js +++ b/Libraries/vendor/emitter/_EventEmitter.js @@ -152,33 +152,6 @@ class EventEmitter { } } } - - /** - * @deprecated Use `remove` on the EventSubscription from `addListener`. - */ - removeListener>( - eventType: K, - // FIXME: listeners should return void instead of mixed to prevent issues - listener: (...$ElementType) => mixed, - ): void { - console.warn( - `EventEmitter.removeListener('${eventType}', ...): Method has been ` + - 'deprecated. Please instead use `remove()` on the subscription ' + - 'returned by `EventEmitter.addListener`.', - ); - const subscriptions = this._subscriber.getSubscriptionsForType(eventType); - if (subscriptions) { - for (let i = 0, l = subscriptions.length; i < l; i++) { - const subscription = subscriptions[i]; - - // The subscription may have been removed during this event loop. - // its listener matches the listener in method parameters - if (subscription && subscription.listener === listener) { - subscription.remove(); - } - } - } - } } module.exports = EventEmitter; diff --git a/packages/rn-tester/js/examples/Accessibility/AccessibilityExample.js b/packages/rn-tester/js/examples/Accessibility/AccessibilityExample.js index 7c7e74c0d8..ce8151ce8f 100644 --- a/packages/rn-tester/js/examples/Accessibility/AccessibilityExample.js +++ b/packages/rn-tester/js/examples/Accessibility/AccessibilityExample.js @@ -1151,12 +1151,15 @@ class DisplayOptionsStatusExample extends React.Component<{}> { function DisplayOptionStatusExample({optionName, optionChecker, notification}) { const [statusEnabled, setStatusEnabled] = React.useState(false); React.useEffect(() => { - AccessibilityInfo.addEventListener(notification, setStatusEnabled); + const listener = AccessibilityInfo.addEventListener( + notification, + setStatusEnabled, + ); optionChecker().then(isEnabled => { setStatusEnabled(isEnabled); }); return function cleanup() { - AccessibilityInfo.removeEventListener(notification, setStatusEnabled); + listener.remove(); }; }, [optionChecker, notification]); return (