@@ -211,10 +211,14 @@ class DatabaseRef extends ReferenceBase {
211211 } )
212212 }
213213
214- off ( evt = '' ) {
214+ off ( evt = '' , origCB ) {
215215 const path = this . dbPath ( ) ;
216- return this . db . off ( path , evt )
216+ return this . db . off ( path , evt , origCB )
217217 . then ( ( { callback, subscriptions} ) => {
218+ if ( dbSubscriptions [ path ] [ evt ] . length > 0 ) {
219+ return subscriptions ;
220+ }
221+
218222 return promisify ( 'off' , FirestackDatabase ) ( path , evt )
219223 . then ( ( ) => {
220224 // subscriptions.forEach(sub => sub.remove());
@@ -432,15 +436,20 @@ export class Database extends Base {
432436 return Promise . resolve ( { callback, subscriptions} ) ;
433437 }
434438
435- off ( path , evt ) {
439+ off ( path , evt , origCB ) {
436440 const key = this . _pathKey ( path ) ;
437441 // Remove subscription
438442 if ( dbSubscriptions [ key ] ) {
439443 if ( ! evt || evt === "" ) {
440444 dbSubscriptions [ key ] = { } ;
441445 } else if ( dbSubscriptions [ key ] [ evt ] ) {
442- delete dbSubscriptions [ key ] [ evt ] ;
446+ if ( origCB ) {
447+ dbSubscriptions [ key ] [ evt ] . splice ( dbSubscriptions [ key ] [ evt ] . indexOf ( origCB ) , 1 ) ;
448+ } else {
449+ delete dbSubscriptions [ key ] [ evt ] ;
450+ }
443451 }
452+
444453 if ( Object . keys ( dbSubscriptions [ key ] ) . length <= 0 ) {
445454 // there are no more subscriptions
446455 // so we can unwatch
0 commit comments