Skip to content

Commit

Permalink
fix(listeners): port Emitter.once to analogous addListener/remove API
Browse files Browse the repository at this point in the history
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: facebook/react-native@87a2e29
  • Loading branch information
mikehardy committed Mar 17, 2021
1 parent 748431d commit 5eb2f59
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
15 changes: 12 additions & 3 deletions packages/auth/lib/PhoneAuthListener.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
}
}

Expand Down Expand Up @@ -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;
Expand Down
8 changes: 4 additions & 4 deletions packages/database/lib/DatabaseSyncTree.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down

0 comments on commit 5eb2f59

Please sign in to comment.