Skip to content

Commit fc2e967

Browse files
author
Greg Soltis
authored
Ensure persistence started (#1043)
* Ensure persistence is started when passed to the local store * [AUTOMATED]: Prettier Code Styling * Review responses
1 parent b8a061b commit fc2e967

File tree

5 files changed

+38
-23
lines changed

5 files changed

+38
-23
lines changed

packages/firestore/src/local/indexeddb_persistence.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ export class IndexedDbPersistence implements Persistence {
9696
static MAIN_DATABASE = 'main';
9797

9898
private simpleDb: SimpleDb;
99-
private started: boolean;
99+
private _started = false;
100100
private dbName: string;
101101
private localStoragePrefix: string;
102102
private ownerId: string = this.generateOwnerId();
@@ -130,7 +130,6 @@ export class IndexedDbPersistence implements Persistence {
130130
}
131131

132132
assert(!this.started, 'IndexedDbPersistence double-started!');
133-
this.started = true;
134133

135134
return SimpleDb.openOrCreate(this.dbName, SCHEMA_VERSION, createOrUpgradeDb)
136135
.then(db => {
@@ -140,12 +139,15 @@ export class IndexedDbPersistence implements Persistence {
140139
.then(() => {
141140
this.scheduleOwnerLeaseRefreshes();
142141
this.attachWindowUnloadHook();
142+
})
143+
.then(() => {
144+
this._started = true;
143145
});
144146
}
145147

146148
shutdown(deleteData?: boolean): Promise<void> {
147149
assert(this.started, 'IndexedDbPersistence shutdown without start!');
148-
this.started = false;
150+
this._started = false;
149151
this.detachWindowUnloadHook();
150152
this.stopOwnerLeaseRefreshes();
151153
return this.releaseOwnerLease().then(() => {
@@ -156,6 +158,10 @@ export class IndexedDbPersistence implements Persistence {
156158
});
157159
}
158160

161+
get started(): boolean {
162+
return this._started;
163+
}
164+
159165
getMutationQueue(user: User): MutationQueue {
160166
return IndexedDbMutationQueue.forUser(user, this.serializer);
161167
}

packages/firestore/src/local/local_store.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,10 @@ export class LocalStore {
163163
*/
164164
private garbageCollector: GarbageCollector
165165
) {
166+
assert(
167+
persistence.started,
168+
'LocalStore was passed an unstarted persistence implementation'
169+
);
166170
this.mutationQueue = persistence.getMutationQueue(initialUser);
167171
this.remoteDocuments = persistence.getRemoteDocumentCache();
168172
this.queryCache = persistence.getQueryCache();

packages/firestore/src/local/memory_persistence.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,18 +45,22 @@ export class MemoryPersistence implements Persistence {
4545
private remoteDocumentCache = new MemoryRemoteDocumentCache();
4646
private queryCache = new MemoryQueryCache();
4747

48-
private started = false;
48+
private _started = false;
4949

5050
async start(): Promise<void> {
5151
// No durable state to read on startup.
52-
assert(!this.started, 'MemoryPersistence double-started!');
53-
this.started = true;
52+
assert(!this._started, 'MemoryPersistence double-started!');
53+
this._started = true;
5454
}
5555

5656
async shutdown(deleteData?: boolean): Promise<void> {
5757
// No durable state to ensure is closed on shutdown.
58-
assert(this.started, 'MemoryPersistence shutdown without start!');
59-
this.started = false;
58+
assert(this._started, 'MemoryPersistence shutdown without start!');
59+
this._started = false;
60+
}
61+
62+
get started(): boolean {
63+
return this._started;
6064
}
6165

6266
getMutationQueue(user: User): MutationQueue {

packages/firestore/src/local/persistence.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,11 @@ export interface PersistenceTransaction {}
6868
* writes.
6969
*/
7070
export interface Persistence {
71+
/**
72+
* Whether or not this persistence instance has been started.
73+
*/
74+
readonly started: boolean;
75+
7176
/**
7277
* Starts persistent storage, opening the database or similar.
7378
*

packages/firestore/test/unit/specs/spec_test_runner.ts

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -356,26 +356,31 @@ abstract class TestRunner {
356356
this.serializer = new JsonProtoSerializer(this.databaseInfo.databaseId, {
357357
useProto3Json: true
358358
});
359-
this.persistence = this.getPersistence(this.serializer);
360359

361360
this.useGarbageCollection = config.useGarbageCollection;
362361

363-
this.init();
362+
this.queue = new AsyncQueue();
364363

365364
this.expectedLimboDocs = [];
366365
this.expectedActiveTargets = {};
367366
}
368367

369-
private init(): void {
368+
async start(): Promise<void> {
369+
this.persistence = this.getPersistence(this.serializer);
370+
await this.persistence.start();
371+
await this.init();
372+
}
373+
374+
async init(): Promise<void> {
370375
const garbageCollector = this.getGarbageCollector();
371376

372377
this.localStore = new LocalStore(
373378
this.persistence,
374379
this.user,
375380
garbageCollector
376381
);
382+
await this.localStore.start();
377383

378-
this.queue = new AsyncQueue();
379384
this.connection = new MockConnection(this.queue);
380385
this.datastore = new Datastore(
381386
this.queue,
@@ -393,6 +398,7 @@ abstract class TestRunner {
393398
this.queue,
394399
onlineStateChangedHandler
395400
);
401+
await this.remoteStore.start();
396402

397403
this.syncEngine = new SyncEngine(
398404
this.localStore,
@@ -417,13 +423,6 @@ abstract class TestRunner {
417423
): Persistence;
418424
protected abstract destroyPersistence(): Promise<void>;
419425

420-
async start(): Promise<void> {
421-
this.connection.reset();
422-
await this.persistence.start();
423-
await this.localStore.start();
424-
await this.remoteStore.start();
425-
}
426-
427426
async shutdown(): Promise<void> {
428427
await this.remoteStore.shutdown();
429428
await this.persistence.shutdown(/* deleteData= */ true);
@@ -784,13 +783,10 @@ abstract class TestRunner {
784783
// No local store to shutdown.
785784
await this.remoteStore.shutdown();
786785

787-
this.init();
788-
789786
// We have to schedule the starts, otherwise we could end up with
790787
// interleaved events.
791788
await this.queue.enqueue(async () => {
792-
await this.localStore.start();
793-
await this.remoteStore.start();
789+
await this.init();
794790
});
795791
}
796792

0 commit comments

Comments
 (0)