Skip to content

Ensure persistence started #1043

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 25, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions packages/firestore/src/local/indexeddb_persistence.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ export class IndexedDbPersistence implements Persistence {
static MAIN_DATABASE = 'main';

private simpleDb: SimpleDb;
private started: boolean;
private _started = false;
private dbName: string;
private localStoragePrefix: string;
private ownerId: string = this.generateOwnerId();
Expand Down Expand Up @@ -130,7 +130,6 @@ export class IndexedDbPersistence implements Persistence {
}

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

return SimpleDb.openOrCreate(this.dbName, SCHEMA_VERSION, createOrUpgradeDb)
.then(db => {
Expand All @@ -140,12 +139,15 @@ export class IndexedDbPersistence implements Persistence {
.then(() => {
this.scheduleOwnerLeaseRefreshes();
this.attachWindowUnloadHook();
})
.then(() => {
this._started = true;
});
}

shutdown(deleteData?: boolean): Promise<void> {
assert(this.started, 'IndexedDbPersistence shutdown without start!');
this.started = false;
this._started = false;
this.detachWindowUnloadHook();
this.stopOwnerLeaseRefreshes();
return this.releaseOwnerLease().then(() => {
Expand All @@ -156,6 +158,10 @@ export class IndexedDbPersistence implements Persistence {
});
}

get started(): boolean {
return this._started;
}

getMutationQueue(user: User): MutationQueue {
return IndexedDbMutationQueue.forUser(user, this.serializer);
}
Expand Down
4 changes: 4 additions & 0 deletions packages/firestore/src/local/local_store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,10 @@ export class LocalStore {
*/
private garbageCollector: GarbageCollector
) {
assert(
persistence.started,
'LocalStore was passed an unstarted persistence implementation'
);
this.mutationQueue = persistence.getMutationQueue(initialUser);
this.remoteDocuments = persistence.getRemoteDocumentCache();
this.queryCache = persistence.getQueryCache();
Expand Down
14 changes: 9 additions & 5 deletions packages/firestore/src/local/memory_persistence.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,22 @@ export class MemoryPersistence implements Persistence {
private remoteDocumentCache = new MemoryRemoteDocumentCache();
private queryCache = new MemoryQueryCache();

private started = false;
private _started = false;

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

async shutdown(deleteData?: boolean): Promise<void> {
// No durable state to ensure is closed on shutdown.
assert(this.started, 'MemoryPersistence shutdown without start!');
this.started = false;
assert(this._started, 'MemoryPersistence shutdown without start!');
this._started = false;
}

get started(): boolean {
return this._started;
}

getMutationQueue(user: User): MutationQueue {
Expand Down
5 changes: 5 additions & 0 deletions packages/firestore/src/local/persistence.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ export interface PersistenceTransaction {}
* writes.
*/
export interface Persistence {
/**
* Whether or not this persistence instance has been started.
*/
readonly started: boolean;

/**
* Starts persistent storage, opening the database or similar.
*
Expand Down
26 changes: 11 additions & 15 deletions packages/firestore/test/unit/specs/spec_test_runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -356,26 +356,31 @@ abstract class TestRunner {
this.serializer = new JsonProtoSerializer(this.databaseInfo.databaseId, {
useProto3Json: true
});
this.persistence = this.getPersistence(this.serializer);

this.useGarbageCollection = config.useGarbageCollection;

this.init();
this.queue = new AsyncQueue();

this.expectedLimboDocs = [];
this.expectedActiveTargets = {};
}

private init(): void {
async start(): Promise<void> {
this.persistence = this.getPersistence(this.serializer);
await this.persistence.start();
await this.init();
}

async init(): Promise<void> {
const garbageCollector = this.getGarbageCollector();

this.localStore = new LocalStore(
this.persistence,
this.user,
garbageCollector
);
await this.localStore.start();

this.queue = new AsyncQueue();
this.connection = new MockConnection(this.queue);
this.datastore = new Datastore(
this.queue,
Expand All @@ -393,6 +398,7 @@ abstract class TestRunner {
this.queue,
onlineStateChangedHandler
);
await this.remoteStore.start();

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

async start(): Promise<void> {
this.connection.reset();
await this.persistence.start();
await this.localStore.start();
await this.remoteStore.start();
}

async shutdown(): Promise<void> {
await this.remoteStore.shutdown();
await this.persistence.shutdown(/* deleteData= */ true);
Expand Down Expand Up @@ -784,13 +783,10 @@ abstract class TestRunner {
// No local store to shutdown.
await this.remoteStore.shutdown();

this.init();

// We have to schedule the starts, otherwise we could end up with
// interleaved events.
await this.queue.enqueue(async () => {
await this.localStore.start();
await this.remoteStore.start();
await this.init();
});
}

Expand Down