From c2de1793e18fc0ed107b8f5e5345d57337110764 Mon Sep 17 00:00:00 2001 From: Mike Hardy Date: Tue, 16 Mar 2021 17:26:30 -0500 Subject: [PATCH] fix(listeners): port Emitter.once to analogous addListener/remove API react-native 0.64 removes Emitter.once, but the same effect is achievable by adding a listener, then in the listener removing the subscription after executing the callback reference: https://github.com/facebook/react-native/commit/87a2e29f5928c2e09ac9a98c54732d5f697d8e61 --- packages/auth/lib/PhoneAuthListener.js | 15 ++++++++++++--- packages/database/lib/DatabaseSyncTree.js | 8 ++++---- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/packages/auth/lib/PhoneAuthListener.js b/packages/auth/lib/PhoneAuthListener.js index 46a5bcb2f6..171da0cc53 100644 --- a/packages/auth/lib/PhoneAuthListener.js +++ b/packages/auth/lib/PhoneAuthListener.js @@ -68,7 +68,10 @@ export default class PhoneAuthListener { for (let i = 0, len = events.length; i < len; i++) { const type = events[i]; - this._auth.emitter.once(this._internalEvents[type], this[`_${type}Handler`].bind(this)); + const subscription = this._auth.emitter.addListener(this._internalEvents[type], event => { + this[`_${type}Handler`](event); + subscription.remove(); + }); } } @@ -203,11 +206,17 @@ export default class PhoneAuthListener { this._addUserObserver(observer); if (isFunction(errorCb)) { - this._auth.emitter.once(this._publicEvents.error, errorCb); + const subscription = this._auth.emitter.addListener(this._publicEvents.error, () => { + errorCb; + subscription.remove(); + }); } if (isFunction(successCb)) { - this._auth.emitter.once(this._publicEvents.success, successCb); + const subscription = this._auth.emitter.addListener(this._publicEvents.success, () => { + successCb; + subscription.remove(); + }); } return this; diff --git a/packages/database/lib/DatabaseSyncTree.js b/packages/database/lib/DatabaseSyncTree.js index dc3440263a..25f4e522db 100644 --- a/packages/database/lib/DatabaseSyncTree.js +++ b/packages/database/lib/DatabaseSyncTree.js @@ -262,10 +262,10 @@ class DatabaseSyncTree { this._reverseLookup[eventRegistrationKey] = registration; if (once) { - SharedEventEmitter.once( - eventRegistrationKey, - this._onOnceRemoveRegistration(eventRegistrationKey, listener), - ); + const subscription = SharedEventEmitter.addListener(eventRegistrationKey, event => { + this._onOnceRemoveRegistration(eventRegistrationKey, listener, event); + subscription.remove(); + }); } else { SharedEventEmitter.addListener(eventRegistrationKey, listener); }