Skip to content

Commit 035718b

Browse files
yungstersfacebook-github-bot
authored andcommitted
RN: Restore Deprecated Event Methods
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
1 parent c47a035 commit 035718b

File tree

5 files changed

+75
-13
lines changed

5 files changed

+75
-13
lines changed

Libraries/AppState/AppState.js

+33
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,39 @@ class AppState {
123123
}
124124
throw new Error('Trying to subscribe to unknown event: ' + type);
125125
}
126+
127+
/**
128+
* @deprecated Use `remove` on the EventSubscription from `addEventListener`.
129+
*/
130+
removeEventListener<K: $Keys<AppStateEventDefinitions>>(
131+
type: K,
132+
listener: (...$ElementType<AppStateEventDefinitions, K>) => mixed,
133+
): void {
134+
const emitter = this._emitter;
135+
if (emitter == null) {
136+
throw new Error('Cannot use AppState when `isAvailable` is false.');
137+
}
138+
// NOTE: This will report a deprecation notice via `console.error`.
139+
switch (type) {
140+
case 'change':
141+
// $FlowIssue[invalid-tuple-arity] Flow cannot refine handler based on the event type
142+
// $FlowIssue[incompatible-call]
143+
emitter.removeListener('appStateDidChange', listener);
144+
return;
145+
case 'memoryWarning':
146+
// $FlowIssue[invalid-tuple-arity] Flow cannot refine handler based on the event type
147+
// $FlowIssue[incompatible-call]
148+
emitter.removeListener('memoryWarning', listener);
149+
return;
150+
case 'blur':
151+
case 'focus':
152+
// $FlowIssue[invalid-tuple-arity] Flow cannot refine handler based on the event type
153+
// $FlowIssue[incompatible-call]
154+
emitter.addListener('appStateFocusChange', listener);
155+
return;
156+
}
157+
throw new Error('Trying to unsubscribe from unknown event: ' + type);
158+
}
126159
}
127160

128161
module.exports = (new AppState(): AppState);

Libraries/Components/Keyboard/Keyboard.js

+11
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,17 @@ class Keyboard {
137137
return this._emitter.addListener(eventType, listener);
138138
}
139139

140+
/**
141+
* @deprecated Use `remove` on the EventSubscription from `addEventListener`.
142+
*/
143+
removeEventListener<K: $Keys<KeyboardEventDefinitions>>(
144+
eventType: K,
145+
listener: (...$ElementType<KeyboardEventDefinitions, K>) => mixed,
146+
): void {
147+
// NOTE: This will report a deprecation notice via `console.error`.
148+
this._emitter.removeListener(eventType, listener);
149+
}
150+
140151
/**
141152
* Removes all listeners for a specific event type.
142153
*

Libraries/EventEmitter/NativeEventEmitter.js

+13
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,19 @@ export default class NativeEventEmitter<TEventToArgsMap: {...}>
7474
};
7575
}
7676

77+
/**
78+
* @deprecated Use `remove` on the EventSubscription from `addListener`.
79+
*/
80+
removeListener<TEvent: $Keys<TEventToArgsMap>>(
81+
eventType: TEvent,
82+
listener: (...args: $ElementType<TEventToArgsMap, TEvent>) => mixed,
83+
): void {
84+
this._nativeModule?.removeListeners(1);
85+
// NOTE: This will report a deprecation notice via `console.error`.
86+
// $FlowFixMe[prop-missing] - `removeListener` exists but is deprecated.
87+
RCTDeviceEventEmitter.removeListener(eventType, listener);
88+
}
89+
7790
emit<TEvent: $Keys<TEventToArgsMap>>(
7891
eventType: TEvent,
7992
...args: $ElementType<TEventToArgsMap, TEvent>

Libraries/Linking/Linking.js

+11
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,17 @@ class Linking extends NativeEventEmitter<LinkingEventDefinitions> {
4646
return this.addListener(eventType, listener);
4747
}
4848

49+
/**
50+
* @deprecated Use `remove` on the EventSubscription from `addEventListener`.
51+
*/
52+
removeEventListener<K: $Keys<LinkingEventDefinitions>>(
53+
eventType: K,
54+
listener: (...$ElementType<LinkingEventDefinitions, K>) => mixed,
55+
): void {
56+
// NOTE: This will report a deprecation notice via `console.error`.
57+
this.removeListener(eventType, listener);
58+
}
59+
4960
/**
5061
* Try to open the given `url` with any of the installed apps.
5162
*

Libraries/vendor/emitter/_EventEmitter.js

+7-13
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,7 @@ class EventEmitter<EventDefinitions: {...}>
9797
}
9898

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

162161
/**
163-
* Removes the given listener for event of specific type.
164-
*
165-
* @param {string} eventType - Name of the event to emit
166-
* @param {function} listener - Function to invoke when the specified event is
167-
* emitted
168-
*
169-
* @example
170-
* emitter.removeListener('someEvent', function(message) {
171-
* console.log(message);
172-
* }); // removes the listener if already registered
173-
*
162+
* @deprecated Use `remove` on the EventSubscription from `addListener`.
174163
*/
175164
removeListener<K: $Keys<EventDefinitions>>(
176165
eventType: K,
177166
// FIXME: listeners should return void instead of mixed to prevent issues
178167
listener: (...$ElementType<EventDefinitions, K>) => mixed,
179168
): void {
169+
console.error(
170+
`EventEmitter.removeListener('${eventType}', ...): Method has been ` +
171+
'deprecated. Please instead use `remove()` on the subscription ' +
172+
'returned by `EventEmitter.addListener`.',
173+
);
180174
const subscriptions = this._subscriber.getSubscriptionsForType(eventType);
181175
if (subscriptions) {
182176
for (let i = 0, l = subscriptions.length; i < l; i++) {

0 commit comments

Comments
 (0)