Skip to content

Commit

Permalink
RN: Restore Deprecated Event Methods
Browse files Browse the repository at this point in the history
Summary:
Restore deprecated event listener removal methods in order to minimize breaking changes for the next release. The methods will work, but they will not report a warning via `console.error`.

Changelog:
[General][Added] - `EventEmitter.removeListener` now emits a deprecation notice.
[General][Added] - Restored `AppState.removeEventListener` with a deprecation notice.
[General][Added] - Restored `Keyboard.removeEventListener` with a deprecation notice.
[General][Added] - Restored `Linking.removeEventListener` with a deprecation notice.

Reviewed By: nadiia, kacieb

Differential Revision: D26589441

fbshipit-source-id: 7d89982a182cf2163136e157d4c1beee91c30393
  • Loading branch information
yungsters authored and facebook-github-bot committed Mar 11, 2021
1 parent c47a035 commit 035718b
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 13 deletions.
33 changes: 33 additions & 0 deletions Libraries/AppState/AppState.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,39 @@ class AppState {
}
throw new Error('Trying to subscribe to unknown event: ' + type);
}

/**
* @deprecated Use `remove` on the EventSubscription from `addEventListener`.
*/
removeEventListener<K: $Keys<AppStateEventDefinitions>>(
type: K,
listener: (...$ElementType<AppStateEventDefinitions, K>) => 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
// $FlowIssue[incompatible-call]
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.addListener('appStateFocusChange', listener);
return;
}
throw new Error('Trying to unsubscribe from unknown event: ' + type);
}
}

module.exports = (new AppState(): AppState);
11 changes: 11 additions & 0 deletions Libraries/Components/Keyboard/Keyboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,17 @@ class Keyboard {
return this._emitter.addListener(eventType, listener);
}

/**
* @deprecated Use `remove` on the EventSubscription from `addEventListener`.
*/
removeEventListener<K: $Keys<KeyboardEventDefinitions>>(
eventType: K,
listener: (...$ElementType<KeyboardEventDefinitions, K>) => 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.
*
Expand Down
13 changes: 13 additions & 0 deletions Libraries/EventEmitter/NativeEventEmitter.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,19 @@ export default class NativeEventEmitter<TEventToArgsMap: {...}>
};
}

/**
* @deprecated Use `remove` on the EventSubscription from `addListener`.
*/
removeListener<TEvent: $Keys<TEventToArgsMap>>(
eventType: TEvent,
listener: (...args: $ElementType<TEventToArgsMap, TEvent>) => 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<TEvent: $Keys<TEventToArgsMap>>(
eventType: TEvent,
...args: $ElementType<TEventToArgsMap, TEvent>
Expand Down
11 changes: 11 additions & 0 deletions Libraries/Linking/Linking.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,17 @@ class Linking extends NativeEventEmitter<LinkingEventDefinitions> {
return this.addListener(eventType, listener);
}

/**
* @deprecated Use `remove` on the EventSubscription from `addEventListener`.
*/
removeEventListener<K: $Keys<LinkingEventDefinitions>>(
eventType: K,
listener: (...$ElementType<LinkingEventDefinitions, K>) => 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.
*
Expand Down
20 changes: 7 additions & 13 deletions Libraries/vendor/emitter/_EventEmitter.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,7 @@ class EventEmitter<EventDefinitions: {...}>
}

/**
* Removes a specific subscription. Called by the `remove()` method of the
* subscription itself to ensure any necessary cleanup is performed.
* @deprecated Use `remove` on the EventSubscription from `addListener`.
*/
removeSubscription<K: $Keys<EventDefinitions>>(
subscription: EmitterSubscription<EventDefinitions, K>,
Expand Down Expand Up @@ -160,23 +159,18 @@ class EventEmitter<EventDefinitions: {...}>
}

/**
* Removes the given listener for event of specific type.
*
* @param {string} eventType - Name of the event to emit
* @param {function} listener - Function to invoke when the specified event is
* emitted
*
* @example
* emitter.removeListener('someEvent', function(message) {
* console.log(message);
* }); // removes the listener if already registered
*
* @deprecated Use `remove` on the EventSubscription from `addListener`.
*/
removeListener<K: $Keys<EventDefinitions>>(
eventType: K,
// FIXME: listeners should return void instead of mixed to prevent issues
listener: (...$ElementType<EventDefinitions, K>) => mixed,
): void {
console.error(
`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++) {
Expand Down

0 comments on commit 035718b

Please sign in to comment.