Skip to content

Commit 1b53351

Browse files
committed
clean up addBundleDocument readTime logic.
1 parent 06d84e0 commit 1b53351

File tree

2 files changed

+19
-13
lines changed

2 files changed

+19
-13
lines changed

packages/firestore/src/remote/serializer.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,10 @@ export function toTimestamp(
226226
}
227227
}
228228

229-
function fromTimestamp(date: ProtoTimestamp): Timestamp {
229+
/**
230+
* Returns a Timestamp type given timestamp from a protobuf.
231+
*/
232+
export function fromTimestamp(date: ProtoTimestamp): Timestamp {
230233
const timestamp = normalizeTimestamp(date);
231234
return new Timestamp(timestamp.seconds, timestamp.nanos);
232235
}

packages/firestore/src/util/bundle_builder_impl.ts

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import {
1919
JsonProtoSerializer,
20+
fromTimestamp,
2021
toName,
2122
toQueryTarget,
2223
toTimestamp
@@ -99,24 +100,29 @@ export class BundleBuilder {
99100
): void {
100101
const originalDocument = this.documents.get(docBundleData.documentPath);
101102
const originalQueries = originalDocument?.metadata.queries;
103+
const docReadTime: Timestamp | undefined = docBundleData.readTime;
104+
const origDocReadTime: Timestamp | null = !!originalDocument?.metadata
105+
.readTime
106+
? fromTimestamp(originalDocument.metadata.readTime)
107+
: null;
102108

103-
const readTime = docBundleData.readTime;
104-
// Update with document built from `snap` because it is newer.
105-
if (
106-
!originalDocument ||
107-
(!readTime && !originalDocument.metadata.readTime) ||
108-
(readTime && originalDocument.metadata.readTime! < readTime)
109-
) {
109+
const neitherHasReadTime: boolean = !docReadTime && origDocReadTime == null;
110+
const docIsNewer: boolean = docReadTime !== undefined && (origDocReadTime == null || origDocReadTime < docReadTime);
111+
if (neitherHasReadTime || docIsNewer) {
112+
// Store document.
110113
this.documents.set(docBundleData.documentPath, {
111114
document: this.toBundleDocument(docBundleData),
112115
metadata: {
113116
name: toName(this.serializer, docBundleData.documentKey),
114-
readTime: !!readTime
115-
? toTimestamp(this.serializer, readTime)
117+
readTime: !!docReadTime
118+
? toTimestamp(this.serializer, docReadTime) // Convert Timestamp to proto format.
116119
: undefined,
117120
exists: docBundleData.documentExists
118121
}
119122
});
123+
}
124+
if (docReadTime && docReadTime > this.latestReadTime) {
125+
this.latestReadTime = docReadTime;
120126
}
121127
// Update `queries` to include both original and `queryName`.
122128
if (queryName) {
@@ -126,9 +132,6 @@ export class BundleBuilder {
126132
newDocument.metadata.queries!.push(queryName);
127133
}
128134
}
129-
if (readTime && readTime > this.latestReadTime) {
130-
this.latestReadTime = readTime;
131-
}
132135
}
133136

134137
/**

0 commit comments

Comments
 (0)