Skip to content

Commit acd526d

Browse files
SyncEngine
1 parent 7870925 commit acd526d

File tree

8 files changed

+536
-475
lines changed

8 files changed

+536
-475
lines changed

packages/firestore/exp/test/deps/dependencies.json

Lines changed: 165 additions & 55 deletions
Large diffs are not rendered by default.

packages/firestore/src/core/component_provider.ts

Lines changed: 23 additions & 25 deletions
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';
@@ -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+
import { NoOpSharedClientStateSyncer } from '../../test/unit/local/persistence_test_helpers';
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 ' +
@@ -194,14 +198,18 @@ export class MemoryComponentProvider implements ComponentProvider {
194198
}
195199

196200
createSyncEngine(cfg: ComponentConfiguration): SyncEngine {
197-
return newSyncEngine(
201+
const syncEngine = newSyncEngine(
198202
this.localStore,
199203
this.remoteStore,
200204
this.datastore,
201205
this.sharedClientState,
202206
cfg.initialUser,
203-
cfg.maxConcurrentLimboResolutions
207+
cfg.maxConcurrentLimboResolutions,
208+
/* isPrimary= */ true
204209
);
210+
// TODO(mrschmidt) FIX ME
211+
this.sharedClientState.syncEngine = new NoOpSharedClientStateSyncer([]);
212+
return syncEngine;
205213
}
206214

207215
clearPersistence(
@@ -229,17 +237,6 @@ export class IndexedDbComponentProvider extends MemoryComponentProvider {
229237
);
230238
}
231239

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-
243240
createGarbageCollectionScheduler(
244241
cfg: ComponentConfiguration
245242
): GarbageCollectionScheduler | null {
@@ -296,17 +293,13 @@ export class IndexedDbComponentProvider extends MemoryComponentProvider {
296293
* `synchronizeTabs` will be enabled.
297294
*/
298295
export class MultiTabIndexedDbComponentProvider extends IndexedDbComponentProvider {
299-
syncEngine!: MultiTabSyncEngine;
300-
301296
async initialize(cfg: ComponentConfiguration): Promise<void> {
302297
await super.initialize(cfg);
303298

304299
// NOTE: This will immediately call the listener, so we make sure to
305300
// set it after localStore / remoteStore are started.
306301
await this.persistence.setPrimaryStateListener(async isPrimary => {
307-
await (this.syncEngine as MultiTabSyncEngine).applyPrimaryState(
308-
isPrimary
309-
);
302+
await applyPrimaryState(this.syncEngine, isPrimary);
310303
if (this.gcScheduler) {
311304
if (isPrimary && !this.gcScheduler.started) {
312305
this.gcScheduler.start(this.localStore);
@@ -320,17 +313,22 @@ export class MultiTabIndexedDbComponentProvider extends IndexedDbComponentProvid
320313
}
321314

322315
createSyncEngine(cfg: ComponentConfiguration): SyncEngine {
323-
const syncEngine = newMultiTabSyncEngine(
316+
const syncEngine = newSyncEngine(
324317
this.localStore,
325318
this.remoteStore,
326319
this.datastore,
327320
this.sharedClientState,
328321
cfg.initialUser,
329-
cfg.maxConcurrentLimboResolutions
322+
cfg.maxConcurrentLimboResolutions,
323+
/* isPrimary= */ !cfg.persistenceSettings.durable ||
324+
!cfg.persistenceSettings.synchronizeTabs
330325
);
331-
if (this.sharedClientState instanceof WebStorageSharedClientState) {
332-
this.sharedClientState.syncEngine = syncEngine;
333-
}
326+
this.sharedClientState.syncEngine = {
327+
applyBatchState: applyBatchState.bind(null, syncEngine),
328+
applyTargetState: applyTargetState.bind(null, syncEngine),
329+
applyActiveTargetsChange: applyActiveTargetsChange.bind(null, syncEngine),
330+
getActiveClients: getActiveClients.bind(null, syncEngine)
331+
};
334332
return syncEngine;
335333
}
336334

packages/firestore/src/core/firestore_client.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -201,9 +201,9 @@ export class FirestoreClient {
201201
/** Enables the network connection and requeues all pending operations. */
202202
enableNetwork(): Promise<void> {
203203
this.verifyNotTerminated();
204-
return this.asyncQueue.enqueue(async () => {
205-
await this.remoteStore.enableNetwork();
206-
await this.persistence.setNetworkEnabled(true);
204+
return this.asyncQueue.enqueue(() => {
205+
this.persistence.setNetworkEnabled(true);
206+
return this.remoteStore.enableNetwork();
207207
});
208208
}
209209

@@ -334,9 +334,9 @@ export class FirestoreClient {
334334
/** Disables the network connection. Pending operations will not complete. */
335335
disableNetwork(): Promise<void> {
336336
this.verifyNotTerminated();
337-
return this.asyncQueue.enqueue(async () => {
338-
await this.persistence.setNetworkEnabled(true);
339-
await this.remoteStore.enableNetwork();
337+
return this.asyncQueue.enqueue(() => {
338+
this.persistence.setNetworkEnabled(true);
339+
return this.remoteStore.disableNetwork();
340340
});
341341
}
342342

0 commit comments

Comments
 (0)