27
27
public class FirestackDatabase extends ReactContextBaseJavaModule {
28
28
private static final String TAG = "FirestackDatabase" ;
29
29
private HashMap <String , FirestackDatabaseReference > mDBListeners = new HashMap <String , FirestackDatabaseReference >();
30
+ private FirebaseDatabase mFirebaseDatabase ;
30
31
31
32
public FirestackDatabase (ReactApplicationContext reactContext ) {
32
33
super (reactContext );
34
+ mFirebaseDatabase = FirebaseDatabase .getInstance ();
33
35
}
34
36
35
37
@ Override
@@ -43,8 +45,7 @@ public void enablePersistence(
43
45
final Boolean enable ,
44
46
final Callback callback ) {
45
47
try {
46
- FirebaseDatabase .getInstance ()
47
- .setPersistenceEnabled (enable );
48
+ mFirebaseDatabase .setPersistenceEnabled (enable );
48
49
} catch (Throwable t ) {
49
50
Log .e (TAG , "FirebaseDatabase setPersistenceEnabled exception" , t );
50
51
}
@@ -59,7 +60,7 @@ public void keepSynced(
59
60
final String path ,
60
61
final Boolean enable ,
61
62
final Callback callback ) {
62
- DatabaseReference ref = this . getDatabaseReferenceAtPath (path );
63
+ DatabaseReference ref = mFirebaseDatabase . getReference (path );
63
64
ref .keepSynced (enable );
64
65
65
66
WritableMap res = Arguments .createMap ();
@@ -74,15 +75,13 @@ public void set(
74
75
final String path ,
75
76
final ReadableMap props ,
76
77
final Callback callback ) {
77
- DatabaseReference ref = this .getDatabaseReferenceAtPath (path );
78
-
79
- final FirestackDatabase self = this ;
78
+ DatabaseReference ref = mFirebaseDatabase .getReference (path );
80
79
Map <String , Object > m = Utils .recursivelyDeconstructReadableMap (props );
81
80
82
81
DatabaseReference .CompletionListener listener = new DatabaseReference .CompletionListener () {
83
82
@ Override
84
83
public void onComplete (DatabaseError error , DatabaseReference ref ) {
85
- handleCallback ("set" , callback , error , ref );
84
+ handleCallback ("set" , callback , error );
86
85
}
87
86
};
88
87
@@ -93,14 +92,13 @@ public void onComplete(DatabaseError error, DatabaseReference ref) {
93
92
public void update (final String path ,
94
93
final ReadableMap props ,
95
94
final Callback callback ) {
96
- DatabaseReference ref = this .getDatabaseReferenceAtPath (path );
97
- final FirestackDatabase self = this ;
95
+ DatabaseReference ref = mFirebaseDatabase .getReference (path );
98
96
Map <String , Object > m = Utils .recursivelyDeconstructReadableMap (props );
99
97
100
98
DatabaseReference .CompletionListener listener = new DatabaseReference .CompletionListener () {
101
99
@ Override
102
100
public void onComplete (DatabaseError error , DatabaseReference ref ) {
103
- handleCallback ("update" , callback , error , ref );
101
+ handleCallback ("update" , callback , error );
104
102
}
105
103
};
106
104
@@ -110,12 +108,11 @@ public void onComplete(DatabaseError error, DatabaseReference ref) {
110
108
@ ReactMethod
111
109
public void remove (final String path ,
112
110
final Callback callback ) {
113
- DatabaseReference ref = this .getDatabaseReferenceAtPath (path );
114
- final FirestackDatabase self = this ;
111
+ DatabaseReference ref = mFirebaseDatabase .getReference (path );
115
112
DatabaseReference .CompletionListener listener = new DatabaseReference .CompletionListener () {
116
113
@ Override
117
114
public void onComplete (DatabaseError error , DatabaseReference ref ) {
118
- handleCallback ("remove" , callback , error , ref );
115
+ handleCallback ("remove" , callback , error );
119
116
}
120
117
};
121
118
@@ -128,7 +125,7 @@ public void push(final String path,
128
125
final Callback callback ) {
129
126
130
127
Log .d (TAG , "Called push with " + path );
131
- DatabaseReference ref = this . getDatabaseReferenceAtPath (path );
128
+ DatabaseReference ref = mFirebaseDatabase . getReference (path );
132
129
DatabaseReference newRef = ref .push ();
133
130
134
131
final Uri url = Uri .parse (newRef .toString ());
@@ -138,7 +135,6 @@ public void push(final String path,
138
135
if (iterator .hasNextKey ()) {
139
136
Log .d (TAG , "Passed value to push" );
140
137
// lame way to check if the `props` are empty
141
- final FirestackDatabase self = this ;
142
138
Map <String , Object > m = Utils .recursivelyDeconstructReadableMap (props );
143
139
144
140
DatabaseReference .CompletionListener listener = new DatabaseReference .CompletionListener () {
@@ -175,20 +171,16 @@ public void on(final String path,
175
171
final ReadableArray modifiersArray ,
176
172
final String name ,
177
173
final Callback callback ) {
178
- FirestackDatabaseReference ref = this .getDBHandle (path , modifiersString );
179
-
180
- WritableMap resp = Arguments .createMap ();
174
+ FirestackDatabaseReference ref = this .getDBHandle (path , modifiersArray , modifiersString );
181
175
182
176
if (name .equals ("value" )) {
183
- ref .addValueEventListener (name , modifiersArray , modifiersString );
177
+ ref .addValueEventListener ();
184
178
} else {
185
- ref .addChildEventListener (name , modifiersArray , modifiersString );
179
+ ref .addChildEventListener (name );
186
180
}
187
181
188
- this . saveDBHandle ( path , modifiersString , ref );
182
+ WritableMap resp = Arguments . createMap ( );
189
183
resp .putString ("result" , "success" );
190
- Log .d (TAG , "Added listener " + name + " for " + ref + "with modifiers: " + modifiersString );
191
-
192
184
resp .putString ("handle" , path );
193
185
callback .invoke (null , resp );
194
186
}
@@ -199,9 +191,8 @@ public void onOnce(final String path,
199
191
final ReadableArray modifiersArray ,
200
192
final String name ,
201
193
final Callback callback ) {
202
- Log .d (TAG , "Setting one-time listener on event: " + name + " for path " + path );
203
- FirestackDatabaseReference ref = this .getDBHandle (path , modifiersString );
204
- ref .addOnceValueEventListener (modifiersArray , modifiersString , callback );
194
+ FirestackDatabaseReference ref = this .getDBHandle (path , modifiersArray , modifiersString );
195
+ ref .addOnceValueEventListener (callback );
205
196
}
206
197
207
198
/**
@@ -227,82 +218,60 @@ public void off(
227
218
// On Disconnect
228
219
@ ReactMethod
229
220
public void onDisconnectSetObject (final String path , final ReadableMap props , final Callback callback ) {
230
- DatabaseReference ref = this . getDatabaseReferenceAtPath (path );
221
+ DatabaseReference ref = mFirebaseDatabase . getReference (path );
231
222
Map <String , Object > m = Utils .recursivelyDeconstructReadableMap (props );
232
223
233
224
OnDisconnect od = ref .onDisconnect ();
234
225
od .setValue (m , new DatabaseReference .CompletionListener () {
235
226
@ Override
236
- public void onComplete (DatabaseError databaseError , DatabaseReference databaseReference ) {
237
- handleCallback ("onDisconnectSetObject" , callback , databaseError , databaseReference );
227
+ public void onComplete (DatabaseError error , DatabaseReference ref ) {
228
+ handleCallback ("onDisconnectSetObject" , callback , error );
238
229
}
239
230
});
240
231
}
241
232
242
233
@ ReactMethod
243
234
public void onDisconnectSetString (final String path , final String value , final Callback callback ) {
244
- DatabaseReference ref = this . getDatabaseReferenceAtPath (path );
235
+ DatabaseReference ref = mFirebaseDatabase . getReference (path );
245
236
246
237
OnDisconnect od = ref .onDisconnect ();
247
238
od .setValue (value , new DatabaseReference .CompletionListener () {
248
239
@ Override
249
- public void onComplete (DatabaseError databaseError , DatabaseReference databaseReference ) {
250
- handleCallback ("onDisconnectSetString" , callback , databaseError , databaseReference );
240
+ public void onComplete (DatabaseError error , DatabaseReference ref ) {
241
+ handleCallback ("onDisconnectSetString" , callback , error );
251
242
}
252
243
});
253
244
}
254
245
255
246
@ ReactMethod
256
247
public void onDisconnectRemove (final String path , final Callback callback ) {
257
- DatabaseReference ref = this . getDatabaseReferenceAtPath (path );
248
+ DatabaseReference ref = mFirebaseDatabase . getReference (path );
258
249
259
250
OnDisconnect od = ref .onDisconnect ();
260
251
od .removeValue (new DatabaseReference .CompletionListener () {
261
252
@ Override
262
- public void onComplete (DatabaseError databaseError , DatabaseReference databaseReference ) {
263
- handleCallback ("onDisconnectRemove" , callback , databaseError , databaseReference );
253
+ public void onComplete (DatabaseError error , DatabaseReference ref ) {
254
+ handleCallback ("onDisconnectRemove" , callback , error );
264
255
}
265
256
});
266
257
}
267
258
@ ReactMethod
268
259
public void onDisconnectCancel (final String path , final Callback callback ) {
269
- DatabaseReference ref = this . getDatabaseReferenceAtPath (path );
260
+ DatabaseReference ref = mFirebaseDatabase . getReference (path );
270
261
271
262
OnDisconnect od = ref .onDisconnect ();
272
263
od .cancel (new DatabaseReference .CompletionListener () {
273
264
@ Override
274
- public void onComplete (DatabaseError databaseError , DatabaseReference databaseReference ) {
275
- handleCallback ("onDisconnectCancel" , callback , databaseError , databaseReference );
265
+ public void onComplete (DatabaseError error , DatabaseReference ref ) {
266
+ handleCallback ("onDisconnectCancel" , callback , error );
276
267
}
277
268
});
278
269
}
279
270
280
- // Private helpers
281
- // private void handleDatabaseEvent(final String name, final DataSnapshot dataSnapshot) {
282
- // WritableMap data = this.dataSnapshotToMap(name, dataSnapshot);
283
- // WritableMap evt = Arguments.createMap();
284
- // evt.putString("eventName", name);
285
- // evt.putMap("body", data);
286
- // Utils.sendEvent(mReactContext, "database_event", evt);
287
- // }
288
-
289
- // private void handleDatabaseError(final String name, final DatabaseError error) {
290
- // WritableMap err = Arguments.createMap();
291
- // err.putInt("errorCode", error.getCode());
292
- // err.putString("errorDetails", error.getDetails());
293
- // err.putString("description", error.getMessage());
294
-
295
- // WritableMap evt = Arguments.createMap();
296
- // evt.putString("eventName", name);
297
- // evt.putMap("body", err);
298
- // Utils.sendEvent(mReactContext, "database_error", evt);
299
- // }
300
-
301
271
private void handleCallback (
302
272
final String methodName ,
303
273
final Callback callback ,
304
- final DatabaseError databaseError ,
305
- final DatabaseReference databaseReference ) {
274
+ final DatabaseError databaseError ) {
306
275
if (databaseError != null ) {
307
276
WritableMap err = Arguments .createMap ();
308
277
err .putInt ("errorCode" , databaseError .getCode ());
@@ -317,19 +286,19 @@ private void handleCallback(
317
286
}
318
287
}
319
288
320
- private FirestackDatabaseReference getDBHandle (final String path , final String modifiersString ) {
289
+ private FirestackDatabaseReference getDBHandle (final String path ,
290
+ final ReadableArray modifiersArray ,
291
+ final String modifiersString ) {
321
292
String key = this .getDBListenerKey (path , modifiersString );
322
- if (!mDBListeners .containsKey (key )) {
293
+ FirestackDatabaseReference r = mDBListeners .get (key );
294
+
295
+ if (r == null ) {
323
296
ReactContext ctx = getReactApplicationContext ();
324
- mDBListeners .put (key , new FirestackDatabaseReference (ctx , path ));
297
+ r = new FirestackDatabaseReference (ctx , mFirebaseDatabase , path , modifiersArray , modifiersString );
298
+ mDBListeners .put (key , r );
325
299
}
326
300
327
- return mDBListeners .get (key );
328
- }
329
-
330
- private void saveDBHandle (final String path , String modifiersString , final FirestackDatabaseReference dbRef ) {
331
- String key = this .getDBListenerKey (path , modifiersString );
332
- mDBListeners .put (key , dbRef );
301
+ return r ;
333
302
}
334
303
335
304
private String getDBListenerKey (String path , String modifiersString ) {
@@ -338,20 +307,11 @@ private String getDBListenerKey(String path, String modifiersString) {
338
307
339
308
private void removeDBHandle (final String path , String modifiersString ) {
340
309
String key = this .getDBListenerKey (path , modifiersString );
341
- if (mDBListeners .containsKey (key )) {
342
- FirestackDatabaseReference r = mDBListeners .get (key );
310
+ FirestackDatabaseReference r = mDBListeners .get (key );
311
+
312
+ if (r != null ) {
343
313
r .cleanup ();
344
314
mDBListeners .remove (key );
345
315
}
346
316
}
347
-
348
- private String keyPath (final String path , final String eventName ) {
349
- return path + "-" + eventName ;
350
- }
351
-
352
- // TODO: move to FirestackDatabaseReference?
353
- private DatabaseReference getDatabaseReferenceAtPath (final String path ) {
354
- DatabaseReference mDatabase = FirebaseDatabase .getInstance ().getReference (path );
355
- return mDatabase ;
356
- }
357
317
}
0 commit comments