@@ -45,7 +45,6 @@ import {
45
45
QueryTargetState ,
46
46
SharedClientStateSyncer
47
47
} from '../local/shared_client_state_syncer' ;
48
- import * as objUtils from '../util/obj' ;
49
48
import { SortedSet } from '../util/sorted_set' ;
50
49
import { ListenSequence } from './listen_sequence' ;
51
50
import { Query , LimitType } from './query' ;
@@ -147,7 +146,7 @@ export class SyncEngine implements RemoteSyncer, SharedClientStateSyncer {
147
146
private queryViewsByQuery = new ObjectMap < Query , QueryView > ( q =>
148
147
q . canonicalId ( )
149
148
) ;
150
- private queriesByTarget : { [ targetId : number ] : Query [ ] } = { } ;
149
+ private queriesByTarget = new Map < TargetId , Query [ ] > ( ) ;
151
150
/**
152
151
* The keys of documents that are in limbo for which we haven't yet started a
153
152
* limbo resolution query.
@@ -164,9 +163,7 @@ export class SyncEngine implements RemoteSyncer, SharedClientStateSyncer {
164
163
* Keeps track of the information about an active limbo resolution for each
165
164
* active target ID that was started for the purpose of limbo resolution.
166
165
*/
167
- private activeLimboResolutionsByTarget : {
168
- [ targetId : number ] : LimboResolution ;
169
- } = { } ;
166
+ private activeLimboResolutionsByTarget = new Map < TargetId , LimboResolution > ( ) ;
170
167
private limboDocumentRefs = new ReferenceSet ( ) ;
171
168
/** Stores user completion handlers, indexed by User and BatchId. */
172
169
private mutationUserCallbacks = { } as {
@@ -285,10 +282,11 @@ export class SyncEngine implements RemoteSyncer, SharedClientStateSyncer {
285
282
286
283
const data = new QueryView ( query , targetId , view ) ;
287
284
this . queryViewsByQuery . set ( query , data ) ;
288
- if ( ! this . queriesByTarget [ targetId ] ) {
289
- this . queriesByTarget [ targetId ] = [ ] ;
285
+ if ( this . queriesByTarget . has ( targetId ) ) {
286
+ this . queriesByTarget . get ( targetId ) ! . push ( query ) ;
287
+ } else {
288
+ this . queriesByTarget . set ( targetId , [ query ] ) ;
290
289
}
291
- this . queriesByTarget [ targetId ] . push ( query ) ;
292
290
return viewChange . snapshot ! ;
293
291
}
294
292
@@ -322,10 +320,11 @@ export class SyncEngine implements RemoteSyncer, SharedClientStateSyncer {
322
320
323
321
// Only clean up the query view and target if this is the only query mapped
324
322
// to the target.
325
- const queries = this . queriesByTarget [ queryView . targetId ] ;
323
+ const queries = this . queriesByTarget . get ( queryView . targetId ) ! ;
326
324
if ( queries . length > 1 ) {
327
- this . queriesByTarget [ queryView . targetId ] = queries . filter (
328
- q => ! q . isEqual ( query )
325
+ this . queriesByTarget . set (
326
+ queryView . targetId ,
327
+ queries . filter ( q => ! q . isEqual ( query ) )
329
328
) ;
330
329
this . queryViewsByQuery . delete ( query ) ;
331
330
return ;
@@ -413,10 +412,10 @@ export class SyncEngine implements RemoteSyncer, SharedClientStateSyncer {
413
412
try {
414
413
const changes = await this . localStore . applyRemoteEvent ( remoteEvent ) ;
415
414
// Update `receivedDocument` as appropriate for any limbo targets.
416
- objUtils . forEach ( remoteEvent . targetChanges , ( targetId , targetChange ) => {
417
- const limboResolution = this . activeLimboResolutionsByTarget [
418
- Number ( targetId )
419
- ] ;
415
+ remoteEvent . targetChanges . forEach ( ( targetChange , targetId ) => {
416
+ const limboResolution = this . activeLimboResolutionsByTarget . get (
417
+ targetId
418
+ ) ;
420
419
if ( limboResolution ) {
421
420
// Since this is a limbo resolution lookup, it's for a single document
422
421
// and it could be added, modified, or removed, but not a combination.
@@ -495,15 +494,15 @@ export class SyncEngine implements RemoteSyncer, SharedClientStateSyncer {
495
494
// PORTING NOTE: Multi-tab only.
496
495
this . sharedClientState . updateQueryState ( targetId , 'rejected' , err ) ;
497
496
498
- const limboResolution = this . activeLimboResolutionsByTarget [ targetId ] ;
497
+ const limboResolution = this . activeLimboResolutionsByTarget . get ( targetId ) ;
499
498
const limboKey = limboResolution && limboResolution . key ;
500
499
if ( limboKey ) {
501
500
// Since this query failed, we won't want to manually unlisten to it.
502
501
// So go ahead and remove it from bookkeeping.
503
502
this . activeLimboTargetsByKey = this . activeLimboTargetsByKey . remove (
504
503
limboKey
505
504
) ;
506
- delete this . activeLimboResolutionsByTarget [ targetId ] ;
505
+ this . activeLimboResolutionsByTarget . delete ( targetId ) ;
507
506
this . pumpEnqueuedLimboResolutions ( ) ;
508
507
509
508
// TODO(klimt): We really only should do the following on permission
@@ -523,7 +522,7 @@ export class SyncEngine implements RemoteSyncer, SharedClientStateSyncer {
523
522
const resolvedLimboDocuments = documentKeySet ( ) . add ( limboKey ) ;
524
523
const event = new RemoteEvent (
525
524
SnapshotVersion . MIN ,
526
- /* targetChanges= */ { } ,
525
+ /* targetChanges= */ new Map < TargetId , TargetChange > ( ) ,
527
526
/* targetMismatches= */ new SortedSet < TargetId > ( primitiveComparator ) ,
528
527
documentUpdates ,
529
528
resolvedLimboDocuments
@@ -720,19 +719,19 @@ export class SyncEngine implements RemoteSyncer, SharedClientStateSyncer {
720
719
this . sharedClientState . removeLocalQueryTarget ( targetId ) ;
721
720
722
721
assert (
723
- this . queriesByTarget [ targetId ] &&
724
- this . queriesByTarget [ targetId ] . length !== 0 ,
722
+ this . queriesByTarget . has ( targetId ) &&
723
+ this . queriesByTarget . get ( targetId ) ! . length !== 0 ,
725
724
`There are no queries mapped to target id ${ targetId } `
726
725
) ;
727
726
728
- for ( const query of this . queriesByTarget [ targetId ] ) {
727
+ for ( const query of this . queriesByTarget . get ( targetId ) ! ) {
729
728
this . queryViewsByQuery . delete ( query ) ;
730
729
if ( error ) {
731
730
this . syncEngineListener ! . onWatchError ( query , error ) ;
732
731
}
733
732
}
734
733
735
- delete this . queriesByTarget [ targetId ] ;
734
+ this . queriesByTarget . delete ( targetId ) ;
736
735
737
736
if ( this . isPrimary ) {
738
737
const limboKeys = this . limboDocumentRefs . referencesForId ( targetId ) ;
@@ -758,7 +757,7 @@ export class SyncEngine implements RemoteSyncer, SharedClientStateSyncer {
758
757
759
758
this . remoteStore . unlisten ( limboTargetId ) ;
760
759
this . activeLimboTargetsByKey = this . activeLimboTargetsByKey . remove ( key ) ;
761
- delete this . activeLimboResolutionsByTarget [ limboTargetId ] ;
760
+ this . activeLimboResolutionsByTarget . delete ( limboTargetId ) ;
762
761
this . pumpEnqueuedLimboResolutions ( ) ;
763
762
}
764
763
@@ -810,8 +809,9 @@ export class SyncEngine implements RemoteSyncer, SharedClientStateSyncer {
810
809
) {
811
810
const key = this . enqueuedLimboResolutions . shift ( ) ! ;
812
811
const limboTargetId = this . limboTargetIdGenerator . next ( ) ;
813
- this . activeLimboResolutionsByTarget [ limboTargetId ] = new LimboResolution (
814
- key
812
+ this . activeLimboResolutionsByTarget . set (
813
+ limboTargetId ,
814
+ new LimboResolution ( key )
815
815
) ;
816
816
this . activeLimboTargetsByKey = this . activeLimboTargetsByKey . insert (
817
817
key ,
@@ -868,7 +868,7 @@ export class SyncEngine implements RemoteSyncer, SharedClientStateSyncer {
868
868
} )
869
869
. then ( ( viewDocChanges : ViewDocumentChanges ) => {
870
870
const targetChange =
871
- remoteEvent && remoteEvent . targetChanges [ queryView . targetId ] ;
871
+ remoteEvent && remoteEvent . targetChanges . get ( queryView . targetId ) ;
872
872
const viewChange = queryView . view . applyChanges (
873
873
viewDocChanges ,
874
874
/* updateLimboDocuments= */ this . isPrimary === true ,
@@ -957,7 +957,7 @@ export class SyncEngine implements RemoteSyncer, SharedClientStateSyncer {
957
957
const activeTargets : TargetId [ ] = [ ] ;
958
958
959
959
let p = Promise . resolve ( ) ;
960
- objUtils . forEachNumber ( this . queriesByTarget , ( targetId , _ ) => {
960
+ this . queriesByTarget . forEach ( ( _ , targetId ) => {
961
961
if ( this . sharedClientState . isLocalQueryTarget ( targetId ) ) {
962
962
activeTargets . push ( targetId ) ;
963
963
} else {
@@ -981,11 +981,11 @@ export class SyncEngine implements RemoteSyncer, SharedClientStateSyncer {
981
981
982
982
// PORTING NOTE: Multi-tab only.
983
983
private resetLimboDocuments ( ) : void {
984
- objUtils . forEachNumber ( this . activeLimboResolutionsByTarget , targetId => {
984
+ this . activeLimboResolutionsByTarget . forEach ( ( _ , targetId ) => {
985
985
this . remoteStore . unlisten ( targetId ) ;
986
986
} ) ;
987
987
this . limboDocumentRefs . removeAllReferences ( ) ;
988
- this . activeLimboResolutionsByTarget = [ ] ;
988
+ this . activeLimboResolutionsByTarget = new Map < TargetId , LimboResolution > ( ) ;
989
989
this . activeLimboTargetsByKey = new SortedMap < DocumentKey , TargetId > (
990
990
DocumentKey . comparator
991
991
) ;
@@ -1004,7 +1004,7 @@ export class SyncEngine implements RemoteSyncer, SharedClientStateSyncer {
1004
1004
const newViewSnapshots : ViewSnapshot [ ] = [ ] ;
1005
1005
for ( const targetId of targets ) {
1006
1006
let targetData : TargetData ;
1007
- const queries = this . queriesByTarget [ targetId ] ;
1007
+ const queries = this . queriesByTarget . get ( targetId ) ;
1008
1008
1009
1009
if ( queries && queries . length !== 0 ) {
1010
1010
// For queries that have a local View, we need to update their state
@@ -1096,7 +1096,7 @@ export class SyncEngine implements RemoteSyncer, SharedClientStateSyncer {
1096
1096
return ;
1097
1097
}
1098
1098
1099
- if ( this . queriesByTarget [ targetId ] ) {
1099
+ if ( this . queriesByTarget . has ( targetId ) ) {
1100
1100
switch ( state ) {
1101
1101
case 'current' :
1102
1102
case 'not-current' : {
@@ -1136,7 +1136,7 @@ export class SyncEngine implements RemoteSyncer, SharedClientStateSyncer {
1136
1136
1137
1137
for ( const targetId of added ) {
1138
1138
assert (
1139
- ! this . queriesByTarget [ targetId ] ,
1139
+ ! this . queriesByTarget . has ( targetId ) ,
1140
1140
'Trying to add an already active target'
1141
1141
) ;
1142
1142
const target = await this . localStore . getTarget ( targetId ) ;
@@ -1153,7 +1153,7 @@ export class SyncEngine implements RemoteSyncer, SharedClientStateSyncer {
1153
1153
for ( const targetId of removed ) {
1154
1154
// Check that the target is still active since the target might have been
1155
1155
// removed if it has been rejected by the backend.
1156
- if ( ! this . queriesByTarget [ targetId ] ) {
1156
+ if ( ! this . queriesByTarget . has ( targetId ) ) {
1157
1157
continue ;
1158
1158
}
1159
1159
@@ -1183,12 +1183,12 @@ export class SyncEngine implements RemoteSyncer, SharedClientStateSyncer {
1183
1183
}
1184
1184
1185
1185
getRemoteKeysForTarget ( targetId : TargetId ) : DocumentKeySet {
1186
- const limboResolution = this . activeLimboResolutionsByTarget [ targetId ] ;
1186
+ const limboResolution = this . activeLimboResolutionsByTarget . get ( targetId ) ;
1187
1187
if ( limboResolution && limboResolution . receivedDocument ) {
1188
1188
return documentKeySet ( ) . add ( limboResolution . key ) ;
1189
1189
} else {
1190
1190
let keySet = documentKeySet ( ) ;
1191
- const queries = this . queriesByTarget [ targetId ] ;
1191
+ const queries = this . queriesByTarget . get ( targetId ) ;
1192
1192
if ( ! queries ) {
1193
1193
return keySet ;
1194
1194
}
0 commit comments