misc cleanup of redundant code + flow type fixes

This commit is contained in:
Salakar 2017-09-25 23:19:13 +01:00
parent 72d27f5680
commit b53bf5bbe8
2 changed files with 99 additions and 100 deletions

View File

@ -6,6 +6,8 @@ export default class PerformanceMonitoring extends ModuleBase {
static _NAMESPACE = 'perf';
static _NATIVE_MODULE = 'RNFirebasePerformance';
_native: Object;
constructor(firebaseApp: Object, options: Object = {}) {
super(firebaseApp, options);
}
@ -26,8 +28,4 @@ export default class PerformanceMonitoring extends ModuleBase {
newTrace(trace: string): void {
return new Trace(this, trace);
}
get namespace(): string {
return 'firebase:perf';
}
}

View File

@ -6,7 +6,8 @@ import { Platform } from 'react-native';
// modeled after base64 web-safe chars, but ordered by ASCII
const PUSH_CHARS = '-0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz';
const hasOwnProperty = Object.hasOwnProperty;
const DEFAULT_CHUNK_SIZE = 50;
// const DEFAULT_CHUNK_SIZE = 50;
/**
* Deep get a value from an object.
@ -129,63 +130,63 @@ export function noop(): void {
}
/**
* Delays chunks based on sizes per event loop.
* @param collection
* @param chunkSize
* @param operation
* @param callback
* @private
*/
function _delayChunk(collection: Array<*>,
chunkSize: number,
operation: Function,
callback: Function): void {
const length = collection.length;
const iterations = Math.ceil(length / chunkSize);
// noinspection ES6ConvertVarToLetConst
let thisIteration = 0;
setImmediate(function next() {
const start = thisIteration * chunkSize;
const _end = start + chunkSize;
const end = _end >= length ? length : _end;
const result = operation(collection.slice(start, end), start, end);
if (thisIteration++ > iterations) {
callback(null, result);
} else {
setImmediate(next);
}
});
}
/**
* Async each with optional chunk size limit
* @param array
* @param chunkSize
* @param iterator
* @param cb
*/
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;
}
if (cb) {
_delayChunk(array, chunkSize, (slice, start) => {
for (let ii = 0, jj = slice.length; ii < jj; ii += 1) {
iterator(slice[ii], start + ii);
}
}, cb);
}
}
// /**
// * Delays chunks based on sizes per event loop.
// * @param collection
// * @param chunkSize
// * @param operation
// * @param callback
// * @private
// */
// function _delayChunk(collection: Array<*>,
// chunkSize: number,
// operation: Function,
// callback: Function): void {
// const length = collection.length;
// const iterations = Math.ceil(length / chunkSize);
//
// // noinspection ES6ConvertVarToLetConst
// let thisIteration = 0;
//
// setImmediate(function next() {
// const start = thisIteration * chunkSize;
// const _end = start + chunkSize;
// const end = _end >= length ? length : _end;
// const result = operation(collection.slice(start, end), start, end);
//
// if (thisIteration++ > iterations) {
// callback(null, result);
// } else {
// setImmediate(next);
// }
// });
// }
//
// /**
// * Async each with optional chunk size limit
// * @param array
// * @param chunkSize
// * @param iterator
// * @param cb
// */
// 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;
// }
//
// if (cb) {
// _delayChunk(array, chunkSize, (slice, start) => {
// for (let ii = 0, jj = slice.length; ii < jj; ii += 1) {
// iterator(slice[ii], start + ii);
// }
// }, cb);
// }
// }
/**
* Returns a string typeof that's valid for Firebase usage
@ -198,41 +199,41 @@ export function typeOf(value: any): string {
return typeof value;
}
/**
* Async map with optional chunk size limit
* @param array
* @param chunkSize
* @param iterator
* @param cb
* @returns {*}
*/
export function map(array: Array<*>,
chunkSize: number | Function,
iterator: Function,
cb?: Function): void {
if (typeof chunkSize === 'function') {
cb = iterator;
iterator = chunkSize;
chunkSize = DEFAULT_CHUNK_SIZE;
}
// /**
// * Async map with optional chunk size limit
// * @param array
// * @param chunkSize
// * @param iterator
// * @param cb
// * @returns {*}
// */
// export function map(array: Array<*>,
// chunkSize: number | Function,
// iterator: Function,
// cb?: Function): void {
// if (typeof chunkSize === 'function') {
// cb = iterator;
// iterator = chunkSize;
// chunkSize = DEFAULT_CHUNK_SIZE;
// }
//
// const result = [];
// _delayChunk(array, chunkSize, (slice, start) => {
// for (let ii = 0, jj = slice.length; ii < jj; ii += 1) {
// result.push(iterator(slice[ii], start + ii, array));
// }
// return result;
// }, () => cb && cb(result));
// }
const result = [];
_delayChunk(array, chunkSize, (slice, start) => {
for (let ii = 0, jj = slice.length; ii < jj; ii += 1) {
result.push(iterator(slice[ii], start + ii, array));
}
return result;
}, () => cb && cb(result));
}
/**
*
* @param string
* @return {string}
*/
export function capitalizeFirstLetter(string: String) {
return `${string.charAt(0).toUpperCase()}${string.slice(1)}`;
}
// /**
// *
// * @param string
// * @return {string}
// */
// export function capitalizeFirstLetter(string: String) {
// return `${string.charAt(0).toUpperCase()}${string.slice(1)}`;
// }
// timestamp of last push, used to prevent local collisions if you push twice in one ms.
let lastPushTime = 0;
@ -296,7 +297,7 @@ export function generatePushID(serverTimeOffset?: number = 0): string {
* @returns {Error}
*/
export function nativeToJSError(code: string, message: string, additionalProps?: Object = {}) {
const error = new Error(message);
const error: Object = new Error(message);
error.code = code;
Object.assign(error, additionalProps);
// exclude this function from the stack
@ -310,7 +311,7 @@ export function nativeToJSError(code: string, message: string, additionalProps?:
* @param appName
* @param NativeModule
*/
export function nativeWithApp(appName, NativeModule) {
export function nativeWithApp(appName: string, NativeModule: Object) {
const native = {};
const methods = Object.keys(NativeModule);
@ -329,7 +330,7 @@ export function nativeWithApp(appName, NativeModule) {
* @param object
* @return {string}
*/
export function objectToUniqueId(object: Object): String {
export function objectToUniqueId(object: Object): string {
if (!isObject(object) || object === null) return JSON.stringify(object);
const keys = Object.keys(object).sort();
@ -355,7 +356,7 @@ export function objectToUniqueId(object: Object): String {
* @param optionalCallback
* @return {Promise}
*/
export function promiseOrCallback(promise: Promise, optionalCallback?: Function) {
export function promiseOrCallback(promise: Promise<*>, optionalCallback?: Function) {
if (!isFunction(optionalCallback)) return promise;
return promise.then((result) => {