Merge pull request #34 from Leeds-eBooks/patch/flow-errors-1

Fixing various flow errors
This commit is contained in:
Michael Diarmid 2017-04-04 23:30:04 +01:00 committed by GitHub
commit f6ee3ff6f9
10 changed files with 117 additions and 40 deletions

View File

@ -15,7 +15,7 @@ import Messaging, { statics as MessagingStatics } from './modules/messaging';
import Analytics from './modules/analytics';
import Crash from './modules/crash';
const instances = { default: null };
const instances: Object = { default: null };
const FirebaseModule = NativeModules.RNFirebase;
/**
@ -34,6 +34,19 @@ export default class Firebase {
_remoteConfig: ?Object;
_crash: ?Object;
auth: Function;
storage: Function;
database: Function;
messaging: Function;
eventHandlers: Object;
debug: boolean;
options: {
errorOnMissingPlayServices: boolean,
debug?: boolean,
persistence?: boolean
};
/**
*
* @param options
@ -127,14 +140,17 @@ export default class Firebase {
* @returns {function()}
* @private
*/
_staticsOrInstance(name, statics, InstanceClass) {
_staticsOrInstance(name, statics, InstanceClass): Function {
const getInstance = () => {
const internalPropName = `_${name}`;
// $FlowFixMe
if (!this[internalPropName]) {
// $FlowFixMe
this[internalPropName] = new InstanceClass(this);
}
// $FlowFixMe
return this[internalPropName];
};

View File

@ -21,3 +21,13 @@ declare type GoogleApiAvailabilityType = {
isUserResolvableError?: boolean,
error?: string
};
declare class FirebaseError {
message: string,
name: string,
code: string,
stack: string,
path: string,
details: string,
modifiers: string
};

View File

@ -42,7 +42,12 @@ export default class Auth extends Base {
if (auth && auth.user && !this._user) this._user = new User(this, auth);
else if ((!auth || !auth.user) && this._user) this._user = null;
else if (this._user) this._user._updateValues(auth);
if (emit) this.emit('onAuthStateChanged', this._authResult.user || null);
if (emit) {
this.emit(
'onAuthStateChanged',
this._authResult && this._authResult.user || null
);
}
return auth ? this._user : null;
}

View File

@ -33,7 +33,7 @@ export default class Analytics extends Base {
* @param {Error} error
* @param maxStackSize
*/
report(error: Error, maxStackSize: Number = 10): void {
report(error: FirebaseError, maxStackSize: number = 10): void {
if (!error || !error.code || !error.message) return;
let errorMessage = `Message: ${error.message}\r\n`;

View File

@ -12,6 +12,7 @@ const FirebaseDatabase = NativeModules.RNFirebaseDatabase;
*/
export default class Disconnect {
ref: Reference;
path: string;
/**
*

View File

@ -218,7 +218,8 @@ export default class Database extends Base {
firebaseMessage = `${firebaseMessage} at /${path}\r\n`;
}
const firebaseError = new Error(firebaseMessage);
// $FlowFixMe
const firebaseError: FirebaseError = new Error(firebaseMessage);
firebaseError.code = code;
firebaseError.path = path;

View File

@ -157,19 +157,25 @@ export default class Reference extends ReferenceBase {
* @param onComplete
* @param applyLocally
*/
transaction(transactionUpdate, onComplete?: () => any, applyLocally: boolean = false) {
transaction(
transactionUpdate: Function,
onComplete?: (?Error, any, ?Snapshot) => any,
applyLocally: boolean = false
) {
if (!isFunction(transactionUpdate)) return Promise.reject(new Error('Missing transactionUpdate function argument.'));
return new Promise((resolve, reject) => {
const onCompleteWrapper = (error, committed, snapshotData) => {
if (error) {
if (isFunction(onComplete)) onComplete(error, committed, null);
if (typeof onComplete === 'function') {
onComplete(error, committed, null);
}
return reject(error);
}
const snapshot = new Snapshot(this, snapshotData);
if (isFunction(onComplete)) {
if (typeof onComplete === 'function') {
onComplete(null, committed, snapshot);
}

View File

@ -62,7 +62,7 @@ export default class Snapshot {
* @link https://firebase.google.com/docs/reference/js/firebase.database.DataSnapshot#exists
* @returns {boolean}
*/
exists(): Boolean {
exists(): boolean {
return this._value !== null;
}
@ -71,7 +71,7 @@ export default class Snapshot {
* @link https://firebase.google.com/docs/reference/js/firebase.database.DataSnapshot#forEach
* @param action
*/
forEach(action: (key: any) => any): Boolean {
forEach(action: (key: any) => any): boolean {
if (!this._childKeys.length) return false;
let cancelled = false;
@ -94,7 +94,7 @@ export default class Snapshot {
* @link https://firebase.google.com/docs/reference/js/firebase.database.DataSnapshot#getPriority
* @returns {String|Number|null}
*/
getPriority(): String|Number|null {
getPriority(): string | number | null {
return this._priority;
}
@ -104,7 +104,7 @@ export default class Snapshot {
* @param path
* @returns {Boolean}
*/
hasChild(path: string): Boolean {
hasChild(path: string): boolean {
return deepExists(this._value, path);
}
@ -113,7 +113,7 @@ export default class Snapshot {
* @link https://firebase.google.com/docs/reference/js/firebase.database.DataSnapshot#hasChildren
* @returns {boolean}
*/
hasChildren(): Boolean {
hasChildren(): boolean {
return this.numChildren() > 0;
}
@ -122,7 +122,7 @@ export default class Snapshot {
* @link https://firebase.google.com/docs/reference/js/firebase.database.DataSnapshot#numChildren
* @returns {Number}
*/
numChildren(): Number {
numChildren(): number {
if (!isObject(this._value)) return 0;
return Object.keys(this._value).length;
}

View File

@ -13,7 +13,7 @@ const FirebaseDatabase = NativeModules.RNFirebaseDatabase;
* @class Database
*/
export default class TransactionHandler extends Base {
constructor(firebase: Object, database: Object, FirebaseDatabaseEvt) {
constructor(firebase: Object, database: Object, FirebaseDatabaseEvt: Object) {
super(firebase, {});
this.transactions = {};
this.database = database;
@ -32,7 +32,12 @@ export default class TransactionHandler extends Base {
* @param onComplete
* @param applyLocally
*/
add(reference, transactionUpdater, onComplete, applyLocally = false) {
add(
reference: Object,
transactionUpdater: Function,
onComplete?: Function,
applyLocally?: boolean = false
) {
const id = this._generateTransactionId();
this.transactions[id] = {
@ -45,7 +50,7 @@ export default class TransactionHandler extends Base {
started: true,
};
FirebaseDatabase.startTransaction(reference.path, id, applyLocally || false);
FirebaseDatabase.startTransaction(reference.path, id, applyLocally);
}
/**
@ -57,7 +62,7 @@ export default class TransactionHandler extends Base {
* @returns {string}
* @private
*/
_generateTransactionId() {
_generateTransactionId(): string {
return generatePushID(this.database.serverTimeOffset);
}
@ -72,7 +77,7 @@ export default class TransactionHandler extends Base {
case 'update':
return this._handleUpdate(event);
case 'error':
return this._handleError(error);
return this._handleError(event);
case 'complete':
return this._handleComplete(event);
default:

View File

@ -1,3 +1,6 @@
/**
* @flow
*/
// modeled after base64 web-safe chars, but ordered by ASCII
const PUSH_CHARS = '-0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz';
const hasOwnProperty = Object.hasOwnProperty;
@ -8,7 +11,8 @@ const _handler = (resolve, reject, errorPrefix, err, resp) => {
// resolve / reject after events etc
setImmediate(() => {
if (err) {
const firebaseError = new Error(err.message);
// $FlowFixMe
const firebaseError: FirebaseError = new Error(err.message);
if (isObject(err)) {
Object.keys(err).forEach(key => Object.defineProperty(firebaseError, key, { value: err[key] }));
@ -23,7 +27,7 @@ const _handler = (resolve, reject, errorPrefix, err, resp) => {
});
};
export function toWebSDKErrorCode(code, prefix) {
export function toWebSDKErrorCode(code: any, prefix: string): string {
if (!code || typeof code !== 'string') return '';
return code.toLowerCase().replace('error_', prefix).replace(/_/g, '-');
}
@ -36,7 +40,11 @@ export function toWebSDKErrorCode(code, prefix) {
* @param joiner
* @returns {*}
*/
export function deepGet(object, path, joiner = '/') {
export function deepGet(
object: Object,
path: string,
joiner?: string = '/'
): any {
const keys = path.split(joiner);
let i = 0;
@ -60,7 +68,11 @@ export function deepGet(object, path, joiner = '/') {
* @param joiner
* @returns {*}
*/
export function deepExists(object, path, joiner = '/') {
export function deepExists(
object: Object,
path: string,
joiner?: string = '/'
): boolean {
const keys = path.split(joiner);
let i = 0;
@ -81,7 +93,7 @@ export function deepExists(object, path, joiner = '/') {
* @param item
* @returns {boolean}
*/
export function isObject(item) {
export function isObject(item: any): boolean {
return (item && typeof item === 'object' && !Array.isArray(item) && item !== null);
}
@ -90,8 +102,8 @@ export function isObject(item) {
* @param item
* @returns {*|boolean}
*/
export function isFunction(item) {
return (item && typeof item === 'function');
export function isFunction(item?: any): boolean {
return Boolean(item && typeof item === 'function');
}
/**
@ -99,9 +111,9 @@ export function isFunction(item) {
* @param string
* @returns {*}
*/
export function tryJSONParse(string) {
export function tryJSONParse(string: string | null): any {
try {
return JSON.parse(string);
return string && JSON.parse(string);
} catch (jsonError) {
return string;
}
@ -112,7 +124,7 @@ export function tryJSONParse(string) {
* @param data
* @returns {*}
*/
export function tryJSONStringify(data) {
export function tryJSONStringify(data: any): string | null {
try {
return JSON.stringify(data);
} catch (jsonError) {
@ -149,7 +161,11 @@ export function noop(): void {
* @param NativeModule
* @param errorPrefix
*/
export function promisify(fn: Function, NativeModule: Object, errorPrefix): Function<Promise> {
export function promisify(
fn: Function | string,
NativeModule: Object,
errorPrefix?: string
): (any) => Promise<> {
return (...args) => {
return new Promise((resolve, reject) => {
const _fn = typeof fn === 'function' ? fn : NativeModule[fn];
@ -168,7 +184,12 @@ export function promisify(fn: Function, NativeModule: Object, errorPrefix): Func
* @param callback
* @private
*/
function _delayChunk(collection, chunkSize, operation, callback): void {
function _delayChunk(
collection: Array<*>,
chunkSize: number,
operation: Function,
callback: Function
): void {
const length = collection.length;
const iterations = Math.ceil(length / chunkSize);
@ -196,21 +217,28 @@ function _delayChunk(collection, chunkSize, operation, callback): void {
* @param iterator
* @param cb
*/
export function each(array: Array, chunkSize?: number, iterator: Function, cb: Function): void {
export function each(
array: Array<*>,
chunkSize: number | Function,
iterator: Function,
cb?: Function
): void {
if (typeof chunkSize === 'function') {
cb = iterator;
iterator = chunkSize;
chunkSize = DEFAULT_CHUNK_SIZE;
}
_delayChunk(array, chunkSize, (slice, start) => {
for (let ii = 0, jj = slice.length; ii < jj; ii += 1) {
iterator(slice[ii], start + ii);
}
}, cb);
if (cb) {
_delayChunk(array, chunkSize, (slice, start) => {
for (let ii = 0, jj = slice.length; ii < jj; ii += 1) {
iterator(slice[ii], start + ii);
}
}, cb);
}
}
export function typeOf(value) {
export function typeOf(value: any): string {
if (value === null) return 'null';
if (Array.isArray(value)) return 'array';
return typeof value;
@ -224,7 +252,12 @@ export function typeOf(value) {
* @param cb
* @returns {*}
*/
export function map(array: Array, chunkSize?: number, iterator: Function, cb: Function): void {
export function map(
array: Array<*>,
chunkSize: number | Function,
iterator: Function,
cb?: Function
): void {
if (typeof chunkSize === 'function') {
cb = iterator;
iterator = chunkSize;
@ -237,7 +270,7 @@ export function map(array: Array, chunkSize?: number, iterator: Function, cb: Fu
result.push(iterator(slice[ii], start + ii, array));
}
return result;
}, () => cb(result));
}, () => cb && cb(result));
}