Skip to content

[Multi-Tab] Don't write document change log when multi-tab is disabled #1056

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 10 commits into from
Jul 27, 2018
17 changes: 16 additions & 1 deletion packages/firestore/src/local/indexeddb_persistence.ts
Original file line number Diff line number Diff line change
Expand Up @@ -440,15 +440,30 @@ export class IndexedDbPersistence implements Persistence {
}

getMutationQueue(user: User): MutationQueue {
assert(
this.started,
'Cannot initialize MutationQueue before persistence is started.'
);
return IndexedDbMutationQueue.forUser(user, this.serializer);
}

getQueryCache(): QueryCache {
assert(
this.started,
'Cannot initialize QueryCache before persistence is started.'
);
return new IndexedDbQueryCache(this.serializer);
}

getRemoteDocumentCache(): RemoteDocumentCache {
return new IndexedDbRemoteDocumentCache(this.serializer);
assert(
this.started,
'Cannot initialize RemoteDocumentCache before persistence is started.'
);
return new IndexedDbRemoteDocumentCache(
this.serializer,
/*keepDocumentChangeLog=*/ this.allowTabSynchronization
);
}

runTransaction<T>(
Expand Down
30 changes: 24 additions & 6 deletions packages/firestore/src/local/indexeddb_remote_document_cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,23 @@ import { PersistencePromise } from './persistence_promise';
import { RemoteDocumentCache } from './remote_document_cache';
import { SimpleDb, SimpleDbStore } from './simple_db';
import { SnapshotVersion } from '../core/snapshot_version';
import { assert } from '../util/assert';

export class IndexedDbRemoteDocumentCache implements RemoteDocumentCache {
/** The last id read by `getNewDocumentChanges()`. */
private lastReturnedDocumentChangesId = 0;

constructor(private serializer: LocalSerializer) {}
/**
* @param {LocalSerializer} serializer The document serializer.
* @param keepDocumentChangeLog Whether to keep a document change log in
* IndexedDb. This change log is required for Multi-Tab synchronization, but
* not needed in clients that don't share access to their remote document
* cache.
*/
constructor(
private readonly serializer: LocalSerializer,
private readonly keepDocumentChangeLog: boolean
) {}

start(transaction: PersistenceTransaction): PersistencePromise<void> {
// If there are no existing changes, we set `lastReturnedDocumentChangesId`
Expand Down Expand Up @@ -77,11 +88,14 @@ export class IndexedDbRemoteDocumentCache implements RemoteDocumentCache {
changedKeys = changedKeys.add(maybeDocument.key);
}

promises.push(
documentChangesStore(transaction).put({
changes: this.serializer.toDbResourcePaths(changedKeys)
})
);
if (this.keepDocumentChangeLog) {
// TODO(multitab): GC the documentChanges store.
promises.push(
documentChangesStore(transaction).put({
changes: this.serializer.toDbResourcePaths(changedKeys)
})
);
}
}

return PersistencePromise.waitFor(promises);
Expand Down Expand Up @@ -134,6 +148,10 @@ export class IndexedDbRemoteDocumentCache implements RemoteDocumentCache {
getNewDocumentChanges(
transaction: PersistenceTransaction
): PersistencePromise<MaybeDocumentMap> {
assert(
this.keepDocumentChangeLog,
'Can only call getNewDocumentChanges() when document change log is enabled'
);
let changedKeys = documentKeySet();
let changedDocs = maybeDocumentMap();

Expand Down
10 changes: 4 additions & 6 deletions packages/firestore/test/unit/local/persistence_test_helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,10 @@ const LOCAL_STORAGE_PREFIX = 'firestore_';
* any previous contents if they existed.
*/
export async function testIndexedDbPersistence(
queue?: AsyncQueue,
clientId?: ClientId
synchronizeTabs?: boolean
): Promise<IndexedDbPersistence> {
queue = queue || new AsyncQueue();
clientId = clientId || AutoId.newId();

const queue = new AsyncQueue();
const clientId = AutoId.newId();
const prefix = `${TEST_PERSISTENCE_PREFIX}/`;
await SimpleDb.delete(prefix + IndexedDbPersistence.MAIN_DATABASE);
const partition = new DatabaseId('project');
Expand All @@ -71,7 +69,7 @@ export async function testIndexedDbPersistence(
queue,
serializer
);
await persistence.start();
await persistence.start(synchronizeTabs);
return persistence;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,12 @@ describe('IndexedDbRemoteDocumentCache', () => {
}

beforeEach(() => {
return persistenceHelpers.testIndexedDbPersistence().then(p => {
persistence = p;
});
// We turn on `synchronizeTabs` to test the document change log.
return persistenceHelpers
.testIndexedDbPersistence(/* synchronizeTabs= */ true)
.then(p => {
persistence = p;
});
});

afterEach(() => persistence.shutdown(/* deleteData= */ true));
Expand Down