mirror of
https://github.com/zhigang1992/react-native-firebase.git
synced 2026-04-24 04:24:52 +08:00
Merge commit '49d29b53f21e530f5c918e472db93ee856947426'
This commit is contained in:
@@ -2,10 +2,12 @@
|
||||
* @flow
|
||||
*/
|
||||
import { NativeModules } from 'react-native';
|
||||
|
||||
import Log from '../utils/log';
|
||||
import { nativeWithApp } from './../utils';
|
||||
import INTERNALS from './../internals';
|
||||
import FirebaseCore from './../firebase';
|
||||
import FirebaseApp from '../firebase-app';
|
||||
import { nativeWithApp } from './../utils';
|
||||
|
||||
const logs = {};
|
||||
|
||||
@@ -24,6 +26,7 @@ const NATIVE_MODULE_EVENTS = {
|
||||
],
|
||||
Auth: [
|
||||
'auth_state_changed',
|
||||
'phone_auth_state_changed',
|
||||
],
|
||||
Database: [
|
||||
'database_transaction_event',
|
||||
@@ -42,14 +45,23 @@ const DEFAULTS = {
|
||||
};
|
||||
|
||||
export default class ModuleBase {
|
||||
_native: Object;
|
||||
_module: string;
|
||||
_options: Object;
|
||||
_appName: string;
|
||||
_namespace: string;
|
||||
_firebaseApp: Object;
|
||||
_eventEmitter: Object;
|
||||
static _NAMESPACE: string;
|
||||
static _NATIVE_MODULE: string;
|
||||
|
||||
/**
|
||||
*
|
||||
* @param firebaseApp
|
||||
* @param options
|
||||
* @param moduleName
|
||||
* @param withEventEmitter
|
||||
*/
|
||||
constructor(firebaseApp, options, withEventEmitter = false) {
|
||||
constructor(firebaseApp: Object, options: Object, withEventEmitter: boolean = false) {
|
||||
this._module = this.constructor._NATIVE_MODULE.replace('RNFirebase', '');
|
||||
this._firebaseApp = firebaseApp;
|
||||
this._appName = firebaseApp._name;
|
||||
@@ -113,7 +125,7 @@ export default class ModuleBase {
|
||||
* Returns the FirebaseApp instance for current module
|
||||
* @return {*}
|
||||
*/
|
||||
get app() {
|
||||
get app(): FirebaseApp {
|
||||
return this._firebaseApp;
|
||||
}
|
||||
|
||||
@@ -128,27 +140,27 @@ export default class ModuleBase {
|
||||
* Proxy functions to shared event emitter instance
|
||||
* https://github.com/facebook/react-native/blob/master/Libraries/EventEmitter/EventEmitter.js
|
||||
*/
|
||||
get sharedEventEmitter() {
|
||||
get sharedEventEmitter(): Object {
|
||||
return INTERNALS.SharedEventEmitter;
|
||||
}
|
||||
|
||||
get addListener() {
|
||||
get addListener(): Function {
|
||||
return INTERNALS.SharedEventEmitter.addListener.bind(INTERNALS.SharedEventEmitter);
|
||||
}
|
||||
|
||||
get once() {
|
||||
get once(): Function {
|
||||
return INTERNALS.SharedEventEmitter.once.bind(INTERNALS.SharedEventEmitter);
|
||||
}
|
||||
|
||||
get on() {
|
||||
get on(): Function {
|
||||
return INTERNALS.SharedEventEmitter.addListener.bind(INTERNALS.SharedEventEmitter);
|
||||
}
|
||||
|
||||
get emit() {
|
||||
get emit(): Function {
|
||||
return INTERNALS.SharedEventEmitter.emit.bind(INTERNALS.SharedEventEmitter);
|
||||
}
|
||||
|
||||
get listeners() {
|
||||
get listeners(): Function {
|
||||
return INTERNALS.SharedEventEmitter.listeners.bind(INTERNALS.SharedEventEmitter);
|
||||
}
|
||||
|
||||
@@ -157,11 +169,11 @@ export default class ModuleBase {
|
||||
return subscriptions && subscriptions.length;
|
||||
}
|
||||
|
||||
get removeListener() {
|
||||
get removeListener(): Function {
|
||||
return INTERNALS.SharedEventEmitter.removeListener.bind(INTERNALS.SharedEventEmitter);
|
||||
}
|
||||
|
||||
get removeAllListeners() {
|
||||
get removeAllListeners(): Function {
|
||||
return INTERNALS.SharedEventEmitter.removeAllListeners.bind(INTERNALS.SharedEventEmitter);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,7 +7,8 @@ import { Platform } from 'react-native';
|
||||
const PUSH_CHARS = '-0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz';
|
||||
const AUTO_ID_CHARS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
|
||||
const hasOwnProperty = Object.hasOwnProperty;
|
||||
const DEFAULT_CHUNK_SIZE = 50;
|
||||
|
||||
// const DEFAULT_CHUNK_SIZE = 50;
|
||||
|
||||
// Source: https://cloud.google.com/bigquery/docs/reference/standard-sql/lexical
|
||||
const REGEXP_FIELD_NAME = new RegExp(
|
||||
@@ -92,7 +93,7 @@ export function isFunction(item?: any): boolean {
|
||||
* @param value
|
||||
* @return {boolean}
|
||||
*/
|
||||
export function isString(value): Boolean {
|
||||
export function isString(value: any): boolean {
|
||||
return typeof value === 'string';
|
||||
}
|
||||
|
||||
@@ -148,63 +149,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
|
||||
@@ -217,41 +218,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;
|
||||
@@ -315,7 +316,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
|
||||
@@ -329,7 +330,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);
|
||||
|
||||
@@ -348,7 +349,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();
|
||||
@@ -374,7 +375,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) => {
|
||||
|
||||
Reference in New Issue
Block a user