Skip to content

Commit cafe5c4

Browse files
use provider
1 parent 3e2c2e2 commit cafe5c4

21 files changed

+430
-409
lines changed

packages/firestore/src/core/firestore_client.ts

Lines changed: 8 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
import { CredentialsProvider } from '../api/credentials';
1919
import { User } from '../auth/user';
20-
import { IndexFreeQueryEngine } from '../local/index_free_query_engine';
2120
import { LocalStore } from '../local/local_store';
2221
import { MemoryPersistenceProvider } from '../local/memory_persistence';
2322
import {
@@ -172,14 +171,15 @@ export class FirestoreClient {
172171
this.credentials.setChangeListener(user => {
173172
if (!initialized) {
174173
initialized = true;
175-
174+
175+
logDebug(LOG_TAG, 'Initializing. user=', user.uid);
176176
this.initializePersistence(
177177
persistenceProvider,
178178
persistenceSettings,
179179
user,
180180
persistenceResult
181181
)
182-
.then(() => this.initializeRest(persistenceProvider, user))
182+
.then(() => this.initializeRest(persistenceProvider))
183183
.then(initializationDone.resolve, initializationDone.reject);
184184
} else {
185185
this.asyncQueue.enqueueAndForget(() => {
@@ -236,15 +236,16 @@ export class FirestoreClient {
236236
try {
237237
await persistenceProvider.initialize(
238238
this.asyncQueue,
239+
this.remoteStore,
239240
this.databaseInfo,
240241
this.platform,
241242
this.clientId,
242243
user,
244+
MAX_CONCURRENT_LIMBO_RESOLUTIONS,
243245
persistenceSettings
244246
);
245247

246248
this.persistence = persistenceProvider.getPersistence();
247-
this.gcScheduler = persistenceProvider.getGarbageCollectionScheduler();
248249
this.sharedClientState = persistenceProvider.getSharedClientState();
249250
persistenceResult.resolve();
250251
} catch (error) {
@@ -325,20 +326,12 @@ export class FirestoreClient {
325326
* implementation is available in this.persistence.
326327
*/
327328
private initializeRest(
328-
persistenceProvider: PersistenceProvider,
329-
user: User
329+
persistenceProvider: PersistenceProvider
330330
): Promise<void> {
331-
logDebug(LOG_TAG, 'Initializing. user=', user.uid);
332331
return this.platform
333332
.loadConnection(this.databaseInfo)
334333
.then(async connection => {
335-
const queryEngine = new IndexFreeQueryEngine();
336-
this.localStore = persistenceProvider.newLocalStore(
337-
this.persistence,
338-
queryEngine,
339-
user
340-
);
341-
await this.localStore.start();
334+
this.localStore = persistenceProvider.getLocalStore();
342335
const connectivityMonitor = this.platform.newConnectivityMonitor();
343336
const serializer = this.platform.newSerializer(
344337
this.databaseInfo.databaseId
@@ -357,13 +350,6 @@ export class FirestoreClient {
357350
onlineState,
358351
OnlineStateSource.RemoteStore
359352
);
360-
const sharedClientStateOnlineStateChangedHandler = (
361-
onlineState: OnlineState
362-
): void =>
363-
this.syncEngine.applyOnlineStateChange(
364-
onlineState,
365-
OnlineStateSource.SharedClientState
366-
);
367353

368354
this.remoteStore = new RemoteStore(
369355
this.localStore,
@@ -373,19 +359,7 @@ export class FirestoreClient {
373359
connectivityMonitor
374360
);
375361

376-
this.syncEngine = await persistenceProvider.newSyncEngine(
377-
this.localStore,
378-
this.remoteStore,
379-
this.sharedClientState,
380-
user,
381-
MAX_CONCURRENT_LIMBO_RESOLUTIONS
382-
);
383-
384-
this.sharedClientState.onlineStateHandler = sharedClientStateOnlineStateChangedHandler;
385-
386-
// Set up wiring between sync engine and other components
387-
this.remoteStore.syncEngine = this.syncEngine;
388-
//this.sharedClientState.syncEngine = this.syncEngine;
362+
this.syncEngine = await persistenceProvider.getSyncEngine();
389363

390364
this.eventMgr = new EventManager(this.syncEngine);
391365

packages/firestore/src/core/sync_engine.ts

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -875,18 +875,12 @@ export class SyncEngine implements RemoteSyncer {
875875

876876
await this.remoteStore.handleCredentialChange();
877877
}
878-
879-
// PORTING NOTE: Multi-tab only. In other clients, LocalStore is unaware of
880-
// the online state.
878+
881879
enableNetwork(): Promise<void> {
882-
this.localStore.setNetworkEnabled(true);
883880
return this.remoteStore.enableNetwork();
884881
}
885-
886-
// PORTING NOTE: Multi-tab only. In other clients, LocalStore is unaware of
887-
// the online state.
882+
888883
disableNetwork(): Promise<void> {
889-
this.localStore.setNetworkEnabled(false);
890884
return this.remoteStore.disableNetwork();
891885
}
892886

@@ -938,6 +932,21 @@ export class MultiTabSyncEngine extends SyncEngine
938932
return this.isPrimary === true;
939933
}
940934

935+
enableNetwork(): Promise<void> {
936+
// PORTING NOTE: Multi-tab only. In other clients, LocalStore is unaware of
937+
// the online state.
938+
this.localStore.setNetworkEnabled(true);
939+
return super.enableNetwork();
940+
}
941+
942+
943+
disableNetwork(): Promise<void> {
944+
// PORTING NOTE: Multi-tab only. In other clients, LocalStore is unaware of
945+
// the online state.
946+
this.localStore.setNetworkEnabled(false);
947+
return super.disableNetwork();
948+
}
949+
941950
/**
942951
* Reconcile the list of synced documents in an existing view with those
943952
* from persistence.

packages/firestore/src/local/indexeddb_mutation_queue.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,14 @@ export class IndexedDbMutationQueue implements MutationQueue {
230230
return null;
231231
});
232232
}
233-
233+
234+
/**
235+
* Returns the document keys for the mutation batch with the given batchId.
236+
* For primary clients, this method returns `null` after
237+
* `removeMutationBatches()` has been called. Secondary clients return a
238+
* cached result until `removeCachedMutationKeys()` is invoked.
239+
*/
240+
// PORTING NOTE: Multi-tab only.
234241
lookupMutationKeys(
235242
transaction: PersistenceTransaction,
236243
batchId: BatchId
@@ -518,6 +525,15 @@ export class IndexedDbMutationQueue implements MutationQueue {
518525
});
519526
}
520527

528+
/**
529+
* Clears the cached keys for a mutation batch. This method should be
530+
* called by secondary clients after they process mutation updates.
531+
*
532+
* Note that this method does not have to be called from primary clients as
533+
* the corresponding cache entries are cleared when an acknowledged or
534+
* rejected batch is removed from the mutation queue.
535+
*/
536+
// PORTING NOTE: Multi-tab only
521537
removeCachedMutationKeys(batchId: BatchId): void {
522538
delete this.documentKeysByBatchId[batchId];
523539
}

0 commit comments

Comments
 (0)