-
Notifications
You must be signed in to change notification settings - Fork 127
Expand file tree
/
Copy pathfirestack.js
More file actions
250 lines (209 loc) · 6.06 KB
/
firestack.js
File metadata and controls
250 lines (209 loc) · 6.06 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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/**
* @providesModule Firestack
* @flow
*/
import { NativeModules, NativeEventEmitter } from 'react-native';
import Log from './utils/log';
import { promisify } from './utils';
import Singleton from './utils/singleton';
// modules
import Auth from './modules/auth';
import Storage from './modules/storage';
import Database from './modules/database';
import Presence from './modules/presence';
import Messaging from './modules/messaging';
import Analytics from './modules/analytics';
import RemoteConfig from './modules/remoteConfig';
let log;
const instances = { default: null };
const FirestackModule = NativeModules.Firestack;
const FirestackModuleEvt = new NativeEventEmitter(FirestackModule);
type GoogleApiAvailabilityType = {
status: number,
isAvailable: boolean,
isUserResolvableError?: boolean,
error?: string
};
/**
* @class Firestack
*/
export default class Firestack extends Singleton {
/**
*
* @param options
*/
constructor(options: Object = {}) {
const instance = super(options);
instance.options = Object.assign({ errorOnMissingPlayServices: true }, options);
instance._debug = instance.options.debug || false;
Log.enable(instance._debug);
log = instance._log = new Log('firestack');
log.info('Creating new firestack instance');
instance._remoteConfig = instance.options.remoteConfig || {};
delete instance.options.remoteConfig;
instance.configured = instance.options.configure || false;
instance.eventHandlers = {};
log.info('Calling configure with options', instance.options);
instance.configurePromise = instance.configure(instance.options);
instance._auth = new Auth(instance, instance.options);
if (instance.options.errorOnMissingPlayServices && !this.googleApiAvailability.isAvailable) {
throw new Error(`Google Play Services is required to run this application but no valid installation was found (Code ${this.googleApiAvailability.status}).`);
}
const database = {
ServerValue: {
TIMESTAMP: FirestackModule.serverValueTimestamp || { '.sv': 'timestamp' },
}
};
database.__proto__ = instance.database.__proto__;
instance.database.__proto__ = database;
}
_db: ?Object;
_log: ?Object;
_auth: ?Object;
_store: ?Object;
_storage: ?Object;
_presence: ?Object;
_analytics: ?Object;
_constants: ?Object;
_messaging: ?Object;
_remoteConfig: ?Object;
/**
* Support web version of initApp.
* @param options
* @param name
* @returns {*}
*/
static initializeApp(options: Object = {}, name: string = 'default') {
if (!instances[name]) instances[name] = new Firestack(options);
return instances[name];
}
/**
*
* @param opts
* @returns {Promise.<TResult>|*|Promise.<T>}
*/
configure(opts: Object = {}) {
if (!this.configurePromise) {
const firestackOptions = Object.assign({}, this.options, opts);
this.configurePromise = promisify('configureWithOptions', FirestackModule)(firestackOptions)
.then((configuredProperties) => {
log.info('Native configureWithOptions success', configuredProperties);
this.configured = true;
this.firestackOptions = configuredProperties;
return configuredProperties;
}).catch((err) => {
log.info('Native error occurred while calling configure', err);
});
}
return this.configurePromise;
}
onReady(cb: Function) {
// TODO wut o.O
return this.configurePromise = this.configurePromise.then(cb);
}
/**
* 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)
*/
auth() {
return this._auth;
}
database() {
if (!this._db) {
this._db = new Database(this);
}
return this._db;
}
analytics() {
if (!this._analytics) {
this._analytics = new Analytics(this);
}
return this._analytics;
}
// storage
storage() {
if (!this._storage) {
this._storage = new Storage(this);
}
return this._storage;
}
presence() {
if (!this._presence) {
this._presence = new Presence(this);
}
return this._presence;
}
messaging() {
if (!this._messaging) {
this._messaging = new Messaging(this);
}
return this._messaging;
}
remoteConfig() {
if (!this._remoteConfig) {
this._remoteConfig = new RemoteConfig(this);
}
return this._remoteConfig;
}
// // TODO remove as deprecated and its in the wrong place anyway
get ServerValue(): Promise<*> {
return promisify('serverValue', FirestackModule)();
}
get apps(): Array<string> {
return Object.keys(instances);
}
/**
* 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.
return FirestackModule.googleApiAvailability || { isAvailable: true, status: 0 };
}
/**
* Logger
*/
get log(): Log {
return this._log;
}
/**
* Redux store
**/
get store(): ?Object {
return this._store;
}
get constants(): Object {
if (!this._constants) {
this._constants = Object.assign({}, Storage.constants)
}
return this._constants;
}
/**
* Set the redux store helper
*/
setStore(store: Object) {
if (store) {
this.log.info('Setting the store for Firestack instance');
this._store = store;
}
}
/**
* Global event handlers for the single Firestack instance
*/
on(name: string, cb: Function, nativeModule: Object = FirestackModuleEvt) {
if (!this.eventHandlers[name]) {
this.eventHandlers[name] = [];
}
const sub = nativeModule.addListener(name, cb);
this.eventHandlers[name].push(sub);
return sub;
}
off(name: string) {
if (this.eventHandlers[name]) {
this.eventHandlers[name]
.forEach(subscription => subscription.remove());
}
}
}