15
15
* limitations under the License.
16
16
*/
17
17
18
- import { assert , contains , getModularInstance , Deferred } from '@firebase/util' ;
18
+ import { assert , getModularInstance , Deferred } from '@firebase/util' ;
19
19
20
20
import {
21
21
Repo ,
@@ -870,28 +870,24 @@ export class ValueEventRegistration implements EventRegistration {
870
870
}
871
871
872
872
/**
873
- * Represents the registration of 1 or more child_xxx events.
874
- *
875
- * Currently, it is always exactly 1 child_xxx event, but the idea is we might
876
- * let you register a group of callbacks together in the future.
873
+ * Represents the registration of a child_x event.
877
874
*/
878
875
export class ChildEventRegistration implements EventRegistration {
879
876
constructor (
880
- private callbacks : {
881
- [ child : string ] : CallbackContext ;
882
- } | null
877
+ private eventType : string ,
878
+ private callbackContext : CallbackContext | null
883
879
) { }
884
880
885
881
respondsTo ( eventType : string ) : boolean {
886
882
let eventToCheck =
887
883
eventType === 'children_added' ? 'child_added' : eventType ;
888
884
eventToCheck =
889
885
eventToCheck === 'children_removed' ? 'child_removed' : eventToCheck ;
890
- return contains ( this . callbacks , eventToCheck ) ;
886
+ return this . eventType === eventToCheck ;
891
887
}
892
888
893
889
createCancelEvent ( error : Error , path : Path ) : CancelEvent | null {
894
- if ( this . callbacks [ 'cancel' ] . hasCancelCallback ) {
890
+ if ( this . callbackContext . hasCancelCallback ) {
895
891
return new CancelEvent ( this , error , path ) ;
896
892
} else {
897
893
return null ;
@@ -915,12 +911,11 @@ export class ChildEventRegistration implements EventRegistration {
915
911
916
912
getEventRunner ( eventData : CancelEvent | DataEvent ) : ( ) => void {
917
913
if ( eventData . getEventType ( ) === 'cancel' ) {
918
- const cancelCB = this . callbacks [ 'cancel' ] ;
919
- return ( ) => cancelCB . onCancel ( ( eventData as CancelEvent ) . error ) ;
914
+ return ( ) =>
915
+ this . callbackContext . onCancel ( ( eventData as CancelEvent ) . error ) ;
920
916
} else {
921
- const cb = this . callbacks [ ( eventData as DataEvent ) . eventType ] ;
922
917
return ( ) =>
923
- cb . onValue (
918
+ this . callbackContext . onValue (
924
919
( eventData as DataEvent ) . snapshot ,
925
920
( eventData as DataEvent ) . prevName
926
921
) ;
@@ -929,42 +924,19 @@ export class ChildEventRegistration implements EventRegistration {
929
924
930
925
matches ( other : EventRegistration ) : boolean {
931
926
if ( other instanceof ChildEventRegistration ) {
932
- if ( ! this . callbacks || ! other . callbacks ) {
933
- return true ;
934
- } else {
935
- const otherKeys = Object . keys ( other . callbacks ) ;
936
- const thisKeys = Object . keys ( this . callbacks ) ;
937
- const otherCount = otherKeys . length ;
938
- const thisCount = thisKeys . length ;
939
- if ( otherCount === thisCount ) {
940
- // If count is 1, do an exact match on eventType, if either is defined but null, it's a match.
941
- // If event types don't match, not a match
942
- // If count is not 1, exact match across all
943
-
944
- if ( otherCount === 1 ) {
945
- const otherKey = otherKeys [ 0 ] ;
946
- const thisKey = thisKeys [ 0 ] ;
947
- return (
948
- thisKey === otherKey &&
949
- ( ! other . callbacks [ otherKey ] ||
950
- ! this . callbacks [ thisKey ] ||
951
- other . callbacks [ otherKey ] . matches ( this . callbacks [ thisKey ] ) )
952
- ) ;
953
- } else {
954
- // Exact match on each key.
955
- return thisKeys . every ( eventType =>
956
- other . callbacks [ eventType ] . matches ( this . callbacks [ eventType ] )
957
- ) ;
958
- }
959
- }
960
- }
927
+ return (
928
+ this . eventType === other . eventType &&
929
+ ( ! this . callbackContext ||
930
+ ! other . callbackContext ||
931
+ this . callbackContext . matches ( other . callbackContext ) )
932
+ ) ;
961
933
}
962
934
963
935
return false ;
964
936
}
965
937
966
938
hasAnyCallback ( ) : boolean {
967
- return this . callbacks !== null ;
939
+ return ! ! this . callbackContext ;
968
940
}
969
941
}
970
942
@@ -1002,9 +974,7 @@ function addEventListener(
1002
974
const container =
1003
975
eventType === 'value'
1004
976
? new ValueEventRegistration ( callbackContext )
1005
- : new ChildEventRegistration ( {
1006
- [ eventType ] : callbackContext
1007
- } ) ;
977
+ : new ChildEventRegistration ( eventType , callbackContext ) ;
1008
978
repoAddEventCallbackForQuery ( query . _repo , query , container ) ;
1009
979
return ( ) => repoRemoveEventCallbackForQuery ( query . _repo , query , container ) ;
1010
980
}
@@ -1656,16 +1626,11 @@ export function off(
1656
1626
) => unknown
1657
1627
) : void {
1658
1628
let container : EventRegistration | null = null ;
1659
- let callbacks : { [ k : string ] : CallbackContext } | null = null ;
1660
1629
const expCallback = callback ? new CallbackContext ( callback ) : null ;
1661
1630
if ( eventType === 'value' ) {
1662
1631
container = new ValueEventRegistration ( expCallback ) ;
1663
1632
} else if ( eventType ) {
1664
- if ( callback ) {
1665
- callbacks = { } ;
1666
- callbacks [ eventType ] = expCallback ;
1667
- }
1668
- container = new ChildEventRegistration ( callbacks ) ;
1633
+ container = new ChildEventRegistration ( eventType , expCallback ) ;
1669
1634
}
1670
1635
repoRemoveEventCallbackForQuery ( query . _repo , query , container ) ;
1671
1636
}
0 commit comments