2017-03-02 13:10:10 +00:00
|
|
|
/**
|
|
|
|
* @providesModule Firebase
|
|
|
|
* @flow
|
|
|
|
*/
|
2017-09-01 17:51:42 +03:00
|
|
|
import { NativeModules } from 'react-native'
|
2017-03-02 13:10:10 +00:00
|
|
|
|
2017-09-01 17:51:42 +03:00
|
|
|
import Log from './utils/log'
|
|
|
|
import { isObject } from './utils'
|
2017-03-02 13:10:10 +00:00
|
|
|
|
|
|
|
// modules
|
2017-09-01 17:51:42 +03:00
|
|
|
import Auth, { statics as AuthStatics } from './modules/auth'
|
|
|
|
import Storage, { statics as StorageStatics } from './modules/storage'
|
|
|
|
import Database, { statics as DatabaseStatics } from './modules/database'
|
|
|
|
import Messaging, { statics as MessagingStatics } from './modules/messaging'
|
|
|
|
import Analytics from './modules/analytics'
|
|
|
|
import Crash from './modules/crash'
|
|
|
|
import RemoteConfig from './modules/config'
|
|
|
|
import Performance from './modules/perf'
|
|
|
|
import AdMob, { statics as AdMobStatics } from './modules/admob'
|
|
|
|
import Links, { statics as LinksStatics } from './modules/links'
|
|
|
|
|
|
|
|
const instances: Object = { default: null }
|
|
|
|
const FirebaseModule = NativeModules.RNFirebase
|
2017-03-02 13:10:10 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @class Firebase
|
|
|
|
*/
|
2017-03-10 18:12:46 +00:00
|
|
|
export default class Firebase {
|
2017-09-01 17:51:42 +03:00
|
|
|
_log: ?Object
|
|
|
|
_auth: ?Object
|
|
|
|
_store: ?Object
|
|
|
|
_storage: ?Object
|
|
|
|
_database: ?Object
|
|
|
|
_presence: ?Object
|
|
|
|
_analytics: ?Object
|
|
|
|
_constants: ?Object
|
|
|
|
_messaging: ?Object
|
|
|
|
_config: ?Object
|
|
|
|
_crash: ?Object
|
|
|
|
_perf: ?Object
|
|
|
|
_admob: ?Object
|
|
|
|
_links: ?Object
|
|
|
|
|
|
|
|
auth: Function
|
|
|
|
crash: Function
|
|
|
|
storage: Function
|
|
|
|
database: Function
|
|
|
|
analytics: Function
|
|
|
|
messaging: Function
|
|
|
|
config: Function
|
|
|
|
perf: Function
|
|
|
|
admob: Function
|
|
|
|
links: Function
|
|
|
|
|
|
|
|
eventHandlers: Object
|
|
|
|
debug: boolean
|
2017-04-04 17:58:20 +01:00
|
|
|
options: {
|
|
|
|
errorOnMissingPlayServices: boolean,
|
|
|
|
debug?: boolean,
|
2017-09-01 17:51:42 +03:00
|
|
|
persistence?: boolean,
|
|
|
|
}
|
2017-04-04 17:58:20 +01:00
|
|
|
|
2017-03-02 13:10:10 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param options
|
|
|
|
*/
|
|
|
|
constructor(options: Object = {}) {
|
2017-09-01 17:51:42 +03:00
|
|
|
this.eventHandlers = {}
|
|
|
|
this.debug = options.debug || false
|
|
|
|
this.options = Object.assign(
|
|
|
|
{ errorOnMissingPlayServices: true, promptOnMissingPlayServices: true },
|
|
|
|
options
|
|
|
|
)
|
2017-03-02 13:10:10 +00:00
|
|
|
|
2017-03-10 18:12:46 +00:00
|
|
|
if (this.debug) {
|
2017-09-01 17:51:42 +03:00
|
|
|
Log.enable(this.debug)
|
2017-03-10 18:12:46 +00:00
|
|
|
}
|
2017-03-02 13:10:10 +00:00
|
|
|
|
2017-09-01 17:51:42 +03:00
|
|
|
this._log = new Log('firebase')
|
2017-03-02 13:10:10 +00:00
|
|
|
|
2017-04-10 09:50:42 +01:00
|
|
|
if (!this.googleApiAvailability.isAvailable) {
|
2017-09-01 17:51:42 +03:00
|
|
|
if (
|
|
|
|
this.options.promptOnMissingPlayServices &&
|
|
|
|
this.googleApiAvailability.isUserResolvableError
|
|
|
|
) {
|
|
|
|
FirebaseModule.promptPlayServices()
|
2017-04-10 09:50:42 +01:00
|
|
|
} else {
|
2017-09-01 17:51:42 +03:00
|
|
|
const error = `Google Play Services is required to run this application but no valid installation was found (Code ${this
|
|
|
|
.googleApiAvailability.status}).`
|
2017-04-10 09:50:42 +01:00
|
|
|
if (this.options.errorOnMissingPlayServices) {
|
2017-09-01 17:51:42 +03:00
|
|
|
throw new Error(error)
|
2017-04-10 09:50:42 +01:00
|
|
|
} else {
|
2017-09-01 17:51:42 +03:00
|
|
|
console.warn(error)
|
2017-04-10 09:50:42 +01:00
|
|
|
}
|
|
|
|
}
|
2017-03-02 13:10:10 +00:00
|
|
|
}
|
2017-03-22 20:15:02 +00:00
|
|
|
|
2017-09-01 17:51:42 +03:00
|
|
|
this.auth = this._staticsOrInstance('auth', AuthStatics, Auth)
|
|
|
|
this.storage = this._staticsOrInstance('storage', StorageStatics, Storage)
|
|
|
|
this.database = this._staticsOrInstance(
|
|
|
|
'database',
|
|
|
|
DatabaseStatics,
|
|
|
|
Database
|
|
|
|
)
|
|
|
|
this.messaging = this._staticsOrInstance(
|
|
|
|
'messaging',
|
|
|
|
MessagingStatics,
|
|
|
|
Messaging
|
|
|
|
)
|
|
|
|
this.analytics = this._staticsOrInstance('analytics', {}, Analytics)
|
|
|
|
this.crash = this._staticsOrInstance('crash', {}, Crash)
|
|
|
|
this.config = this._staticsOrInstance('config', {}, RemoteConfig)
|
|
|
|
this.perf = this._staticsOrInstance('perf', {}, Performance)
|
|
|
|
this.admob = this._staticsOrInstance('admob', AdMobStatics, AdMob)
|
|
|
|
this.links = this._staticsOrInstance('links', LinksStatics, Links)
|
2017-03-27 19:11:26 +01:00
|
|
|
|
2017-03-30 16:25:27 +01:00
|
|
|
// init auth to start listeners
|
2017-05-30 16:23:51 +01:00
|
|
|
if (NativeModules.RNFirebaseAuth) {
|
2017-09-01 17:51:42 +03:00
|
|
|
this.auth()
|
2017-05-30 16:23:51 +01:00
|
|
|
}
|
2017-03-02 13:10:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Support web version of initApp.
|
|
|
|
* @param options
|
|
|
|
* @param name
|
|
|
|
* @returns {*}
|
|
|
|
*/
|
|
|
|
static initializeApp(options: Object = {}, name: string = 'default') {
|
2017-03-10 18:12:46 +00:00
|
|
|
if (!isObject(options)) {
|
2017-09-01 17:51:42 +03:00
|
|
|
throw new Error(
|
|
|
|
'Firebase.initializeApp(options <- requires a configuration object'
|
|
|
|
)
|
2017-03-10 18:12:46 +00:00
|
|
|
}
|
2017-03-02 13:10:10 +00:00
|
|
|
|
2017-03-10 18:12:46 +00:00
|
|
|
if (typeof name !== 'string') {
|
2017-09-01 17:51:42 +03:00
|
|
|
throw new Error(
|
|
|
|
'Firebase.initializeApp(options, name <- requires a string value'
|
|
|
|
)
|
2017-03-10 18:12:46 +00:00
|
|
|
}
|
2017-03-02 13:10:10 +00:00
|
|
|
|
2017-03-10 18:12:46 +00:00
|
|
|
if (name !== 'default') {
|
2017-09-01 17:51:42 +03:00
|
|
|
throw new Error(
|
|
|
|
'RNFirebase currently only supports one instance of firebase - the default one.'
|
|
|
|
)
|
2017-03-02 13:10:10 +00:00
|
|
|
}
|
|
|
|
|
2017-09-01 17:51:42 +03:00
|
|
|
if (!instances[name]) instances[name] = new Firebase(options)
|
|
|
|
return instances[name]
|
2017-03-02 13:10:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
get apps(): Array<string> {
|
2017-09-01 17:51:42 +03:00
|
|
|
return Object.keys(instances)
|
2017-03-02 13:10:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns androids GoogleApiAvailability status and message if available.
|
|
|
|
* @returns {GoogleApiAvailabilityType|{isAvailable: boolean, status: number}}
|
|
|
|
*/
|
|
|
|
get googleApiAvailability(): GoogleApiAvailabilityType {
|
|
|
|
// if not available then return a fake object for ios - saves doing platform specific logic.
|
2017-09-01 17:51:42 +03:00
|
|
|
return (
|
|
|
|
FirebaseModule.googleApiAvailability || { isAvailable: true, status: 0 }
|
|
|
|
)
|
2017-03-02 13:10:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Logger
|
|
|
|
*/
|
|
|
|
get log(): Log {
|
2017-09-01 17:51:42 +03:00
|
|
|
return this._log
|
2017-03-02 13:10:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-03-22 20:45:53 +00:00
|
|
|
*
|
|
|
|
* @param name
|
|
|
|
* @param statics
|
|
|
|
* @param InstanceClass
|
|
|
|
* @returns {function()}
|
|
|
|
* @private
|
|
|
|
*/
|
2017-04-04 17:58:20 +01:00
|
|
|
_staticsOrInstance(name, statics, InstanceClass): Function {
|
2017-03-22 20:45:53 +00:00
|
|
|
const getInstance = () => {
|
2017-09-01 17:51:42 +03:00
|
|
|
const internalPropName = `_${name}`
|
2017-03-02 13:10:10 +00:00
|
|
|
|
2017-04-04 17:58:20 +01:00
|
|
|
// $FlowFixMe
|
2017-03-22 20:45:53 +00:00
|
|
|
if (!this[internalPropName]) {
|
2017-04-04 17:58:20 +01:00
|
|
|
// $FlowFixMe
|
2017-09-01 17:51:42 +03:00
|
|
|
this[internalPropName] = new InstanceClass(this)
|
2017-03-22 20:45:53 +00:00
|
|
|
}
|
2017-03-02 13:10:10 +00:00
|
|
|
|
2017-04-04 17:58:20 +01:00
|
|
|
// $FlowFixMe
|
2017-09-01 17:51:42 +03:00
|
|
|
return this[internalPropName]
|
|
|
|
}
|
2017-03-02 13:10:10 +00:00
|
|
|
|
2017-09-01 17:51:42 +03:00
|
|
|
Object.assign(getInstance, statics || {})
|
|
|
|
return getInstance
|
2017-03-02 13:10:10 +00:00
|
|
|
}
|
|
|
|
}
|