From cb6cbd12f80152b4ce742f37e2e6eefadf89d927 Mon Sep 17 00:00:00 2001 From: Tim Yung Date: Mon, 12 Apr 2021 12:41:15 -0700 Subject: [PATCH] EventEmitter: Deprecate `removeSubscription` 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 --- Libraries/vendor/emitter/_EmitterSubscription.js | 2 +- Libraries/vendor/emitter/_EventEmitter.js | 15 ++++++++++++++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/Libraries/vendor/emitter/_EmitterSubscription.js b/Libraries/vendor/emitter/_EmitterSubscription.js index eba91d9a4f8c22..b69f2daff97fe3 100644 --- a/Libraries/vendor/emitter/_EmitterSubscription.js +++ b/Libraries/vendor/emitter/_EmitterSubscription.js @@ -54,7 +54,7 @@ class EmitterSubscription> * for removing the subscription lies with the EventEmitter. */ remove(): void { - this.emitter.removeSubscription(this); + this.emitter.__removeSubscription(this); } } diff --git a/Libraries/vendor/emitter/_EventEmitter.js b/Libraries/vendor/emitter/_EventEmitter.js index 998f5cec74770d..7650cc9a2242fe 100644 --- a/Libraries/vendor/emitter/_EventEmitter.js +++ b/Libraries/vendor/emitter/_EventEmitter.js @@ -82,6 +82,19 @@ class EventEmitter { */ removeSubscription>( subscription: EmitterSubscription, + ): 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>( + subscription: EmitterSubscription, ): void { invariant( subscription.emitter === this, @@ -147,7 +160,7 @@ class EventEmitter { // FIXME: listeners should return void instead of mixed to prevent issues listener: (...$ElementType) => mixed, ): void { - console.error( + console.warn( `EventEmitter.removeListener('${eventType}', ...): Method has been ` + 'deprecated. Please instead use `remove()` on the subscription ' + 'returned by `EventEmitter.addListener`.',