[auth] convert native error object in phoneAuthListener to js error + misc flow typings/fixes

This commit is contained in:
Salakar 2017-09-25 23:07:27 +01:00
parent c4e2414295
commit 1d0dffbefe
1 changed files with 20 additions and 17 deletions

View File

@ -1,6 +1,6 @@
// @flow // @flow
import INTERNALS from './../../internals'; import INTERNALS from './../../internals';
import { generatePushID, isFunction, isAndroid, isIOS, isString } from './../../utils'; import { generatePushID, isFunction, isAndroid, isIOS, isString, nativeToJSError } from './../../utils';
type PhoneAuthSnapshot = { type PhoneAuthSnapshot = {
state: 'sent' | 'timeout' | 'verified' | 'error', state: 'sent' | 'timeout' | 'verified' | 'error',
@ -17,15 +17,16 @@ type PhoneAuthError = {
}; };
export default class PhoneAuthListener { export default class PhoneAuthListener {
_auth: Object; _auth: Object;
_reject: Function | null;
_resolve: Function | null;
_promise: Promise | null;
_credential: Object | null;
_timeout: number; _timeout: number;
_phoneAuthRequestKey: string;
_publicEvents: Object; _publicEvents: Object;
_internalEvents: Object; _internalEvents: Object;
_reject: Function | null;
_resolve: Function | null;
_credential: Object | null;
_promise: Promise<*> | null;
_phoneAuthRequestKey: string;
/** /**
* *
@ -33,7 +34,7 @@ export default class PhoneAuthListener {
* @param phoneNumber * @param phoneNumber
* @param timeout * @param timeout
*/ */
constructor(auth: Object, phoneNumber: string, timeout): PhoneAuthListener { constructor(auth: Object, phoneNumber: string, timeout?: number) {
this._auth = auth; this._auth = auth;
this._reject = null; this._reject = null;
this._resolve = null; this._resolve = null;
@ -110,8 +111,6 @@ export default class PhoneAuthListener {
*/ */
_emitToErrorCb(snapshot) { _emitToErrorCb(snapshot) {
const error = snapshot.error; const error = snapshot.error;
error.verificationId = snapshot.verificationId;
if (this._reject) this._reject(error); if (this._reject) this._reject(error);
this._auth.emit(this._publicEvents.error, error); this._auth.emit(this._publicEvents.error, error);
} }
@ -234,17 +233,19 @@ export default class PhoneAuthListener {
/** /**
* Internal verification failed event handler * Internal verification failed event handler
* @param errObject * @param state
* @private * @private
*/ */
_verificationFailedHandler(errObject) { _verificationFailedHandler(state) {
const snapshot: PhoneAuthSnapshot = { const snapshot: PhoneAuthSnapshot = {
verificationId: errObject.verificationId, verificationId: state.verificationId,
code: null, code: null,
error: null, error: null,
state: 'error', state: 'error',
}; };
const { code, message, nativeErrorMessage } = state.error;
snapshot.error = nativeToJSError(code, message, { nativeErrorMessage });
this._emitToObservers(snapshot); this._emitToObservers(snapshot);
this._emitToErrorCb(snapshot); this._emitToErrorCb(snapshot);
@ -256,7 +257,7 @@ export default class PhoneAuthListener {
-- PUBLIC API -- PUBLIC API
--------------*/ --------------*/
on(event: string, observer: () => PhoneAuthSnapshot, errorCb?: () => PhoneAuthError, successCb?: () => PhoneAuthSnapshot) { on(event: string, observer: () => PhoneAuthSnapshot, errorCb?: () => PhoneAuthError, successCb?: () => PhoneAuthSnapshot): this {
if (!isString(event)) { if (!isString(event)) {
throw new Error(INTERNALS.STRINGS.ERROR_MISSING_ARG_NAMED('event', 'string', 'on')); throw new Error(INTERNALS.STRINGS.ERROR_MISSING_ARG_NAMED('event', 'string', 'on'));
} }
@ -286,17 +287,19 @@ export default class PhoneAuthListener {
* Promise .then proxy * Promise .then proxy
* @param fn * @param fn
*/ */
then(fn) { then(fn: () => PhoneAuthSnapshot) {
this._promiseDeferred(); this._promiseDeferred();
return this._promise.then.bind(this._promise)(fn); if (this._promise) return this._promise.then.bind(this._promise)(fn);
return undefined; // will never get here - just to keep flow happy
} }
/** /**
* Promise .catch proxy * Promise .catch proxy
* @param fn * @param fn
*/ */
catch(fn) { catch(fn: () => Error) {
this._promiseDeferred(); this._promiseDeferred();
return this._promise.catch.bind(this._promise)(fn); if (this._promise) return this._promise.catch.bind(this._promise)(fn);
return undefined; // will never get here - just to keep flow happy
} }
} }