Skip to content

Commit 7835818

Browse files
Refactor applyOnlineStateChange
1 parent d9b0725 commit 7835818

File tree

2 files changed

+38
-28
lines changed

2 files changed

+38
-28
lines changed

packages/firestore/src/core/component_provider.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ export interface ComponentProvider {
8282
* Provides all components needed for Firestore with in-memory persistence.
8383
* Uses EagerGC garbage collection.
8484
*/
85-
export class MemoryComponentProvider {
85+
export class MemoryComponentProvider implements ComponentProvider {
8686
persistence!: Persistence;
8787
sharedClientState!: SharedClientState;
8888
localStore!: LocalStore;

packages/firestore/src/core/sync_engine.ts

Lines changed: 37 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -442,34 +442,21 @@ export class SyncEngine implements RemoteSyncer {
442442
onlineState: OnlineState,
443443
source: OnlineStateSource
444444
): void {
445-
// If we are the secondary client, we explicitly ignore the remote store's
446-
// online state (the local client may go offline, even though the primary
447-
// tab remains online) and only apply the primary tab's online state from
448-
// SharedClientState.
449-
if (
450-
(this.isPrimaryClient && source === OnlineStateSource.RemoteStore) ||
451-
(!this.isPrimaryClient && source === OnlineStateSource.SharedClientState)
452-
) {
453-
this.assertSubscribed('applyOnlineStateChange()');
454-
const newViewSnapshots = [] as ViewSnapshot[];
455-
this.queryViewsByQuery.forEach((query, queryView) => {
456-
const viewChange = queryView.view.applyOnlineStateChange(onlineState);
457-
debugAssert(
458-
viewChange.limboChanges.length === 0,
459-
'OnlineState should not affect limbo documents.'
460-
);
461-
if (viewChange.snapshot) {
462-
newViewSnapshots.push(viewChange.snapshot);
463-
}
464-
});
465-
this.syncEngineListener!.onOnlineStateChange(onlineState);
466-
this.syncEngineListener!.onWatchChange(newViewSnapshots);
467-
468-
this.onlineState = onlineState;
469-
if (this.isPrimaryClient) {
470-
this.sharedClientState.setOnlineState(onlineState);
445+
this.assertSubscribed('applyOnlineStateChange()');
446+
const newViewSnapshots = [] as ViewSnapshot[];
447+
this.queryViewsByQuery.forEach((query, queryView) => {
448+
const viewChange = queryView.view.applyOnlineStateChange(onlineState);
449+
debugAssert(
450+
viewChange.limboChanges.length === 0,
451+
'OnlineState should not affect limbo documents.'
452+
);
453+
if (viewChange.snapshot) {
454+
newViewSnapshots.push(viewChange.snapshot);
471455
}
472-
}
456+
});
457+
this.syncEngineListener!.onOnlineStateChange(onlineState);
458+
this.syncEngineListener!.onWatchChange(newViewSnapshots);
459+
this.onlineState = onlineState;
473460
}
474461

475462
async rejectListen(targetId: TargetId, err: FirestoreError): Promise<void> {
@@ -968,6 +955,29 @@ export class MultiTabSyncEngine extends SyncEngine
968955
return viewSnapshot;
969956
}
970957

958+
applyOnlineStateChange(
959+
onlineState: OnlineState,
960+
source: OnlineStateSource
961+
): void {
962+
// If we are the primary client, the online state of all clients only
963+
// depends on the online state of the local RemoteStore.
964+
if (this.isPrimaryClient && source === OnlineStateSource.RemoteStore) {
965+
super.applyOnlineStateChange(onlineState, source);
966+
this.sharedClientState.setOnlineState(onlineState);
967+
}
968+
969+
// If we are the secondary client, we explicitly ignore the remote store's
970+
// online state (the local client may go offline, even though the primary
971+
// tab remains online) and only apply the primary tab's online state from
972+
// SharedClientState.
973+
if (
974+
!this.isPrimaryClient &&
975+
source === OnlineStateSource.SharedClientState
976+
) {
977+
super.applyOnlineStateChange(onlineState, source);
978+
}
979+
}
980+
971981
async applyBatchState(
972982
batchId: BatchId,
973983
batchState: MutationBatchState,

0 commit comments

Comments
 (0)