Skip to content

Commit 5eb2f59

Browse files
committed
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: facebook/react-native@87a2e29
1 parent 748431d commit 5eb2f59

File tree

2 files changed

+16
-7
lines changed

2 files changed

+16
-7
lines changed

packages/auth/lib/PhoneAuthListener.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,10 @@ export default class PhoneAuthListener {
6868

6969
for (let i = 0, len = events.length; i < len; i++) {
7070
const type = events[i];
71-
this._auth.emitter.once(this._internalEvents[type], this[`_${type}Handler`].bind(this));
71+
const subscription = this._auth.emitter.addListener(this._internalEvents[type], event => {
72+
this[`_${type}Handler`](event);
73+
subscription.remove();
74+
});
7275
}
7376
}
7477

@@ -203,11 +206,17 @@ export default class PhoneAuthListener {
203206
this._addUserObserver(observer);
204207

205208
if (isFunction(errorCb)) {
206-
this._auth.emitter.once(this._publicEvents.error, errorCb);
209+
const subscription = this._auth.emitter.addListener(this._publicEvents.error, () => {
210+
errorCb;
211+
subscription.remove();
212+
});
207213
}
208214

209215
if (isFunction(successCb)) {
210-
this._auth.emitter.once(this._publicEvents.success, successCb);
216+
const subscription = this._auth.emitter.addListener(this._publicEvents.success, () => {
217+
successCb;
218+
subscription.remove();
219+
});
211220
}
212221

213222
return this;

packages/database/lib/DatabaseSyncTree.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -262,10 +262,10 @@ class DatabaseSyncTree {
262262
this._reverseLookup[eventRegistrationKey] = registration;
263263

264264
if (once) {
265-
SharedEventEmitter.once(
266-
eventRegistrationKey,
267-
this._onOnceRemoveRegistration(eventRegistrationKey, listener),
268-
);
265+
const subscription = SharedEventEmitter.addListener(eventRegistrationKey, event => {
266+
this._onOnceRemoveRegistration(eventRegistrationKey, listener, event);
267+
subscription.remove();
268+
});
269269
} else {
270270
SharedEventEmitter.addListener(eventRegistrationKey, listener);
271271
}

0 commit comments

Comments
 (0)