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