Skip to content

Commit fdf4084

Browse files
authored
Merge b103153 into 49c7903
2 parents 49c7903 + b103153 commit fdf4084

File tree

2 files changed

+34
-23
lines changed

2 files changed

+34
-23
lines changed

.changeset/good-rice-work.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
---
2+
---

packages/firestore/src/core/component_provider.ts

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -237,19 +237,18 @@ export class IndexedDbOfflineComponentProvider extends MemoryOfflineComponentPro
237237
// NOTE: This will immediately call the listener, so we make sure to
238238
// set it after localStore / remoteStore are started.
239239
await this.persistence.setPrimaryStateListener(() => {
240-
if (this.gcScheduler && !this.gcScheduler.started) {
241-
this.gcScheduler.start();
242-
}
243-
if (
244-
this.indexBackfillerScheduler &&
245-
!this.indexBackfillerScheduler.started
246-
) {
247-
this.indexBackfillerScheduler.start();
248-
}
240+
this.startScheduler(this.gcScheduler);
241+
this.startScheduler(this.indexBackfillerScheduler);
249242
return Promise.resolve();
250243
});
251244
}
252245

246+
private startScheduler(scheduler: Scheduler | null): void {
247+
if (scheduler && !scheduler.started) {
248+
scheduler.start();
249+
}
250+
}
251+
253252
createLocalStore(cfg: ComponentConfiguration): LocalStore {
254253
return newLocalStore(
255254
this.persistence,
@@ -350,23 +349,33 @@ export class MultiTabOfflineComponentProvider extends IndexedDbOfflineComponentP
350349
this.onlineComponentProvider.syncEngine,
351350
isPrimary
352351
);
353-
if (this.gcScheduler) {
354-
if (isPrimary && !this.gcScheduler.started) {
355-
this.gcScheduler.start();
356-
} else if (!isPrimary) {
357-
this.gcScheduler.stop();
358-
}
359-
}
360-
if (this.indexBackfillerScheduler) {
361-
if (isPrimary && !this.indexBackfillerScheduler.started) {
362-
this.indexBackfillerScheduler.start();
363-
} else if (!isPrimary) {
364-
this.indexBackfillerScheduler.stop();
365-
}
366-
}
352+
this.startOrStopScheduler(this.gcScheduler, isPrimary);
353+
this.startOrStopScheduler(this.indexBackfillerScheduler, isPrimary);
367354
});
368355
}
369356

357+
/**
358+
* Starts or stops a scheduler, taking into account its nullness and whether
359+
* the SDK is acting as the "primary" tab.
360+
* @param scheduler The scheduler to start or stop; if null, then this
361+
* method does nothing.
362+
* @param isPrimary true if the current tab is the primary tab, and false if
363+
* it is a secondary tab.
364+
*/
365+
private startOrStopScheduler(
366+
scheduler: Scheduler | null,
367+
isPrimary: boolean
368+
): void {
369+
if (!scheduler) {
370+
return;
371+
}
372+
if (isPrimary && !scheduler.started) {
373+
scheduler.start();
374+
} else if (!isPrimary) {
375+
scheduler.stop();
376+
}
377+
}
378+
370379
createSharedClientState(cfg: ComponentConfiguration): SharedClientState {
371380
const window = getWindow();
372381
if (!WebStorageSharedClientState.isAvailable(window)) {

0 commit comments

Comments
 (0)