Skip to content

Commit 878c501

Browse files
Simplify getLastDocumentChange (#2566)
1 parent 7d27807 commit 878c501

File tree

7 files changed

+29
-40
lines changed

7 files changed

+29
-40
lines changed

packages/firestore/src/local/indexeddb_remote_document_cache.ts

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -291,11 +291,6 @@ export class IndexedDbRemoteDocumentCache implements RemoteDocumentCache {
291291
.next(() => results);
292292
}
293293

294-
/**
295-
* Returns the set of documents that have been updated since the specified read
296-
* time.
297-
*/
298-
// PORTING NOTE: This is only used for multi-tab synchronization.
299294
getNewDocumentChanges(
300295
transaction: PersistenceTransaction,
301296
sinceReadTime: SnapshotVersion
@@ -328,37 +323,25 @@ export class IndexedDbRemoteDocumentCache implements RemoteDocumentCache {
328323
});
329324
}
330325

331-
/**
332-
* Returns the last document that has changed, as well as the read time of the
333-
* last change. If no document has changed, returns SnapshotVersion.MIN.
334-
*/
335-
// PORTING NOTE: This is only used for multi-tab synchronization.
336-
getLastDocumentChange(
326+
getLastReadTime(
337327
transaction: PersistenceTransaction
338-
): PersistencePromise<{
339-
changedDoc: MaybeDocument | undefined;
340-
readTime: SnapshotVersion;
341-
}> {
328+
): PersistencePromise<SnapshotVersion> {
342329
const documentsStore = remoteDocumentsStore(transaction);
343330

344331
// If there are no existing entries, we return SnapshotVersion.MIN.
345332
let readTime = SnapshotVersion.MIN;
346-
let changedDoc: MaybeDocument | undefined;
347333

348334
return documentsStore
349335
.iterate(
350336
{ index: DbRemoteDocument.readTimeIndex, reverse: true },
351337
(key, dbRemoteDoc, control) => {
352-
changedDoc = this.serializer.fromDbRemoteDocument(dbRemoteDoc);
353338
if (dbRemoteDoc.readTime) {
354339
readTime = this.serializer.fromDbTimestampKey(dbRemoteDoc.readTime);
355340
}
356341
control.done();
357342
}
358343
)
359-
.next(() => {
360-
return { changedDoc, readTime };
361-
});
344+
.next(() => readTime);
362345
}
363346

364347
newChangeBuffer(options?: {

packages/firestore/src/local/local_store.ts

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1112,18 +1112,11 @@ export class LocalStore {
11121112
*/
11131113
// PORTING NOTE: Multi-tab only.
11141114
async synchronizeLastDocumentChangeReadTime(): Promise<void> {
1115-
if (this.remoteDocuments instanceof IndexedDbRemoteDocumentCache) {
1116-
const remoteDocumentCache = this.remoteDocuments;
1117-
return this.persistence
1118-
.runTransaction(
1119-
'Synchronize last document change read time',
1120-
'readonly-idempotent',
1121-
txn => remoteDocumentCache.getLastDocumentChange(txn)
1122-
)
1123-
.then(({ readTime }) => {
1124-
this.lastDocumentChangeReadTime = readTime;
1125-
});
1126-
}
1115+
this.lastDocumentChangeReadTime = await this.persistence.runTransaction(
1116+
'Synchronize last document change read time',
1117+
'readonly-idempotent',
1118+
txn => this.remoteDocuments.getLastReadTime(txn)
1119+
);
11271120
}
11281121
}
11291122

packages/firestore/src/local/memory_remote_document_cache.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,12 @@ export class MemoryRemoteDocumentCache implements RemoteDocumentCache {
188188
);
189189
}
190190

191+
getLastReadTime(
192+
transaction: PersistenceTransaction
193+
): PersistencePromise<SnapshotVersion> {
194+
return PersistencePromise.resolve(SnapshotVersion.MIN);
195+
}
196+
191197
newChangeBuffer(options?: {
192198
trackRemovals: boolean;
193199
}): RemoteDocumentChangeBuffer {

packages/firestore/src/local/remote_document_cache.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,15 @@ export interface RemoteDocumentCache {
9595
readTime: SnapshotVersion;
9696
}>;
9797

98+
/**
99+
* Returns the read time of the most recently read document in the cache, or
100+
* SnapshotVersion.MIN if not available.
101+
*/
102+
// PORTING NOTE: This is only used for multi-tab synchronization.
103+
getLastReadTime(
104+
transaction: PersistenceTransaction
105+
): PersistencePromise<SnapshotVersion>;
106+
98107
/**
99108
* Provides access to add or update the contents of the cache. The buffer
100109
* handles proper size accounting for the change.

packages/firestore/test/integration/api_internal/database.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ apiDescribe('Database (with internal API)', (persistence: boolean) => {
6868
}
6969
);
7070
});
71-
71+
7272
it('app delete leads to instance termination', async () => {
7373
await withTestDoc(persistence, async docRef => {
7474
await docRef.set({ foo: 'bar' });

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ export class CountingQueryEngine implements QueryEngine {
115115
});
116116
},
117117
getNewDocumentChanges: subject.getNewDocumentChanges,
118+
getLastReadTime: subject.getLastReadTime,
118119
getSize: subject.getSize,
119120
newChangeBuffer: subject.newChangeBuffer
120121
};

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

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -90,16 +90,13 @@ describe('IndexedDbRemoteDocumentCache', () => {
9090
await persistenceHelpers.clearTestPersistence();
9191
});
9292

93-
function getLastDocumentChange(): Promise<{
94-
changedDoc: MaybeDocument | undefined;
95-
readTime: SnapshotVersion;
96-
}> {
93+
function getLastReadTime(): Promise<SnapshotVersion> {
9794
return persistence.runTransaction(
98-
'getLastDocumentChange',
95+
'getLastReadTime',
9996
'readonly-idempotent',
10097
txn => {
10198
const remoteDocuments = persistence.getRemoteDocumentCache();
102-
return remoteDocuments.getLastDocumentChange(txn);
99+
return remoteDocuments.getLastReadTime(txn);
103100
}
104101
);
105102
}
@@ -115,7 +112,7 @@ describe('IndexedDbRemoteDocumentCache', () => {
115112
dontPurgeData: true
116113
});
117114
cache = new TestRemoteDocumentCache(persistence);
118-
const { readTime } = await getLastDocumentChange();
115+
const readTime = await getLastReadTime();
119116
const { changedDocs } = await cache.getNewDocumentChanges(readTime);
120117
assertMatches([], changedDocs);
121118
});

0 commit comments

Comments
 (0)