Skip to content

Commit 3960fda

Browse files
Transition to primary only if IndexedDB ops succeed (#3049)
1 parent 8e2fd91 commit 3960fda

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
@@ -1035,9 +1035,6 @@ export class MultiTabSyncEngine extends SyncEngine
10351035

10361036
async applyPrimaryState(isPrimary: boolean): Promise<void> {
10371037
if (isPrimary === true && this.isPrimary !== true) {
1038-
this.isPrimary = true;
1039-
await this.remoteStore.applyPrimaryState(true);
1040-
10411038
// Secondary tabs only maintain Views for their local listeners and the
10421039
// Views internal state may not be 100% populated (in particular
10431040
// secondary tabs don't track syncedDocuments, the set of documents the
@@ -1046,14 +1043,15 @@ export class MultiTabSyncEngine extends SyncEngine
10461043
// match the state on disk.
10471044
const activeTargets = this.sharedClientState.getAllActiveQueryTargets();
10481045
const activeQueries = await this.synchronizeQueryViewsAndRaiseSnapshots(
1049-
activeTargets.toArray()
1046+
activeTargets.toArray(),
1047+
/*transitionToPrimary=*/ true
10501048
);
1049+
this.isPrimary = true;
1050+
await this.remoteStore.applyPrimaryState(true);
10511051
for (const targetData of activeQueries) {
10521052
this.remoteStore.listen(targetData);
10531053
}
10541054
} else if (isPrimary === false && this.isPrimary !== false) {
1055-
this.isPrimary = false;
1056-
10571055
const activeTargets: TargetId[] = [];
10581056

10591057
let p = Promise.resolve();
@@ -1073,8 +1071,12 @@ export class MultiTabSyncEngine extends SyncEngine
10731071
});
10741072
await p;
10751073

1076-
await this.synchronizeQueryViewsAndRaiseSnapshots(activeTargets);
1074+
await this.synchronizeQueryViewsAndRaiseSnapshots(
1075+
activeTargets,
1076+
/*transitionToPrimary=*/ false
1077+
);
10771078
this.resetLimboDocuments();
1079+
this.isPrimary = false;
10781080
await this.remoteStore.applyPrimaryState(false);
10791081
}
10801082
}
@@ -1094,9 +1096,14 @@ export class MultiTabSyncEngine extends SyncEngine
10941096
* Reconcile the query views of the provided query targets with the state from
10951097
* persistence. Raises snapshots for any changes that affect the local
10961098
* 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
10971103
*/
10981104
private async synchronizeQueryViewsAndRaiseSnapshots(
1099-
targets: TargetId[]
1105+
targets: TargetId[],
1106+
transitionToPrimary: boolean
11001107
): Promise<TargetData[]> {
11011108
const activeQueries: TargetData[] = [];
11021109
const newViewSnapshots: ViewSnapshot[] = [];
@@ -1130,7 +1137,7 @@ export class MultiTabSyncEngine extends SyncEngine
11301137
}
11311138
} else {
11321139
debugAssert(
1133-
this.isPrimary === true,
1140+
transitionToPrimary,
11341141
'A secondary tab should never have an active target without an active query.'
11351142
);
11361143
// For queries that never executed on this client, we need to

0 commit comments

Comments
 (0)