mirror of
https://github.com/zhigang1992/react-native-firebase.git
synced 2026-05-28 15:44:46 +08:00
[internals] Start refactoring some of the internals to simplify, tidy up and also reduce flow type pollution
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
// @flow
|
||||
import INTERNALS from '../../utils/internals';
|
||||
import { SharedEventEmitter } from '../../utils/events';
|
||||
import { generatePushID, isFunction, isAndroid, isIOS, isString, nativeToJSError } from './../../utils';
|
||||
|
||||
import type Auth from './';
|
||||
@@ -92,7 +93,7 @@ export default class PhoneAuthListener {
|
||||
|
||||
for (let i = 0, len = events.length; i < len; i++) {
|
||||
const type = events[i];
|
||||
this._auth.once(this._internalEvents[type], this[`_${type}Handler`].bind(this));
|
||||
SharedEventEmitter.once(this._internalEvents[type], this[`_${type}Handler`].bind(this));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -102,7 +103,7 @@ export default class PhoneAuthListener {
|
||||
* @private
|
||||
*/
|
||||
_addUserObserver(observer) {
|
||||
this._auth.on(this._publicEvents.event, observer);
|
||||
SharedEventEmitter.addListener(this._publicEvents.event, observer);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -111,7 +112,7 @@ export default class PhoneAuthListener {
|
||||
* @private
|
||||
*/
|
||||
_emitToObservers(snapshot: PhoneAuthSnapshot) {
|
||||
this._auth.emit(this._publicEvents.event, snapshot);
|
||||
SharedEventEmitter.emit(this._publicEvents.event, snapshot);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -122,7 +123,7 @@ export default class PhoneAuthListener {
|
||||
_emitToErrorCb(snapshot) {
|
||||
const error = snapshot.error;
|
||||
if (this._reject) this._reject(error);
|
||||
this._auth.emit(this._publicEvents.error, error);
|
||||
SharedEventEmitter.emit(this._publicEvents.error, error);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -132,7 +133,7 @@ export default class PhoneAuthListener {
|
||||
*/
|
||||
_emitToSuccessCb(snapshot) {
|
||||
if (this._resolve) this._resolve(snapshot);
|
||||
this._auth.emit(this._publicEvents.success, snapshot);
|
||||
SharedEventEmitter.emit(this._publicEvents.success, snapshot);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -143,12 +144,12 @@ export default class PhoneAuthListener {
|
||||
setTimeout(() => { // move to next event loop - not sure if needed
|
||||
// internal listeners
|
||||
Object.values(this._internalEvents).forEach((event) => {
|
||||
this._auth.removeAllListeners(event);
|
||||
SharedEventEmitter.removeAllListeners(event);
|
||||
});
|
||||
|
||||
// user observer listeners
|
||||
Object.values(this._publicEvents).forEach((publicEvent) => {
|
||||
this._auth.removeAllListeners(publicEvent);
|
||||
SharedEventEmitter.removeAllListeners(publicEvent);
|
||||
});
|
||||
}, 0);
|
||||
}
|
||||
@@ -279,11 +280,11 @@ export default class PhoneAuthListener {
|
||||
this._addUserObserver(observer);
|
||||
|
||||
if (isFunction(errorCb)) {
|
||||
this._auth.once(this._publicEvents.error, errorCb);
|
||||
SharedEventEmitter.once(this._publicEvents.error, errorCb);
|
||||
}
|
||||
|
||||
if (isFunction(successCb)) {
|
||||
this._auth.once(this._publicEvents.success, successCb);
|
||||
SharedEventEmitter.once(this._publicEvents.success, successCb);
|
||||
}
|
||||
|
||||
return this;
|
||||
|
||||
@@ -4,6 +4,8 @@
|
||||
*/
|
||||
import User from './User';
|
||||
import ModuleBase from '../../utils/ModuleBase';
|
||||
import { getAppEventName, SharedEventEmitter } from '../../utils/events';
|
||||
import { getLogger } from '../../utils/log';
|
||||
import INTERNALS from '../../utils/internals';
|
||||
import ConfirmationResult from './ConfirmationResult';
|
||||
|
||||
@@ -37,24 +39,24 @@ export default class Auth extends ModuleBase {
|
||||
this._user = null;
|
||||
this._authResult = null;
|
||||
|
||||
this.addListener(
|
||||
SharedEventEmitter.addListener(
|
||||
// sub to internal native event - this fans out to
|
||||
// public event name: onAuthStateChanged
|
||||
super._getAppEventName('auth_state_changed'),
|
||||
getAppEventName(this, 'auth_state_changed'),
|
||||
this._onInternalAuthStateChanged.bind(this),
|
||||
);
|
||||
|
||||
this.addListener(
|
||||
SharedEventEmitter.addListener(
|
||||
// sub to internal native event - this fans out to
|
||||
// public events based on event.type
|
||||
super._getAppEventName('phone_auth_state_changed'),
|
||||
getAppEventName(this, 'phone_auth_state_changed'),
|
||||
this._onInternalPhoneAuthStateChanged.bind(this),
|
||||
);
|
||||
|
||||
this.addListener(
|
||||
SharedEventEmitter.addListener(
|
||||
// sub to internal native event - this fans out to
|
||||
// public event name: onIdTokenChanged
|
||||
super._getAppEventName('auth_id_token_changed'),
|
||||
getAppEventName(this, 'auth_id_token_changed'),
|
||||
this._onInternalIdTokenChanged.bind(this),
|
||||
);
|
||||
|
||||
@@ -69,13 +71,13 @@ export default class Auth extends ModuleBase {
|
||||
*/
|
||||
_onInternalPhoneAuthStateChanged(event: Object) {
|
||||
const eventKey = `phone:auth:${event.requestKey}:${event.type}`;
|
||||
this.emit(eventKey, event.state);
|
||||
SharedEventEmitter.emit(eventKey, event.state);
|
||||
}
|
||||
|
||||
_setAuthState(auth: AuthResult) {
|
||||
this._authResult = auth;
|
||||
this._user = auth && auth.user ? new User(this, auth.user) : null;
|
||||
this.emit(this._getAppEventName('onUserChanged'), this._user);
|
||||
SharedEventEmitter.emit(getAppEventName(this, 'onUserChanged'), this._user);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -85,7 +87,7 @@ export default class Auth extends ModuleBase {
|
||||
*/
|
||||
_onInternalAuthStateChanged(auth: AuthResult) {
|
||||
this._setAuthState(auth);
|
||||
this.emit(this._getAppEventName('onAuthStateChanged'), this._user);
|
||||
SharedEventEmitter.emit(getAppEventName(this, 'onAuthStateChanged'), this._user);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -96,7 +98,7 @@ export default class Auth extends ModuleBase {
|
||||
*/
|
||||
_onInternalIdTokenChanged(auth: AuthResult) {
|
||||
this._setAuthState(auth);
|
||||
this.emit(this._getAppEventName('onIdTokenChanged'), this._user);
|
||||
SharedEventEmitter.emit(getAppEventName(this, 'onIdTokenChanged'), this._user);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -129,8 +131,8 @@ export default class Auth extends ModuleBase {
|
||||
* @param listener
|
||||
*/
|
||||
onAuthStateChanged(listener: Function) {
|
||||
this.log.info('Creating onAuthStateChanged listener');
|
||||
this.on(this._getAppEventName('onAuthStateChanged'), listener);
|
||||
getLogger(this).info('Creating onAuthStateChanged listener');
|
||||
SharedEventEmitter.addListener(getAppEventName(this, 'onAuthStateChanged'), listener);
|
||||
if (this._authResult) listener(this._user || null);
|
||||
return this._offAuthStateChanged.bind(this, listener);
|
||||
}
|
||||
@@ -140,8 +142,8 @@ export default class Auth extends ModuleBase {
|
||||
* @param listener
|
||||
*/
|
||||
_offAuthStateChanged(listener: Function) {
|
||||
this.log.info('Removing onAuthStateChanged listener');
|
||||
this.removeListener(this._getAppEventName('onAuthStateChanged'), listener);
|
||||
getLogger(this).info('Removing onAuthStateChanged listener');
|
||||
SharedEventEmitter.removeListener(getAppEventName(this, 'onAuthStateChanged'), listener);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -149,8 +151,8 @@ export default class Auth extends ModuleBase {
|
||||
* @param listener
|
||||
*/
|
||||
onIdTokenChanged(listener: Function) {
|
||||
this.log.info('Creating onIdTokenChanged listener');
|
||||
this.on(this._getAppEventName('onIdTokenChanged'), listener);
|
||||
getLogger(this).info('Creating onIdTokenChanged listener');
|
||||
SharedEventEmitter.addListener(getAppEventName(this, 'onIdTokenChanged'), listener);
|
||||
if (this._authResult) listener(this._user || null);
|
||||
return this._offIdTokenChanged.bind(this, listener);
|
||||
}
|
||||
@@ -160,8 +162,8 @@ export default class Auth extends ModuleBase {
|
||||
* @param listener
|
||||
*/
|
||||
_offIdTokenChanged(listener: Function) {
|
||||
this.log.info('Removing onIdTokenChanged listener');
|
||||
this.removeListener(this._getAppEventName('onIdTokenChanged'), listener);
|
||||
getLogger(this).info('Removing onIdTokenChanged listener');
|
||||
SharedEventEmitter.removeListener(getAppEventName(this, 'onIdTokenChanged'), listener);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -169,8 +171,8 @@ export default class Auth extends ModuleBase {
|
||||
* @param listener
|
||||
*/
|
||||
onUserChanged(listener: Function) {
|
||||
this.log.info('Creating onUserChanged listener');
|
||||
this.on(this._getAppEventName('onUserChanged'), listener);
|
||||
getLogger(this).info('Creating onUserChanged listener');
|
||||
SharedEventEmitter.addListener(getAppEventName(this, 'onUserChanged'), listener);
|
||||
if (this._authResult) listener(this._user || null);
|
||||
return this._offUserChanged.bind(this, listener);
|
||||
}
|
||||
@@ -180,8 +182,8 @@ export default class Auth extends ModuleBase {
|
||||
* @param listener
|
||||
*/
|
||||
_offUserChanged(listener: Function) {
|
||||
this.log.info('Removing onUserChanged listener');
|
||||
this.removeListener(this._getAppEventName('onUserChanged'), listener);
|
||||
getLogger(this).info('Removing onUserChanged listener');
|
||||
SharedEventEmitter.removeListener(getAppEventName(this, 'onUserChanged'), listener);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user