@@ -43,6 +43,7 @@ import {
4343 Document as ProtoDocument ,
4444 Document
4545} from '../protos/firestore_proto_api' ;
46+ import { AutoId } from '../util/misc' ;
4647
4748import { debugAssert } from './assert' ;
4849
@@ -82,7 +83,9 @@ export class BundleBuilder {
8283 ) ;
8384 }
8485
85- toBundleDocument ( docBundleData : DocumentBundleData ) : ProtoDocument {
86+ private toBundleDocument (
87+ docBundleData : DocumentSnapshotBundleData
88+ ) : ProtoDocument {
8689 // TODO handle documents that have mutations
8790 debugAssert (
8891 ! docBundleData . documentData . hasLocalMutations ,
@@ -105,8 +108,16 @@ export class BundleBuilder {
105108 } ;
106109 }
107110
111+ /**
112+ * Adds data from a DocumentSnapshot to the bundle.
113+ * @internal
114+ * @param docBundleData A DocumentSnapshotBundleData containing information from the
115+ * DocumentSnapshot. Note we cannot accept a DocumentSnapshot directly due to a circular
116+ * dependency error.
117+ * @param queryName The name of the QuerySnapshot if this document is part of a Query.
118+ */
108119 addBundleDocument (
109- docBundleData : DocumentBundleData ,
120+ docBundleData : DocumentSnapshotBundleData ,
110121 queryName ?: string
111122 ) : void {
112123 const originalDocument = this . documents . get ( docBundleData . documentPath ) ;
@@ -130,7 +141,6 @@ export class BundleBuilder {
130141 }
131142 } ) ;
132143 }
133-
134144 // Update `queries` to include both original and `queryName`.
135145 if ( queryName ) {
136146 const newDocument = this . documents . get ( docBundleData . documentPath ) ! ;
@@ -144,28 +154,33 @@ export class BundleBuilder {
144154 }
145155 }
146156
147- addBundleQuery ( query : Query , docBundleDataArray : DocumentBundleData [ ] ) : void {
148- const queryTarget = toQueryTarget ( this . serializer , queryToTarget ( query ) ) ;
149- const name = queryTarget . parent . canonicalString ( ) ;
150-
157+ /**
158+ * Adds data from a QuerySnapshot to the bundle.
159+ * @internal
160+ * @param docBundleData A QuerySnapshotBundleData containing information from the
161+ * QuerySnapshot. Note we cannot accept a QuerySnapshot directly due to a circular
162+ * dependency error.
163+ */
164+ addBundleQuery ( queryBundleData : QuerySnapshotBundleData ) : void {
165+ const name = AutoId . newId ( ) ;
151166 if ( this . namedQueries . has ( name ) ) {
152167 throw new Error ( `Query name conflict: ${ name } has already been added.` ) ;
153168 }
154-
155169 let latestReadTime = new Timestamp ( 0 , 0 ) ;
156- for ( const docBundleData of docBundleDataArray ) {
170+ for ( const docBundleData of queryBundleData . docBundleDataArray ) {
157171 this . addBundleDocument ( docBundleData , name ) ;
158172 if ( docBundleData . readTime && docBundleData . readTime > latestReadTime ) {
159173 latestReadTime = docBundleData . readTime ;
160174 }
161175 }
162-
176+ const queryTarget = toQueryTarget (
177+ this . serializer ,
178+ queryToTarget ( queryBundleData . query )
179+ ) ;
163180 const bundledQuery = {
164- parent : queryTarget . parent . canonicalString ( ) ,
165- structuredQuery : queryTarget . queryTarget . structuredQuery ,
166- limitType : null
181+ parent : queryBundleData . parent ,
182+ structuredQuery : queryTarget . queryTarget . structuredQuery
167183 } ;
168-
169184 this . namedQueries . set ( name , {
170185 name,
171186 bundledQuery,
@@ -226,19 +241,26 @@ export class BundleBuilder {
226241
227242/**
228243 * Interface for an object that contains data required to bundle a DocumentSnapshot.
229- * Accessing the methods of DocumentSnapshot directly to retreivew this data in this
230- * implementation would create a circular dependency.
231- *
232244 * @internal
233245 */
234- export interface DocumentBundleData {
235- readonly documentData : DocumentData ;
236- readonly documentKey : DocumentKey ;
237- readonly documentPath : string ;
238- readonly documentExists : boolean ;
239- readonly createdTime : Timestamp ;
240- readonly readTime ?: Timestamp ;
241- readonly versionTime : Timestamp ;
246+ export interface DocumentSnapshotBundleData {
247+ documentData : DocumentData ;
248+ documentKey : DocumentKey ;
249+ documentPath : string ;
250+ documentExists : boolean ;
251+ createdTime : Timestamp ;
252+ readTime ?: Timestamp ;
253+ versionTime : Timestamp ;
254+ }
255+
256+ /**
257+ * Interface for an object that contains data required to bundle a QuerySnapshot.
258+ * @internal
259+ */
260+ export interface QuerySnapshotBundleData {
261+ query : Query ;
262+ parent : string ;
263+ docBundleDataArray : DocumentSnapshotBundleData [ ] ;
242264}
243265
244266/**
@@ -252,47 +274,3 @@ class BundledDocument {
252274 readonly document ?: Document
253275 ) { }
254276}
255-
256- /**
257- * Validates that 'value' is a string.
258- *
259- * @private
260- * @internal
261- * @param arg The argument name or argument index (for varargs methods).
262- * @param value The input to validate.
263- * @param options Options that specify whether the string can be omitted.
264- */
265- export function validateString ( arg : string | number , value : unknown ) : void {
266- if ( typeof value !== 'string' ) {
267- throw new Error ( invalidArgumentMessage ( arg , 'string' ) ) ;
268- }
269- }
270-
271- /**
272- * Generates an error message to use with invalid arguments.
273- *
274- * @private
275- * @internal
276- * @param arg The argument name or argument index (for varargs methods).
277- * @param expectedType The expected input type.
278- */
279- export function invalidArgumentMessage (
280- arg : string | number ,
281- expectedType : string
282- ) : string {
283- return `${ formatArgumentName ( arg ) } is not a valid ${ expectedType } .` ;
284- }
285-
286- /**
287- * Creates a descriptive name for the provided argument name or index.
288- *
289- * @private
290- * @internal
291- * @param arg The argument name or argument index (for varargs methods).
292- * @return Either the argument name or its index description.
293- */
294- function formatArgumentName ( arg : string | number ) : string {
295- return typeof arg === 'string'
296- ? `Value for argument "${ arg } "`
297- : `Element at index ${ arg } ` ;
298- }
0 commit comments