@@ -1032,9 +1032,6 @@ export class MultiTabSyncEngine extends SyncEngine
1032
1032
1033
1033
async applyPrimaryState ( isPrimary : boolean ) : Promise < void > {
1034
1034
if ( isPrimary === true && this . isPrimary !== true ) {
1035
- this . isPrimary = true ;
1036
- await this . remoteStore . applyPrimaryState ( true ) ;
1037
-
1038
1035
// Secondary tabs only maintain Views for their local listeners and the
1039
1036
// Views internal state may not be 100% populated (in particular
1040
1037
// secondary tabs don't track syncedDocuments, the set of documents the
@@ -1043,14 +1040,15 @@ export class MultiTabSyncEngine extends SyncEngine
1043
1040
// match the state on disk.
1044
1041
const activeTargets = this . sharedClientState . getAllActiveQueryTargets ( ) ;
1045
1042
const activeQueries = await this . synchronizeQueryViewsAndRaiseSnapshots (
1046
- activeTargets . toArray ( )
1043
+ activeTargets . toArray ( ) ,
1044
+ /*transitionToPrimary=*/ true
1047
1045
) ;
1046
+ this . isPrimary = true ;
1047
+ await this . remoteStore . applyPrimaryState ( true ) ;
1048
1048
for ( const targetData of activeQueries ) {
1049
1049
this . remoteStore . listen ( targetData ) ;
1050
1050
}
1051
1051
} else if ( isPrimary === false && this . isPrimary !== false ) {
1052
- this . isPrimary = false ;
1053
-
1054
1052
const activeTargets : TargetId [ ] = [ ] ;
1055
1053
1056
1054
let p = Promise . resolve ( ) ;
@@ -1070,8 +1068,12 @@ export class MultiTabSyncEngine extends SyncEngine
1070
1068
} ) ;
1071
1069
await p ;
1072
1070
1073
- await this . synchronizeQueryViewsAndRaiseSnapshots ( activeTargets ) ;
1071
+ await this . synchronizeQueryViewsAndRaiseSnapshots (
1072
+ activeTargets ,
1073
+ /*transitionToPrimary=*/ false
1074
+ ) ;
1074
1075
this . resetLimboDocuments ( ) ;
1076
+ this . isPrimary = false ;
1075
1077
await this . remoteStore . applyPrimaryState ( false ) ;
1076
1078
}
1077
1079
}
@@ -1091,9 +1093,14 @@ export class MultiTabSyncEngine extends SyncEngine
1091
1093
* Reconcile the query views of the provided query targets with the state from
1092
1094
* persistence. Raises snapshots for any changes that affect the local
1093
1095
* client and returns the updated state of all target's query data.
1096
+ *
1097
+ * @param targets the list of targets with views that need to be recomputed
1098
+ * @param transitionToPrimary `true` iff the tab transitions from a secondary
1099
+ * tab to a primary tab
1094
1100
*/
1095
1101
private async synchronizeQueryViewsAndRaiseSnapshots (
1096
- targets : TargetId [ ]
1102
+ targets : TargetId [ ] ,
1103
+ transitionToPrimary : boolean
1097
1104
) : Promise < TargetData [ ] > {
1098
1105
const activeQueries : TargetData [ ] = [ ] ;
1099
1106
const newViewSnapshots : ViewSnapshot [ ] = [ ] ;
@@ -1127,7 +1134,7 @@ export class MultiTabSyncEngine extends SyncEngine
1127
1134
}
1128
1135
} else {
1129
1136
debugAssert (
1130
- this . isPrimary === true ,
1137
+ transitionToPrimary ,
1131
1138
'A secondary tab should never have an active target without an active query.'
1132
1139
) ;
1133
1140
// For queries that never executed on this client, we need to
0 commit comments