Skip to content

Commit 9ebb633

Browse files
committed
Add Firestore instance to constructor
1 parent c18ddd5 commit 9ebb633

File tree

1 file changed

+19
-15
lines changed

1 file changed

+19
-15
lines changed

packages/firestore/src/util/bundle_builder_impl.ts

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import {
2323
BundledDocumentMetadata as ProtoBundledDocumentMetadata,
2424
NamedQuery as ProtoNamedQuery,
2525
} from '../protos/firestore_bundle_proto';
26-
26+
import { Firestore } from '../api/database';
2727
import { DocumentSnapshot } from '../lite-api/snapshot';
2828
import { QuerySnapshot } from '../lite-api/snapshot';
2929
import { Timestamp } from '../lite-api/timestamp';
@@ -34,8 +34,6 @@ import {
3434
toDocument,
3535
toName,
3636
toQueryTarget,
37-
toTimestamp,
38-
toVersion,
3937
} from '../../src/remote/serializer';
4038

4139
import {
@@ -66,10 +64,14 @@ export class BundleBuilder {
6664
// The latest read time among all bundled documents and queries.
6765
private latestReadTime = new Timestamp(0, 0);
6866

69-
constructor(readonly bundleId: string) {}
67+
private databaseId: DatabaseId;
7068

71-
add(databaseId: DatabaseId, documentSnapshot: DocumentSnapshot): BundleBuilder;
72-
add(databaseId: DatabaseId, queryName: string, querySnapshot: QuerySnapshot): BundleBuilder;
69+
constructor(private firestore: Firestore, readonly bundleId: string) {
70+
this.databaseId = firestore._databaseId;
71+
}
72+
73+
add(documentSnapshot: DocumentSnapshot): BundleBuilder;
74+
add(queryName: string, querySnapshot: QuerySnapshot): BundleBuilder;
7375

7476
/**
7577
* Adds a Firestore document snapshot or query snapshot to the bundle.
@@ -92,7 +94,6 @@ export class BundleBuilder {
9294
* ```
9395
*/
9496
add(
95-
databaseId: DatabaseId,
9697
documentOrName: DocumentSnapshot | string,
9798
querySnapshot?: QuerySnapshot
9899
): BundleBuilder {
@@ -102,17 +103,17 @@ export class BundleBuilder {
102103
validateMaxNumberOfArguments('BundleBuilder.add', arguments, 2);
103104
if (arguments.length === 1) {
104105
validateDocumentSnapshot('documentOrName', documentOrName);
105-
this.addBundledDocument(databaseId, documentOrName as DocumentSnapshot);
106+
this.addBundledDocument(documentOrName as DocumentSnapshot);
106107
} else {
107108
validateString('documentOrName', documentOrName);
108109
validateQuerySnapshot('querySnapshot', querySnapshot);
109-
this.addNamedQuery(databaseId, documentOrName as string, querySnapshot!);
110+
this.addNamedQuery(documentOrName as string, querySnapshot!);
110111
}
111112
return this;
112113
}
113114

114-
private addBundledDocument(databaseId: DatabaseId, snap: DocumentSnapshot, queryName?: string): void {
115-
// TODO: is this a valid shortcut out?
115+
private addBundledDocument(snap: DocumentSnapshot, queryName?: string): void {
116+
// TODO: is this a valid shortcircuit?
116117
if(!snap._document || !snap._document.isValidDocument()) {
117118
return;
118119
}
@@ -126,7 +127,10 @@ export class BundleBuilder {
126127
(!snapReadTime && !originalDocument.metadata.readTime) ||
127128
(snapReadTime && originalDocument.metadata.readTime! < snapReadTime)
128129
) {
129-
const serializer = new JsonProtoSerializer(databaseId, /*useProto3Json=*/ false);
130+
131+
// TODO: Should I create on serializer for the bundler instance, or just created one adhoc
132+
// like this?
133+
const serializer = new JsonProtoSerializer(this.databaseId, /*useProto3Json=*/ false);
130134

131135
this.documents.set(snap.ref.path, {
132136
document: snap._document.isFoundDocument() ? toDocument(serializer, mutableCopy) : undefined,
@@ -151,16 +155,16 @@ export class BundleBuilder {
151155
}
152156
}
153157

154-
private addNamedQuery(databaseId: DatabaseId, name: string, querySnap: QuerySnapshot): void {
158+
private addNamedQuery(name: string, querySnap: QuerySnapshot): void {
155159
if (this.namedQueries.has(name)) {
156160
throw new Error(`Query name conflict: ${name} has already been added.`);
157161
}
158162

159-
const serializer = new JsonProtoSerializer(databaseId, /*useProto3Json=*/ false);
163+
const serializer = new JsonProtoSerializer(this.databaseId, /*useProto3Json=*/ false);
160164
const queryTarget = toQueryTarget(serializer, queryToTarget(querySnap.query._query));
161165

162166
for (const snap of querySnap.docs) {
163-
this.addBundledDocument(databaseId, snap, name);
167+
this.addBundledDocument(snap, name);
164168
const readTime = snap.readTime;
165169
if (readTime && readTime > this.latestReadTime) {
166170
this.latestReadTime = readTime;

0 commit comments

Comments
 (0)