Skip to content

Commit 8e331c5

Browse files
Merge e80e804 into a57dac5
2 parents a57dac5 + e80e804 commit 8e331c5

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

packages/firestore/src/core/sync_engine.ts

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1032,9 +1032,6 @@ export class MultiTabSyncEngine extends SyncEngine
10321032

10331033
async applyPrimaryState(isPrimary: boolean): Promise<void> {
10341034
if (isPrimary === true && this.isPrimary !== true) {
1035-
this.isPrimary = true;
1036-
await this.remoteStore.applyPrimaryState(true);
1037-
10381035
// Secondary tabs only maintain Views for their local listeners and the
10391036
// Views internal state may not be 100% populated (in particular
10401037
// secondary tabs don't track syncedDocuments, the set of documents the
@@ -1043,14 +1040,15 @@ export class MultiTabSyncEngine extends SyncEngine
10431040
// match the state on disk.
10441041
const activeTargets = this.sharedClientState.getAllActiveQueryTargets();
10451042
const activeQueries = await this.synchronizeQueryViewsAndRaiseSnapshots(
1046-
activeTargets.toArray()
1043+
activeTargets.toArray(),
1044+
/*transitionToPrimary=*/ true
10471045
);
1046+
this.isPrimary = true;
1047+
await this.remoteStore.applyPrimaryState(true);
10481048
for (const targetData of activeQueries) {
10491049
this.remoteStore.listen(targetData);
10501050
}
10511051
} else if (isPrimary === false && this.isPrimary !== false) {
1052-
this.isPrimary = false;
1053-
10541052
const activeTargets: TargetId[] = [];
10551053

10561054
let p = Promise.resolve();
@@ -1070,8 +1068,12 @@ export class MultiTabSyncEngine extends SyncEngine
10701068
});
10711069
await p;
10721070

1073-
await this.synchronizeQueryViewsAndRaiseSnapshots(activeTargets);
1071+
await this.synchronizeQueryViewsAndRaiseSnapshots(
1072+
activeTargets,
1073+
/*transitionToPrimary=*/ false
1074+
);
10741075
this.resetLimboDocuments();
1076+
this.isPrimary = false;
10751077
await this.remoteStore.applyPrimaryState(false);
10761078
}
10771079
}
@@ -1091,9 +1093,14 @@ export class MultiTabSyncEngine extends SyncEngine
10911093
* Reconcile the query views of the provided query targets with the state from
10921094
* persistence. Raises snapshots for any changes that affect the local
10931095
* 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
10941100
*/
10951101
private async synchronizeQueryViewsAndRaiseSnapshots(
1096-
targets: TargetId[]
1102+
targets: TargetId[],
1103+
transitionToPrimary: boolean
10971104
): Promise<TargetData[]> {
10981105
const activeQueries: TargetData[] = [];
10991106
const newViewSnapshots: ViewSnapshot[] = [];
@@ -1127,7 +1134,7 @@ export class MultiTabSyncEngine extends SyncEngine
11271134
}
11281135
} else {
11291136
debugAssert(
1130-
this.isPrimary === true,
1137+
transitionToPrimary,
11311138
'A secondary tab should never have an active target without an active query.'
11321139
);
11331140
// For queries that never executed on this client, we need to

0 commit comments

Comments
 (0)