Skip to content

Commit

Permalink
EventEmitter: Deprecate removeSubscription
Browse files Browse the repository at this point in the history
Summary:
Deprecates `EventEmitter#removeSubscription`. This required temporarily introducing a new `__removeSubscription` method that is only invoked by `EmitterSubscription`. This is necessary so that we do not completely break usages of `EventEmitter` that are supplying constructor arguments (which is also deprecated, but still supported until the next release).

Calling this method will now cause a warning to appear with instructions to instead invoke `remove()` on the subscription itself.

Lastly, changed `console.error` deprecation notice to instead use `console.warn`. This is in line with the principle that errors are "broken today" and warnings will be "broken tomorrow".

Changelog:
[General][Deprecated] - `EventEmitter#removeSubscription` is now deprecated.

Reviewed By: rubennorte

Differential Revision: D27704279

fbshipit-source-id: 581f5b2ab46b1bcfc1d20898b3d3392988dccbd5
  • Loading branch information
yungsters authored and facebook-github-bot committed Apr 12, 2021
1 parent 62934e5 commit cb6cbd1
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Libraries/vendor/emitter/_EmitterSubscription.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class EmitterSubscription<EventDefinitions: {...}, K: $Keys<EventDefinitions>>
* for removing the subscription lies with the EventEmitter.
*/
remove(): void {
this.emitter.removeSubscription(this);
this.emitter.__removeSubscription(this);
}
}

Expand Down
15 changes: 14 additions & 1 deletion Libraries/vendor/emitter/_EventEmitter.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,19 @@ class EventEmitter<EventDefinitions: {...}> {
*/
removeSubscription<K: $Keys<EventDefinitions>>(
subscription: EmitterSubscription<EventDefinitions, K>,
): void {
console.warn(
'EventEmitter.removeSubscription(...): Method has been deprecated. ' +
'Please instead use `remove()` on the subscription itself.',
);
this.__removeSubscription(subscription);
}

/**
* Called by `EmitterSubscription` to bypass the above deprecation warning.
*/
__removeSubscription<K: $Keys<EventDefinitions>>(
subscription: EmitterSubscription<EventDefinitions, K>,
): void {
invariant(
subscription.emitter === this,
Expand Down Expand Up @@ -147,7 +160,7 @@ class EventEmitter<EventDefinitions: {...}> {
// FIXME: listeners should return void instead of mixed to prevent issues
listener: (...$ElementType<EventDefinitions, K>) => mixed,
): void {
console.error(
console.warn(
`EventEmitter.removeListener('${eventType}', ...): Method has been ` +
'deprecated. Please instead use `remove()` on the subscription ' +
'returned by `EventEmitter.addListener`.',
Expand Down

0 comments on commit cb6cbd1

Please sign in to comment.