@@ -62,42 +62,34 @@ public void setModifiers(final ReadableArray modifiers) {
62
62
public void addChildEventListener (final String name , final ReadableArray modifiers ) {
63
63
final FirestackDBReference self = this ;
64
64
65
- mEventListener = new ChildEventListener ( ) {
66
- @ Override
67
- public void onChildAdded ( DataSnapshot dataSnapshot , String previousChildName ) {
68
- if ( name . equals ( "child_added" ) ) {
69
- self .handleDatabaseEvent (name , mPath , dataSnapshot );
65
+ if ( mEventListener == null ) {
66
+ mEventListener = new ChildEventListener () {
67
+ @ Override
68
+ public void onChildAdded ( DataSnapshot dataSnapshot , String previousChildName ) {
69
+ self .handleDatabaseEvent ("child_added" , mPath , dataSnapshot );
70
70
}
71
- }
72
71
73
- @ Override
74
- public void onChildChanged (DataSnapshot dataSnapshot , String previousChildName ) {
75
- if (name .equals ("child_changed" )) {
76
- self .handleDatabaseEvent (name , mPath , dataSnapshot );
72
+ @ Override
73
+ public void onChildChanged (DataSnapshot dataSnapshot , String previousChildName ) {
74
+ self .handleDatabaseEvent ("child_changed" , mPath , dataSnapshot );
77
75
}
78
- }
79
76
80
- @ Override
81
- public void onChildRemoved (DataSnapshot dataSnapshot ) {
82
- if (name .equals ("child_removed" )) {
83
- self .handleDatabaseEvent (name , mPath , dataSnapshot );
77
+ @ Override
78
+ public void onChildRemoved (DataSnapshot dataSnapshot ) {
79
+ self .handleDatabaseEvent ("child_removed" , mPath , dataSnapshot );
84
80
}
85
- }
86
81
87
- @ Override
88
- public void onChildMoved (DataSnapshot dataSnapshot , String previousChildName ) {
89
- if (name .equals ("child_moved" )) {
90
- self .handleDatabaseEvent (name , mPath , dataSnapshot );
82
+ @ Override
83
+ public void onChildMoved (DataSnapshot dataSnapshot , String previousChildName ) {
84
+ self .handleDatabaseEvent ("child_moved" , mPath , dataSnapshot );
91
85
}
92
- }
93
86
94
- @ Override
95
- public void onCancelled (DatabaseError error ) {
96
- //if (name.equals("child_added")) {
97
- //self.handleDatabaseError(name, mPath, error);
98
- //}
99
- }
100
- };
87
+ @ Override
88
+ public void onCancelled (DatabaseError error ) {
89
+ self .handleDatabaseError (name , mPath , error );
90
+ }
91
+ };
92
+ }
101
93
102
94
Query ref = this .getDatabaseQueryAtPathAndModifiers (modifiers );
103
95
ref .addChildEventListener (mEventListener );
@@ -196,8 +188,11 @@ public void removeValueEventListener() {
196
188
}
197
189
198
190
private void handleDatabaseEvent (final String name , final String path , final DataSnapshot dataSnapshot ) {
191
+ if (!FirestackDBReference .this .isListeningTo (path , name )) {
192
+ return ;
193
+ }
199
194
WritableMap data = FirestackUtils .dataSnapshotToMap (name , path , dataSnapshot );
200
- WritableMap evt = Arguments .createMap ();
195
+ WritableMap evt = Arguments .createMap ();
201
196
evt .putString ("eventName" , name );
202
197
evt .putString ("path" , path );
203
198
evt .putMap ("body" , data );
@@ -470,18 +465,8 @@ public void onOnce(final String path,
470
465
471
466
@ ReactMethod
472
467
public void off (final String path , final String name , final Callback callback ) {
473
- // TODO
474
- FirestackDBReference ref = this .getDBHandle (path , name );
475
-
476
- if (name .equals ("value" )) {
477
- ref .removeValueEventListener ();
478
- } else {
479
- ref .removeChildEventListener ();
480
- }
481
-
482
- String key = this .keyPath (path , name );
483
- this .removeDBHandle (key );
484
- Log .d (TAG , "Removed listener " + name );
468
+ String keyPath = this .keyPath (path , name );
469
+ this .removeDBHandle (keyPath );
485
470
WritableMap resp = Arguments .createMap ();
486
471
resp .putString ("handle" , path );
487
472
resp .putString ("result" , "success" );
@@ -582,26 +567,25 @@ private void handleCallback(
582
567
}
583
568
584
569
private FirestackDBReference getDBHandle (final String path , final String eventName ) {
585
- String key = this .keyPath (path , eventName );
586
- if (!mDBListeners .containsKey (key )) {
570
+ String keyPath = this .keyPath (path , eventName );
571
+ if (!mDBListeners .containsKey (keyPath )) {
587
572
ReactContext ctx = getReactApplicationContext ();
588
- mDBListeners .put (key , new FirestackDBReference (ctx , path ));
573
+ mDBListeners .put (keyPath , new FirestackDBReference (ctx , path ));
589
574
}
590
575
591
- return mDBListeners .get (key );
576
+ return mDBListeners .get (keyPath );
592
577
}
593
578
594
579
private void saveDBHandle (final String path ,
595
580
final String eventName ,
596
581
final FirestackDBReference dbRef ) {
597
- String key = this .keyPath (path , eventName );
598
- this .removeDBHandle (key );
599
- mDBListeners .put (key , dbRef );
582
+ String keyPath = this .keyPath (path , eventName );
583
+ mDBListeners .put (keyPath , dbRef );
600
584
}
601
585
602
- private void removeDBHandle (final String key ) {
603
- if (mDBListeners .containsKey (key )) {
604
- FirestackDBReference r = mDBListeners .get (key );
586
+ private void removeDBHandle (final String keyPath ) {
587
+ if (mDBListeners .containsKey (keyPath )) {
588
+ FirestackDBReference r = mDBListeners .get (keyPath );
605
589
r .cleanup ();
606
590
}
607
591
}
0 commit comments