Skip to content

Firestore: Deduplicate some repeated logic in OfflineComponentProvider #7952

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
2 changes: 2 additions & 0 deletions .changeset/good-rice-work.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
---
55 changes: 32 additions & 23 deletions packages/firestore/src/core/component_provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,19 +237,18 @@ export class IndexedDbOfflineComponentProvider extends MemoryOfflineComponentPro
// NOTE: This will immediately call the listener, so we make sure to
// set it after localStore / remoteStore are started.
await this.persistence.setPrimaryStateListener(() => {
if (this.gcScheduler && !this.gcScheduler.started) {
this.gcScheduler.start();
}
if (
this.indexBackfillerScheduler &&
!this.indexBackfillerScheduler.started
) {
this.indexBackfillerScheduler.start();
}
this.startScheduler(this.gcScheduler);
this.startScheduler(this.indexBackfillerScheduler);
return Promise.resolve();
});
}

private startScheduler(scheduler: Scheduler | null): void {
if (scheduler && !scheduler.started) {
scheduler.start();
}
}

createLocalStore(cfg: ComponentConfiguration): LocalStore {
return newLocalStore(
this.persistence,
Expand Down Expand Up @@ -350,23 +349,33 @@ export class MultiTabOfflineComponentProvider extends IndexedDbOfflineComponentP
this.onlineComponentProvider.syncEngine,
isPrimary
);
if (this.gcScheduler) {
if (isPrimary && !this.gcScheduler.started) {
this.gcScheduler.start();
} else if (!isPrimary) {
this.gcScheduler.stop();
}
}
if (this.indexBackfillerScheduler) {
if (isPrimary && !this.indexBackfillerScheduler.started) {
this.indexBackfillerScheduler.start();
} else if (!isPrimary) {
this.indexBackfillerScheduler.stop();
}
}
this.startOrStopScheduler(this.gcScheduler, isPrimary);
this.startOrStopScheduler(this.indexBackfillerScheduler, isPrimary);
});
}

/**
* Starts or stops a scheduler, taking into account its nullness and whether
* the SDK is acting as the "primary" tab.
* @param scheduler The scheduler to start or stop; if null, then this
* method does nothing.
* @param isPrimary true if the current tab is the primary tab, and false if
* it is a secondary tab.
*/
private startOrStopScheduler(
scheduler: Scheduler | null,
isPrimary: boolean
): void {
if (!scheduler) {
return;
}
if (isPrimary && !scheduler.started) {
scheduler.start();
} else if (!isPrimary) {
scheduler.stop();
}
}

createSharedClientState(cfg: ComponentConfiguration): SharedClientState {
const window = getWindow();
if (!WebStorageSharedClientState.isAvailable(window)) {
Expand Down