@@ -56,7 +56,7 @@ import {
56
56
AddedLimboDocument ,
57
57
LimboDocumentChange ,
58
58
RemovedLimboDocument ,
59
- View ,
59
+ View , ViewChange ,
60
60
ViewDocumentChanges
61
61
} from './view' ;
62
62
import { ViewSnapshot } from './view_snapshot' ;
@@ -262,14 +262,12 @@ export class SyncEngine implements RemoteSyncer, SharedClientStateSyncer {
262
262
* persistence.
263
263
*/
264
264
// PORTING NOTE: Multi-tab only.
265
- private async synchronizeLocalView ( targetId : TargetId ) : Promise < void > {
266
- return this . localStore
267
- . remoteDocumentKeys ( targetId )
268
- . then ( async remoteKeys => {
269
- const queryView = this . queryViewsByTarget [ targetId ] ;
270
- assert ( ! ! queryView , 'Expected queryView to be defined' ) ;
271
- queryView . view . synchronizeWithRemoteKeys ( remoteKeys ) ;
272
- } ) ;
265
+ private async synchronizeLocalView ( query :Query , targetId : TargetId ) : Promise < ViewChange > {
266
+ return this . localStore . executeQuery ( query ) . then ( docs => {
267
+ const queryView = this . queryViewsByTarget [ targetId ] ;
268
+ assert ( ! ! queryView , 'Expected queryView to be defined' ) ;
269
+ return queryView . view . synchronizeWithRemoteDocuments ( docs ) ;
270
+ } ) ;
273
271
}
274
272
275
273
/** Stops listening to the query. */
@@ -504,17 +502,6 @@ export class SyncEngine implements RemoteSyncer, SharedClientStateSyncer {
504
502
// connection is disabled.
505
503
await this . remoteStore . fillWritePipeline ( ) ;
506
504
} else if ( batchState === 'acknowledged' || batchState === 'rejected' ) {
507
- // if (this.isPrimary) {
508
- // // If we receive a notification of an `acknowledged` or `rejected` batch
509
- // // via Web Storage, we are either already secondary or another tab has
510
- // // taken the primary lease.
511
- // log.debug(
512
- // LOG_TAG,
513
- // 'Unexpectedly received mutation batch notification when already primary. Releasing primary lease.'
514
- // );
515
- // await this.applyPrimaryState(false);
516
- // }
517
-
518
505
// NOTE: Both these methods are no-ops for batches that originated from
519
506
// other clients.
520
507
this . sharedClientState . removeLocalPendingMutation ( batchId ) ;
@@ -827,7 +814,7 @@ export class SyncEngine implements RemoteSyncer, SharedClientStateSyncer {
827
814
// state (the list of syncedDocuments may have gotten out of sync).
828
815
await this . localStore . releaseQuery ( query , true ) ;
829
816
queryData = await this . localStore . allocateQuery ( query ) ;
830
- await this . synchronizeLocalView ( targetId ) ;
817
+ await this . synchronizeLocalView ( query , targetId ) ;
831
818
}
832
819
this . remoteStore . listen ( queryData ) ;
833
820
} ) ;
@@ -840,16 +827,21 @@ export class SyncEngine implements RemoteSyncer, SharedClientStateSyncer {
840
827
} else if ( isPrimary === false && this . isPrimary !== false ) {
841
828
this . isPrimary = false ;
842
829
await this . remoteStore . disableNetwork ( ) ;
843
- objUtils . forEachNumber ( this . queryViewsByTarget , targetId => {
844
- // TODO(multitab): Remove query views for non-local queries.
845
830
846
- const queryView = this . queryViewsByTarget [ targetId ] ;
847
- const viewChange = queryView . view . clearLimboDocuments ( ) ;
848
- if ( viewChange . snapshot ) {
849
- this . viewHandler ! ( [ viewChange . snapshot ] ) ;
850
- }
831
+ let p = Promise . resolve ( ) ;
832
+ objUtils . forEachNumber ( this . queryViewsByTarget , targetId => {
833
+ p = p . then ( async ( ) => {
834
+ let queryView = this . queryViewsByTarget [ targetId ] ;
835
+ // TODO(multitab): Remove query views for non-local queries.
836
+ const viewChange = await this . synchronizeLocalView ( queryView . query , targetId ) ;
837
+ // const viewChange = queryView.view.clearLimboDocuments();
838
+ if ( viewChange . snapshot ) {
839
+ this . viewHandler ! ( [ viewChange . snapshot ] ) ;
840
+ }
841
+ } ) ;
851
842
this . remoteStore . unlisten ( targetId ) ;
852
843
} ) ;
844
+ await p ;
853
845
}
854
846
}
855
847
@@ -864,15 +856,15 @@ export class SyncEngine implements RemoteSyncer, SharedClientStateSyncer {
864
856
state : QueryTargetState ,
865
857
error ?: FirestoreError
866
858
) : Promise < void > {
867
- // if (this.isPrimary) {
868
- // // If we receive a target state notification via Web Storage, we are
869
- // // either already secondary or another tab has taken the primary lease.
870
- // log.debug(
871
- // LOG_TAG,
872
- // 'Unexpectedly received query state notification when already primary. Releasing primary lease .'
873
- // );
874
- // await this.applyPrimaryState(false) ;
875
- // }
859
+ if ( this . isPrimary ) {
860
+ // If we receive a target state notification via Web Storage, we are
861
+ // either already secondary or another tab has taken the primary lease.
862
+ log . debug (
863
+ LOG_TAG ,
864
+ 'Unexpectedly received query state notification when already primary. Ignoring .'
865
+ ) ;
866
+ return ;
867
+ }
876
868
877
869
if ( this . queryViewsByTarget [ targetId ] ) {
878
870
switch ( state ) {
0 commit comments