Skip to content

Commit fc8d5df

Browse files
WIP
1 parent cafe5c4 commit fc8d5df

19 files changed

+369
-368
lines changed

packages/firestore/src/core/firestore_client.ts

Lines changed: 34 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ import { AutoId } from '../util/misc';
4848
import { DatabaseId, DatabaseInfo } from './database_info';
4949
import { Query } from './query';
5050
import { Transaction } from './transaction';
51-
import { OnlineState, OnlineStateSource } from './types';
5251
import { ViewSnapshot } from './view_snapshot';
52+
import { ConnectivityMonitor } from '../remote/connectivity_monitor';
5353

5454
const LOG_TAG = 'FirestoreClient';
5555
const MAX_CONCURRENT_LIMBO_RESOLUTIONS = 100;
@@ -171,14 +171,32 @@ export class FirestoreClient {
171171
this.credentials.setChangeListener(user => {
172172
if (!initialized) {
173173
initialized = true;
174-
174+
175175
logDebug(LOG_TAG, 'Initializing. user=', user.uid);
176-
this.initializePersistence(
177-
persistenceProvider,
178-
persistenceSettings,
179-
user,
180-
persistenceResult
181-
)
176+
177+
this.platform
178+
.loadConnection(this.databaseInfo)
179+
.then(connection => {
180+
const connectivityMonitor = this.platform.newConnectivityMonitor();
181+
const serializer = this.platform.newSerializer(
182+
this.databaseInfo.databaseId
183+
);
184+
const datastore = new Datastore(
185+
this.asyncQueue,
186+
connection,
187+
this.credentials,
188+
serializer
189+
);
190+
191+
return this.initializePersistence(
192+
datastore,
193+
connectivityMonitor,
194+
persistenceProvider,
195+
persistenceSettings,
196+
user,
197+
persistenceResult
198+
);
199+
})
182200
.then(() => this.initializeRest(persistenceProvider))
183201
.then(initializationDone.resolve, initializationDone.reject);
184202
} else {
@@ -228,6 +246,8 @@ export class FirestoreClient {
228246
* succeeded.
229247
*/
230248
private async initializePersistence(
249+
datastore: Datastore,
250+
connectivityMonitor: ConnectivityMonitor,
231251
persistenceProvider: PersistenceProvider,
232252
persistenceSettings: PersistenceSettings,
233253
user: User,
@@ -236,9 +256,10 @@ export class FirestoreClient {
236256
try {
237257
await persistenceProvider.initialize(
238258
this.asyncQueue,
239-
this.remoteStore,
240259
this.databaseInfo,
241260
this.platform,
261+
datastore,
262+
connectivityMonitor,
242263
this.clientId,
243264
user,
244265
MAX_CONCURRENT_LIMBO_RESOLUTIONS,
@@ -263,6 +284,8 @@ export class FirestoreClient {
263284
error
264285
);
265286
return this.initializePersistence(
287+
datastore,
288+
connectivityMonitor,
266289
new MemoryPersistenceProvider(),
267290
{ durable: false },
268291
user,
@@ -332,41 +355,10 @@ export class FirestoreClient {
332355
.loadConnection(this.databaseInfo)
333356
.then(async connection => {
334357
this.localStore = persistenceProvider.getLocalStore();
335-
const connectivityMonitor = this.platform.newConnectivityMonitor();
336-
const serializer = this.platform.newSerializer(
337-
this.databaseInfo.databaseId
338-
);
339-
const datastore = new Datastore(
340-
this.asyncQueue,
341-
connection,
342-
this.credentials,
343-
serializer
344-
);
345-
346-
const remoteStoreOnlineStateChangedHandler = (
347-
onlineState: OnlineState
348-
): void =>
349-
this.syncEngine.applyOnlineStateChange(
350-
onlineState,
351-
OnlineStateSource.RemoteStore
352-
);
353-
354-
this.remoteStore = new RemoteStore(
355-
this.localStore,
356-
datastore,
357-
this.asyncQueue,
358-
remoteStoreOnlineStateChangedHandler,
359-
connectivityMonitor
360-
);
361-
362-
this.syncEngine = await persistenceProvider.getSyncEngine();
363-
358+
this.remoteStore = persistenceProvider.getRemoteStore();
359+
this.syncEngine = persistenceProvider.getSyncEngine();
364360
this.eventMgr = new EventManager(this.syncEngine);
365361

366-
// PORTING NOTE: LocalStore doesn't need an explicit start() on the Web.
367-
await this.sharedClientState.start();
368-
await this.remoteStore.start();
369-
370362
// When a user calls clearPersistence() in one client, all other clients
371363
// need to be terminated to allow the delete to succeed.
372364
await this.persistence.setDatabaseDeletedListener(async () => {

packages/firestore/src/core/sync_engine.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -875,11 +875,11 @@ export class SyncEngine implements RemoteSyncer {
875875

876876
await this.remoteStore.handleCredentialChange();
877877
}
878-
878+
879879
enableNetwork(): Promise<void> {
880880
return this.remoteStore.enableNetwork();
881881
}
882-
882+
883883
disableNetwork(): Promise<void> {
884884
return this.remoteStore.disableNetwork();
885885
}
@@ -939,7 +939,6 @@ export class MultiTabSyncEngine extends SyncEngine
939939
return super.enableNetwork();
940940
}
941941

942-
943942
disableNetwork(): Promise<void> {
944943
// PORTING NOTE: Multi-tab only. In other clients, LocalStore is unaware of
945944
// the online state.

packages/firestore/src/local/indexeddb_mutation_queue.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ export class IndexedDbMutationQueue implements MutationQueue {
230230
return null;
231231
});
232232
}
233-
233+
234234
/**
235235
* Returns the document keys for the mutation batch with the given batchId.
236236
* For primary clients, this method returns `null` after

packages/firestore/src/local/indexeddb_persistence.ts

Lines changed: 42 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ import {
6868
LruParams,
6969
LruScheduler
7070
} from './lru_garbage_collector';
71-
import { MutationQueue } from './mutation_queue';
7271
import {
7372
GarbageCollectionScheduler,
7473
Persistence,
@@ -92,8 +91,9 @@ import { SimpleDb, SimpleDbStore, SimpleDbTransaction } from './simple_db';
9291
import { LocalStore, MultiTabLocalStore } from './local_store';
9392
import { RemoteStore } from '../remote/remote_store';
9493
import { MultiTabSyncEngine, SyncEngine } from '../core/sync_engine';
95-
import { QueryEngine } from './query_engine';
96-
import {IndexFreeQueryEngine} from "./index_free_query_engine";
94+
import { IndexFreeQueryEngine } from './index_free_query_engine';
95+
import { Datastore } from '../remote/datastore';
96+
import { ConnectivityMonitor } from '../remote/connectivity_monitor';
9797

9898
const LOG_TAG = 'IndexedDbPersistence';
9999

@@ -357,7 +357,7 @@ export class IndexedDbPersistence implements Persistence {
357357
return Promise.reject(reason);
358358
});
359359
}
360-
360+
361361
/**
362362
* Registers a listener that gets called when the primary state of the
363363
* instance changes. Upon registering, this listener is invoked immediately
@@ -719,7 +719,7 @@ export class IndexedDbPersistence implements Persistence {
719719
!this.isClientZombied(client.clientId)
720720
);
721721
}
722-
722+
723723
/**
724724
* Returns the IDs of the clients that are currently active. If multi-tab
725725
* is not supported, returns an array that only contains the local client's
@@ -1346,12 +1346,14 @@ export class IndexedDbPersistenceProvider implements PersistenceProvider {
13461346
private localStore!: MultiTabLocalStore;
13471347
private syncEngine!: MultiTabSyncEngine;
13481348
private gcScheduler!: GarbageCollectionScheduler;
1349+
private remoteStore!: RemoteStore;
13491350

13501351
async initialize(
13511352
asyncQueue: AsyncQueue,
1352-
remoteStore: RemoteStore,
13531353
databaseInfo: DatabaseInfo,
13541354
platform: Platform,
1355+
datastore: Datastore,
1356+
connectivityMonitor: ConnectivityMonitor,
13551357
clientId: ClientId,
13561358
initialUser: User,
13571359
maxConcurrentLimboResolutions: number,
@@ -1404,18 +1406,40 @@ export class IndexedDbPersistenceProvider implements PersistenceProvider {
14041406
.garbageCollector;
14051407
this.gcScheduler = new LruScheduler(garbageCollector, asyncQueue);
14061408

1407-
1408-
this.localStore = new MultiTabLocalStore(this.persistence, new IndexFreeQueryEngine(), initialUser);
1409+
this.localStore = new MultiTabLocalStore(
1410+
this.persistence,
1411+
new IndexFreeQueryEngine(),
1412+
initialUser
1413+
);
14091414
await this.localStore.start();
14101415

1416+
const remoteStoreOnlineStateChangedHandler = (
1417+
onlineState: OnlineState
1418+
): void =>
1419+
this.syncEngine.applyOnlineStateChange(
1420+
onlineState,
1421+
OnlineStateSource.RemoteStore
1422+
);
1423+
1424+
this.remoteStore = new RemoteStore(
1425+
this.localStore,
1426+
datastore,
1427+
asyncQueue,
1428+
remoteStoreOnlineStateChangedHandler,
1429+
connectivityMonitor
1430+
);
1431+
14111432
this.syncEngine = new MultiTabSyncEngine(
14121433
this.localStore,
1413-
remoteStore,
1434+
this.remoteStore,
14141435
this.sharedClientState,
14151436
initialUser,
14161437
maxConcurrentLimboResolutions
14171438
);
14181439

1440+
// Set up wiring between sync engine and other components
1441+
this.remoteStore.syncEngine = this.syncEngine;
1442+
14191443
// NOTE: This will immediately call the listener, so we make sure to
14201444
// set it after localStore / remoteStore are started.
14211445
await this.persistence!.setPrimaryStateListener(async isPrimary => {
@@ -1435,15 +1459,12 @@ export class IndexedDbPersistenceProvider implements PersistenceProvider {
14351459
OnlineStateSource.SharedClientState
14361460
);
14371461

1438-
14391462
this.sharedClientState.syncEngine = this.syncEngine;
1440-
this.sharedClientState.start();
1463+
await this.sharedClientState.start();
14411464

1442-
// Set up wiring between sync engine and other components
1443-
remoteStore.syncEngine = this.syncEngine;
1444-
1445-
await this.localStore.start();
1465+
await this.remoteStore.start();
14461466

1467+
await this.localStore.start();
14471468
}
14481469

14491470
getPersistence(): Persistence {
@@ -1455,7 +1476,7 @@ this.sharedClientState.start();
14551476
assert(!!this.sharedClientState, 'initialize() not called');
14561477
return this.sharedClientState;
14571478
}
1458-
1479+
14591480
getGarbageCollectionScheduler(): GarbageCollectionScheduler {
14601481
assert(!!this.gcScheduler, 'initialize() not called');
14611482
return this.gcScheduler;
@@ -1466,6 +1487,11 @@ this.sharedClientState.start();
14661487
return this.localStore;
14671488
}
14681489

1490+
getRemoteStore(): RemoteStore {
1491+
assert(!!this.remoteStore, 'initialize() not called');
1492+
return this.remoteStore;
1493+
}
1494+
14691495
getSyncEngine(): SyncEngine {
14701496
assert(!!this.syncEngine, 'initialize() not called');
14711497
return this.syncEngine;

packages/firestore/src/local/indexeddb_remote_document_cache.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ export class IndexedDbRemoteDocumentCache implements RemoteDocumentCache {
290290
})
291291
.next(() => results);
292292
}
293-
293+
294294
/**
295295
* Returns the set of documents that have changed since the specified read
296296
* time.
@@ -327,7 +327,7 @@ export class IndexedDbRemoteDocumentCache implements RemoteDocumentCache {
327327
};
328328
});
329329
}
330-
330+
331331
/**
332332
* Returns the read time of the most recently read document in the cache, or
333333
* SnapshotVersion.MIN if not available.

packages/firestore/src/local/indexeddb_target_cache.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ export class IndexedDbTargetCache implements TargetCache {
370370
)
371371
.next(() => count > 0);
372372
}
373-
373+
374374
/**
375375
* Looks up a TargetData entry by target ID.
376376
*

packages/firestore/src/local/local_store.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,10 @@ import { RemoteDocumentChangeBuffer } from './remote_document_change_buffer';
6262
import { ClientId } from './shared_client_state';
6363
import { TargetData, TargetPurpose } from './target_data';
6464
import { ByteString } from '../util/byte_string';
65-
import {IndexedDbPersistence} from "./indexeddb_persistence";
66-
import {IndexedDbMutationQueue} from "./indexeddb_mutation_queue";
67-
import {IndexedDbRemoteDocumentCache} from "./indexeddb_remote_document_cache";
68-
import {IndexedDbTargetCache} from "./indexeddb_target_cache";
65+
import { IndexedDbPersistence } from './indexeddb_persistence';
66+
import { IndexedDbMutationQueue } from './indexeddb_mutation_queue';
67+
import { IndexedDbRemoteDocumentCache } from './indexeddb_remote_document_cache';
68+
import { IndexedDbTargetCache } from './indexeddb_target_cache';
6969

7070
const LOG_TAG = 'LocalStore';
7171

@@ -218,7 +218,7 @@ export class LocalStore {
218218
);
219219
this.queryEngine.setLocalDocumentsView(this.localDocuments);
220220
}
221-
221+
222222
/**
223223
* Tells the LocalStore that the currently authenticated user has changed.
224224
*
@@ -1009,7 +1009,7 @@ export class MultiTabLocalStore extends LocalStore {
10091009
protected mutationQueue: IndexedDbMutationQueue;
10101010
protected remoteDocuments: IndexedDbRemoteDocumentCache;
10111011
protected targetCache: IndexedDbTargetCache;
1012-
1012+
10131013
constructor(
10141014
protected persistence: IndexedDbPersistence,
10151015
queryEngine: QueryEngine,
@@ -1021,7 +1021,7 @@ export class MultiTabLocalStore extends LocalStore {
10211021
this.remoteDocuments = persistence.getRemoteDocumentCache();
10221022
this.targetCache = persistence.getTargetCache();
10231023
}
1024-
1024+
10251025
/** Starts the LocalStore. */
10261026
start(): Promise<void> {
10271027
return this.synchronizeLastDocumentChangeReadTime();
@@ -1048,7 +1048,7 @@ export class MultiTabLocalStore extends LocalStore {
10481048
}
10491049
);
10501050
}
1051-
1051+
10521052
removeCachedMutationBatchMetadata(batchId: BatchId): void {
10531053
this.mutationQueue.removeCachedMutationKeys(batchId);
10541054
}

packages/firestore/src/local/memory_mutation_queue.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import { Timestamp } from '../api/timestamp';
1919
import { Query } from '../core/query';
2020
import { BatchId } from '../core/types';
21-
import { DocumentKeySet } from '../model/collections';
2221
import { DocumentKey } from '../model/document_key';
2322
import { Mutation } from '../model/mutation';
2423
import { MutationBatch, BATCHID_UNKNOWN } from '../model/mutation_batch';

0 commit comments

Comments
 (0)