Skip to content

Commit 2a2eca4

Browse files
Merge a70479b into 7d5678b
2 parents 7d5678b + a70479b commit 2a2eca4

File tree

6 files changed

+375
-439
lines changed

6 files changed

+375
-439
lines changed

packages/firestore/src/core/component_provider.ts

+26-24
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,11 @@ import {
2727
synchronizeLastDocumentChangeReadTime
2828
} from '../local/local_store';
2929
import {
30-
MultiTabSyncEngine,
31-
newMultiTabSyncEngine,
30+
applyActiveTargetsChange,
31+
applyBatchState,
32+
applyPrimaryState,
33+
applyTargetState,
34+
getActiveClients,
3235
newSyncEngine,
3336
SyncEngine
3437
} from './sync_engine';
@@ -46,9 +49,9 @@ import { OnlineStateSource } from './types';
4649
import { LruParams, LruScheduler } from '../local/lru_garbage_collector';
4750
import { IndexFreeQueryEngine } from '../local/index_free_query_engine';
4851
import {
49-
indexedDbStoragePrefix,
52+
indexedDbClearPersistence,
5053
IndexedDbPersistence,
51-
indexedDbClearPersistence
54+
indexedDbStoragePrefix
5255
} from '../local/indexeddb_persistence';
5356
import {
5457
MemoryEagerDelegate,
@@ -59,6 +62,7 @@ import { newSerializer } from '../platform/serializer';
5962
import { getDocument, getWindow } from '../platform/dom';
6063
import { CredentialsProvider } from '../api/credentials';
6164
import { Connection } from '../remote/connection';
65+
6266
const MEMORY_ONLY_PERSISTENCE_ERROR_MESSAGE =
6367
'You are using the memory-only build of Firestore. Persistence support is ' +
6468
'only available via the @firebase/firestore bundle or the ' +
@@ -200,7 +204,8 @@ export class MemoryComponentProvider implements ComponentProvider {
200204
this.datastore,
201205
this.sharedClientState,
202206
cfg.initialUser,
203-
cfg.maxConcurrentLimboResolutions
207+
cfg.maxConcurrentLimboResolutions,
208+
/* isPrimary= */ true
204209
);
205210
}
206211

@@ -229,17 +234,6 @@ export class IndexedDbComponentProvider extends MemoryComponentProvider {
229234
);
230235
}
231236

232-
createSyncEngine(cfg: ComponentConfiguration): SyncEngine {
233-
return newSyncEngine(
234-
this.localStore,
235-
this.remoteStore,
236-
this.datastore,
237-
this.sharedClientState,
238-
cfg.initialUser,
239-
cfg.maxConcurrentLimboResolutions
240-
);
241-
}
242-
243237
createGarbageCollectionScheduler(
244238
cfg: ComponentConfiguration
245239
): GarbageCollectionScheduler | null {
@@ -296,17 +290,13 @@ export class IndexedDbComponentProvider extends MemoryComponentProvider {
296290
* `synchronizeTabs` will be enabled.
297291
*/
298292
export class MultiTabIndexedDbComponentProvider extends IndexedDbComponentProvider {
299-
syncEngine!: MultiTabSyncEngine;
300-
301293
async initialize(cfg: ComponentConfiguration): Promise<void> {
302294
await super.initialize(cfg);
303295

304296
// NOTE: This will immediately call the listener, so we make sure to
305297
// set it after localStore / remoteStore are started.
306298
await this.persistence.setPrimaryStateListener(async isPrimary => {
307-
await (this.syncEngine as MultiTabSyncEngine).applyPrimaryState(
308-
isPrimary
309-
);
299+
await applyPrimaryState(this.syncEngine, isPrimary);
310300
if (this.gcScheduler) {
311301
if (isPrimary && !this.gcScheduler.started) {
312302
this.gcScheduler.start(this.localStore);
@@ -324,16 +314,28 @@ export class MultiTabIndexedDbComponentProvider extends IndexedDbComponentProvid
324314
}
325315

326316
createSyncEngine(cfg: ComponentConfiguration): SyncEngine {
327-
const syncEngine = newMultiTabSyncEngine(
317+
const startsAsPrimary =
318+
!cfg.persistenceSettings.durable ||
319+
!cfg.persistenceSettings.synchronizeTabs;
320+
const syncEngine = newSyncEngine(
328321
this.localStore,
329322
this.remoteStore,
330323
this.datastore,
331324
this.sharedClientState,
332325
cfg.initialUser,
333-
cfg.maxConcurrentLimboResolutions
326+
cfg.maxConcurrentLimboResolutions,
327+
startsAsPrimary
334328
);
335329
if (this.sharedClientState instanceof WebStorageSharedClientState) {
336-
this.sharedClientState.syncEngine = syncEngine;
330+
this.sharedClientState.syncEngine = {
331+
applyBatchState: applyBatchState.bind(null, syncEngine),
332+
applyTargetState: applyTargetState.bind(null, syncEngine),
333+
applyActiveTargetsChange: applyActiveTargetsChange.bind(
334+
null,
335+
syncEngine
336+
),
337+
getActiveClients: getActiveClients.bind(null, syncEngine)
338+
};
337339
}
338340
return syncEngine;
339341
}

0 commit comments

Comments
 (0)