-
Notifications
You must be signed in to change notification settings - Fork 128
Expand file tree
/
Copy pathfirestack.js
More file actions
181 lines (152 loc) · 4.12 KB
/
firestack.js
File metadata and controls
181 lines (152 loc) · 4.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/**
* @providesModule Firestack
* @flow
*/
import Log from './log'
const firebase = require('firebase');
const app = require('firebase/app');
const storage = require('firebase/storage');
import {NativeModules, NativeEventEmitter, AsyncStorage} from 'react-native';
// TODO: Break out modules into component pieces
// i.e. auth component, storage component, etc.
const FirestackModule = NativeModules.Firestack;
const FirestackModuleEvt = new NativeEventEmitter(FirestackModule);
import promisify from './promisify'
import RemoteConfig from './modules/remoteConfig'
import {Authentication} from './modules/authentication'
import {Database} from './modules/database'
import {Analytics} from './modules/analytics'
import {Storage} from './modules/storage'
let log;
export class Firestack {
constructor(options) {
this.options = options || {};
this._debug = options.debug || false;
log = this._log = new Log('firestack', this._debug);
log.info('Creating new instance');
this._remoteConfig = options.remoteConfig || {};
delete options.remoteConfig;
this.configured = this.options.configure || false;
this.auth = null;
this.eventHandlers = {};
log.info('Calling configure with options', options);
this.configure(options);
this._auth = new Authentication(this);
}
configure(opts) {
opts = opts || {};
const firestackOptions = Object.assign({}, this.options, opts);
try {
this.appInstance = app.initializeApp(firestackOptions);
log.info('JS app initialized');
} catch (e) {
log.error('JS error while calling initializeApp', e);
}
log.info('Calling native configureWithOptions')
return promisify('configureWithOptions', FirestackModule)(firestackOptions)
.then((...args) => {
log.info('Native configureWithOptions success', args);
this.configured = true;
return args;
}).catch((err) => {
log.error('Native error occurred while calling configure', err);
})
}
/**
* Wrappers
* We add methods from each wrapper to this instance
* when they are needed. Not sure if this is a good
* idea or not (imperative vs. direct manipulation/proxy)
*/
get auth() {
if (!this._auth) { this._auth = new Authentication(this); }
return this._auth;
}
// database
get database() {
if (!this._db) { this._db = new Database(this); }
return this._db;
// db.enableLogging(this._debug);
// return this.appInstance.database();
}
// analytics
get analytics() {
if (!this._analytics) { this._analytics = new Analytics(this); }
return this._analytics;
}
// storage
get storage() {
if (!this._storage) { this._storage = new Storage(this); }
return this._storage;
}
// Storage
//
// /**
// * Configure the library to store the storage url
// * @param {string} url A string of your firebase storage url
// * @return {Promise}
// */
// setStorageUrl(url) {
// return promisify('setStorageUrl')(url);
// }
// other
get ServerValue() {
return db.ServerValue;
}
/**
* remote config
*/
get remoteConfig() {
if (!this.remoteConfig) {
this.remoteConfig = new RemoteConfig(this._remoteConfig);
}
return this.remoteConfig;
}
/**
* app instance
**/
get app() {
return this.appInstance;
}
/**
* Logger
*/
get log() {
return this._log;
}
/**
* Redux store
**/
get store() {
return this._store;
}
/**
* Set the redux store helper
*/
setStore(store) {
if (store) {
this.log.info('Setting the store for Firestack instance');
this._store = store;
}
}
/**
* Global event handlers for the single Firestack instance
*/
on(name, cb, nativeModule) {
if (!this.eventHandlers[name]) {
this.eventHandlers[name] = [];
}
if (!nativeModule) {
nativeModule = FirestackModuleEvt;
}
const sub = nativeModule.addListener(name, cb);
this.eventHandlers[name].push(sub);
return sub;
}
off(name) {
if (this.eventHandlers[name]) {
this.eventHandlers.forEach(subscription => subscription.remove());
}
}
}
export default Firestack