@@ -62,42 +62,34 @@ public void setModifiers(final ReadableArray modifiers) {
6262 public void addChildEventListener (final String name , final ReadableArray modifiers ) {
6363 final FirestackDBReference self = this ;
6464
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 );
7070 }
71- }
7271
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 );
7775 }
78- }
7976
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 );
8480 }
85- }
8681
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 );
9185 }
92- }
9386
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+ }
10193
10294 Query ref = this .getDatabaseQueryAtPathAndModifiers (modifiers );
10395 ref .addChildEventListener (mEventListener );
@@ -196,8 +188,11 @@ public void removeValueEventListener() {
196188 }
197189
198190 private void handleDatabaseEvent (final String name , final String path , final DataSnapshot dataSnapshot ) {
191+ if (!FirestackDBReference .this .isListeningTo (path , name )) {
192+ return ;
193+ }
199194 WritableMap data = FirestackUtils .dataSnapshotToMap (name , path , dataSnapshot );
200- WritableMap evt = Arguments .createMap ();
195+ WritableMap evt = Arguments .createMap ();
201196 evt .putString ("eventName" , name );
202197 evt .putString ("path" , path );
203198 evt .putMap ("body" , data );
@@ -470,18 +465,8 @@ public void onOnce(final String path,
470465
471466 @ ReactMethod
472467 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 );
485470 WritableMap resp = Arguments .createMap ();
486471 resp .putString ("handle" , path );
487472 resp .putString ("result" , "success" );
@@ -582,26 +567,25 @@ private void handleCallback(
582567 }
583568
584569 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 )) {
587572 ReactContext ctx = getReactApplicationContext ();
588- mDBListeners .put (key , new FirestackDBReference (ctx , path ));
573+ mDBListeners .put (keyPath , new FirestackDBReference (ctx , path ));
589574 }
590575
591- return mDBListeners .get (key );
576+ return mDBListeners .get (keyPath );
592577 }
593578
594579 private void saveDBHandle (final String path ,
595580 final String eventName ,
596581 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 );
600584 }
601585
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 );
605589 r .cleanup ();
606590 }
607591 }
0 commit comments