44 */
55import Log from './utils/log'
66
7+ const instances = {
8+ default : null ,
9+ } ;
10+
711// const firebase = require('firebase');
812
913// const app = require('firebase/app');
1014// const storage = require('firebase/storage');
1115// const db = require('firebase/database');
1216
13- import { NativeModules , NativeEventEmitter , AsyncStorage } from 'react-native' ;
17+ import { NativeModules , NativeEventEmitter , AsyncStorage } from 'react-native' ;
1418// TODO: Break out modules into component pieces
1519// i.e. auth component, storage component, etc.
1620const FirestackModule = NativeModules . Firestack ;
@@ -20,17 +24,17 @@ import promisify from './utils/promisify'
2024import Singleton from './utils/singleton'
2125
2226import RemoteConfig from './modules/remoteConfig'
23- import { Authentication } from './modules/authentication'
24- import { Database } from './modules/database'
25- import { Analytics } from './modules/analytics'
26- import { Storage } from './modules/storage'
27- import { Presence } from './modules/presence'
28- import { CloudMessaging } from './modules/cloudmessaging'
27+ import { Authentication } from './modules/authentication'
28+ import { Database } from './modules/database'
29+ import { Analytics } from './modules/analytics'
30+ import { Storage } from './modules/storage'
31+ import { Presence } from './modules/presence'
32+ import { CloudMessaging } from './modules/cloudmessaging'
2933
3034let log ;
3135export class Firestack extends Singleton {
3236
33- constructor ( options ) {
37+ constructor ( options , name ) {
3438 var instance = super ( options ) ;
3539
3640 instance . options = options || { } ;
@@ -55,10 +59,27 @@ export class Firestack extends Singleton {
5559 instance . _auth = new Authentication ( instance , instance . options ) ;
5660 }
5761
62+ /**
63+ * Support web version of initApp.
64+ * @param options
65+ * @param name
66+ * @returns {* }
67+ */
68+ static initializeApp ( options , name = 'default' ) {
69+ if ( ! instances [ name ] ) instances [ name ] = new Firestack ( options ) ;
70+ return instances [ name ] ;
71+ }
72+
73+
74+ /**
75+ *
76+ * @param opts
77+ * @returns {Promise.<TResult>|*|Promise.<T> }
78+ */
5879 configure ( opts = { } ) {
5980 if ( ! this . configurePromise ) {
6081 const firestackOptions = Object . assign ( { } , this . options , opts ) ;
61-
82+
6283 this . configurePromise = promisify ( 'configureWithOptions' , FirestackModule ) ( firestackOptions )
6384 . then ( ( configuredProperties ) => {
6485 log . info ( 'Native configureWithOptions success' , configuredProperties ) ;
@@ -82,63 +103,89 @@ export class Firestack extends Singleton {
82103 * when they are needed. Not sure if this is a good
83104 * idea or not (imperative vs. direct manipulation/proxy)
84105 */
85- get auth ( ) {
86- if ( ! this . _auth ) { this . _auth = new Authentication ( this ) ; }
106+ auth ( ) {
107+ if ( ! this . _auth ) {
108+ this . _auth = new Authentication ( this ) ;
109+ }
87110 return this . _auth ;
88111 }
112+
89113 // database
90- get database ( ) {
91- if ( ! this . _db ) { this . _db = new Database ( this ) ; }
114+ database ( ) {
115+ if ( ! this . _db ) {
116+ this . _db = new Database ( this ) ;
117+ }
92118 return this . _db ;
93119 // db.enableLogging(this._debug);
94120 // return this.appInstance.database();
95121 }
96122
97123 // analytics
98- get analytics ( ) {
99- if ( ! this . _analytics ) { this . _analytics = new Analytics ( this ) ; }
124+ analytics ( ) {
125+ if ( ! this . _analytics ) {
126+ this . _analytics = new Analytics ( this ) ;
127+ }
100128 return this . _analytics ;
101129 }
102130
103131 // storage
104- get storage ( ) {
105- if ( ! this . _storage ) { this . _storage = new Storage ( this ) ; }
132+ storage ( ) {
133+ if ( ! this . _storage ) {
134+ this . _storage = new Storage ( this ) ;
135+ }
106136 return this . _storage ;
107137 }
108138
109139 // presence
110- get presence ( ) {
111- if ( ! this . _presence ) { this . _presence = new Presence ( this ) ; }
140+ presence ( ) {
141+ if ( ! this . _presence ) {
142+ this . _presence = new Presence ( this ) ;
143+ }
112144 return this . _presence ;
113145 }
146+
114147 // CloudMessaging
115- get cloudMessaging ( ) {
116- if ( ! this . _cloudMessaging ) { this . _cloudMessaging = new CloudMessaging ( this ) ; }
148+ messaging ( ) {
149+ if ( ! this . _cloudMessaging ) {
150+ this . _cloudMessaging = new CloudMessaging ( this ) ;
151+ }
117152 return this . _cloudMessaging ;
118153 }
119154
120- // other
121- get ServerValue ( ) {
122- return promisify ( 'serverValue' , FirestackModule ) ( ) ;
123- }
124-
125155 /**
126156 * remote config
127157 */
128- get remoteConfig ( ) {
158+ remoteConfig ( ) {
129159 if ( ! this . remoteConfig ) {
130160 this . remoteConfig = new RemoteConfig ( this . _remoteConfig ) ;
131161 }
132162 return this . remoteConfig ;
133163 }
134164
165+ // other
166+ get ServerValue ( ) {
167+ return promisify ( 'serverValue' , FirestackModule ) ( ) ;
168+ }
169+
170+
135171 /**
136172 * app instance
137173 **/
138174 get app ( ) {
139175 return this . appInstance ;
140176 }
141177
178+ /**
179+ * app instance
180+ **/
181+ getInstance ( ) {
182+ return this . appInstance ;
183+ }
184+
185+ get apps ( ) {
186+ return Object . keys ( instances ) ;
187+ }
188+
142189 /**
143190 * Logger
144191 */
0 commit comments