-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Expand file tree
/
Copy pathRNFBAppModule.js
More file actions
261 lines (237 loc) · 6.32 KB
/
Copy pathRNFBAppModule.js
File metadata and controls
261 lines (237 loc) · 6.32 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
251
252
253
254
255
256
257
258
259
260
261
import { initializeApp, setLogLevel, getApp, getApps, deleteApp } from './firebaseApp';
import { DeviceEventEmitter } from 'react-native';
// Variables for events tracking.
let jsReady = false;
let jsListenerCount = 0;
let queuedEvents = [];
let jsListeners = {};
// For compatibility we have a fake preferences storage,
// it does not persist across app restarts.
let fakePreferencesStorage = {};
function eventsGetListenersMap() {
return {
listeners: jsListenerCount,
queued: queuedEvents.length,
events: jsListeners,
};
}
function eventsSendEvent(eventName, eventBody) {
if (!jsReady || !jsListeners.hasOwnProperty(eventName)) {
const event = {
eventName,
eventBody,
};
queuedEvents.push(event);
return;
}
setImmediate(() => DeviceEventEmitter.emit('rnfb_' + eventName, eventBody));
}
function eventsSendQueuedEvents() {
if (queuedEvents.length === 0) {
return;
}
const events = Array.from(queuedEvents);
queuedEvents = [];
for (let i = 0; i < events.length; i++) {
const event = events[i];
eventsSendEvent(event.eventName, event.eventBody);
}
}
export default {
// Natively initialized apps, but in the case of web, we don't have any.
NATIVE_FIREBASE_APPS: [],
// The raw JSON string of the Firebase config, from the users firebase.json file.
// In the case of web, we can't support this.
FIREBASE_RAW_JSON: '{}',
/**
* Initializes a Firebase app.
*
* @param {object} options - The Firebase app options.
* @param {object} appConfig - The Firebase app config.
* @returns {object} - The Firebase app instance.
*/
async initializeApp(options, appConfig) {
const name = appConfig.name;
const existingApp = getApps().find(app => app.name === name);
if (existingApp) {
return existingApp;
}
const newAppConfig = {
name,
};
if (
appConfig.automaticDataCollectionEnabled === true ||
appConfig.automaticDataCollectionEnabled === false
) {
newAppConfig.automaticDataCollectionEnabled = appConfig.automaticDataCollectionEnabled;
}
const optionsCopy = Object.assign({}, options);
delete optionsCopy.clientId;
initializeApp(optionsCopy, newAppConfig);
return {
options,
appConfig,
};
},
/**
* Sets the log level for the Firebase app.
*
* @param {string} logLevel - The log level to set.
*/
setLogLevel(logLevel) {
setLogLevel(logLevel);
},
/**
* Sets the automatic data collection for the Firebase app.
*
* @param {string} appName - The name of the Firebase app.
* @param {boolean} enabled - Whether to enable automatic data collection.
*/
setAutomaticDataCollectionEnabled(appName, enabled) {
getApp(appName).automaticDataCollectionEnabled = enabled;
},
/**
* Deletes a Firebase app.
*
* @param {string} appName - The name of the Firebase app to delete.
*/
async deleteApp(appName) {
if (getApp(appName)) {
deleteApp(appName);
}
},
/**
* Gets the meta data.
* Unsupported on web.
*
* @returns {object} - The meta data
*/
metaGetAll() {
return {};
},
/**
* Gets the firebase.json data.
* Unsupported on web.
*
* @returns {object} - The JSON data for the firebase.json file.
*/
jsonGetAll() {
return {};
},
/**
* Sets a boolean value for a preference.
* Unsupported on web.
*
* @param {string} key - The key of the preference.
* @param {boolean} value - The value to set.
*/
async preferencesSetBool(key, value) {
fakePreferencesStorage[key] = value;
},
/**
* Sets a string value for a preference.
* Unsupported on web.
*
* @param {string} key - The key of the preference.
* @param {string} value - The value to set.
*/
preferencesSetString(key, value) {
fakePreferencesStorage[key] = value;
},
/**
* Gets all preferences.
* Unsupported on web.
*
* @returns {object} - The preferences.
*/
preferencesGetAll() {
return Object.assign({}, fakePreferencesStorage);
},
/**
* Clears all preferences.
* Unsupported on web.
*/
preferencesClearAll() {
fakePreferencesStorage = {};
},
/**
* Adds a listener for an event.
* Unsupported on web.
*
* @param {string} eventName - The name of the event to listen for.
*/
addListener() {
// Keep: Required for RN built in Event Emitter Calls.
},
/**
* Removes a listener for an event.
* Unsupported on web.
*
* @param {string} eventName - The name of the event to remove the listener for.
*/
removeListeners() {
// Keep: Required for RN built in Event Emitter Calls.
},
/**
* Notifies the app that it is ready to receive events.
*
* @param {boolean} ready - Whether the app is ready to receive events.
* @returns {void}
*/
eventsNotifyReady(ready) {
jsReady = ready;
if (jsReady) {
setImmediate(() => eventsSendQueuedEvents());
}
},
/**
* Gets all the listeners registered.
*
* @returns {object} - The listeners for the event.
*/
eventsGetListeners() {
return eventsGetListenersMap();
},
/**
* Sends an event to the app for testing purposes.
*
* @param {string} eventName - The name of the event to send.
* @param {object} eventBody - The body of the event to send.
* @returns {void}
*/
eventsPing(eventName, eventBody) {
eventsSendEvent(eventName, eventBody);
},
/**
* Adds a listener for an event.
*
* @param {string} eventName - The name of the event to listen for.
*/
eventsAddListener(eventName) {
jsListenerCount++;
if (!jsListeners.hasOwnProperty(eventName)) {
jsListeners[eventName] = 1;
} else {
jsListeners[eventName]++;
}
setImmediate(() => eventsSendQueuedEvents());
},
/**
* Removes a single listener for an event or all listeners for an event.
*
* @param {string} eventName - The name of the event to remove the listener for.
* @param {boolean} all - Optional. Whether to remove all listeners for the event.
*/
eventsRemoveListener(eventName, all) {
if (jsListeners.hasOwnProperty(eventName)) {
if (jsListeners[eventName] <= 1 || all) {
const count = jsListeners[eventName];
jsListenerCount -= count;
delete jsListeners[eventName];
} else {
jsListenerCount--;
jsListeners[eventName]--;
}
}
},
};