Skip to content

Commit 5a28480

Browse files
[Multi-Tab] Don't write document change log when multi-tab is disabled (#1056)
1 parent f7ca08d commit 5a28480

File tree

4 files changed

+50
-16
lines changed

4 files changed

+50
-16
lines changed

packages/firestore/src/local/indexeddb_persistence.ts

+16-1
Original file line numberDiff line numberDiff line change
@@ -440,15 +440,30 @@ export class IndexedDbPersistence implements Persistence {
440440
}
441441

442442
getMutationQueue(user: User): MutationQueue {
443+
assert(
444+
this.started,
445+
'Cannot initialize MutationQueue before persistence is started.'
446+
);
443447
return IndexedDbMutationQueue.forUser(user, this.serializer);
444448
}
445449

446450
getQueryCache(): QueryCache {
451+
assert(
452+
this.started,
453+
'Cannot initialize QueryCache before persistence is started.'
454+
);
447455
return new IndexedDbQueryCache(this.serializer);
448456
}
449457

450458
getRemoteDocumentCache(): RemoteDocumentCache {
451-
return new IndexedDbRemoteDocumentCache(this.serializer);
459+
assert(
460+
this.started,
461+
'Cannot initialize RemoteDocumentCache before persistence is started.'
462+
);
463+
return new IndexedDbRemoteDocumentCache(
464+
this.serializer,
465+
/*keepDocumentChangeLog=*/ this.allowTabSynchronization
466+
);
452467
}
453468

454469
runTransaction<T>(

packages/firestore/src/local/indexeddb_remote_document_cache.ts

+24-6
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,23 @@ import { PersistencePromise } from './persistence_promise';
3737
import { RemoteDocumentCache } from './remote_document_cache';
3838
import { SimpleDb, SimpleDbStore } from './simple_db';
3939
import { SnapshotVersion } from '../core/snapshot_version';
40+
import { assert } from '../util/assert';
4041

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

45-
constructor(private serializer: LocalSerializer) {}
46+
/**
47+
* @param {LocalSerializer} serializer The document serializer.
48+
* @param keepDocumentChangeLog Whether to keep a document change log in
49+
* IndexedDb. This change log is required for Multi-Tab synchronization, but
50+
* not needed in clients that don't share access to their remote document
51+
* cache.
52+
*/
53+
constructor(
54+
private readonly serializer: LocalSerializer,
55+
private readonly keepDocumentChangeLog: boolean
56+
) {}
4657

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

80-
promises.push(
81-
documentChangesStore(transaction).put({
82-
changes: this.serializer.toDbResourcePaths(changedKeys)
83-
})
84-
);
91+
if (this.keepDocumentChangeLog) {
92+
// TODO(multitab): GC the documentChanges store.
93+
promises.push(
94+
documentChangesStore(transaction).put({
95+
changes: this.serializer.toDbResourcePaths(changedKeys)
96+
})
97+
);
98+
}
8599
}
86100

87101
return PersistencePromise.waitFor(promises);
@@ -134,6 +148,10 @@ export class IndexedDbRemoteDocumentCache implements RemoteDocumentCache {
134148
getNewDocumentChanges(
135149
transaction: PersistenceTransaction
136150
): PersistencePromise<MaybeDocumentMap> {
151+
assert(
152+
this.keepDocumentChangeLog,
153+
'Can only call getNewDocumentChanges() when document change log is enabled'
154+
);
137155
let changedKeys = documentKeySet();
138156
let changedDocs = maybeDocumentMap();
139157

packages/firestore/test/unit/local/persistence_test_helpers.ts

+4-6
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,10 @@ const LOCAL_STORAGE_PREFIX = 'firestore_';
5151
* any previous contents if they existed.
5252
*/
5353
export async function testIndexedDbPersistence(
54-
queue?: AsyncQueue,
55-
clientId?: ClientId
54+
synchronizeTabs?: boolean
5655
): Promise<IndexedDbPersistence> {
57-
queue = queue || new AsyncQueue();
58-
clientId = clientId || AutoId.newId();
59-
56+
const queue = new AsyncQueue();
57+
const clientId = AutoId.newId();
6058
const prefix = `${TEST_PERSISTENCE_PREFIX}/`;
6159
await SimpleDb.delete(prefix + IndexedDbPersistence.MAIN_DATABASE);
6260
const partition = new DatabaseId('project');
@@ -71,7 +69,7 @@ export async function testIndexedDbPersistence(
7169
queue,
7270
serializer
7371
);
74-
await persistence.start();
72+
await persistence.start(synchronizeTabs);
7573
return persistence;
7674
}
7775

packages/firestore/test/unit/local/remote_document_cache.test.ts

+6-3
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,12 @@ describe('IndexedDbRemoteDocumentCache', () => {
5252
}
5353

5454
beforeEach(() => {
55-
return persistenceHelpers.testIndexedDbPersistence().then(p => {
56-
persistence = p;
57-
});
55+
// We turn on `synchronizeTabs` to test the document change log.
56+
return persistenceHelpers
57+
.testIndexedDbPersistence(/* synchronizeTabs= */ true)
58+
.then(p => {
59+
persistence = p;
60+
});
5861
});
5962

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

0 commit comments

Comments
 (0)