Skip to content

Commit f80c49d

Browse files
committed
Update bundleReader to be multi use utility
1 parent f0d3304 commit f80c49d

File tree

1 file changed

+34
-17
lines changed

1 file changed

+34
-17
lines changed

packages/firestore/src/core/bundle_impl.ts

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

1818
import { LoadBundleTaskProgress } from '@firebase/firestore-types';
1919

20+
import { fromBundledQuery } from '../local/local_serializer';
2021
import { LocalStore } from '../local/local_store';
2122
import {
2223
localStoreApplyBundledDocuments,
@@ -105,34 +106,48 @@ export class BundleConverterImpl implements BundleConverter {
105106
mutableDoc
106107
};
107108
}
108-
109109
toSnapshotVersion(time: ApiTimestamp): SnapshotVersion {
110110
return fromVersion(time);
111111
}
112112
}
113113

114114
/**
115-
* A class to process the elements from a bundle, load them into local
115+
* A class to process the elements from a bundle, and optionally load them into local
116116
* storage and provide progress update while loading.
117117
*/
118118
export class BundleLoader {
119119
/** The current progress of loading */
120120
private progress: LoadBundleTaskProgress;
121121
/** Batched queries to be saved into storage */
122-
private queries: ProtoNamedQuery[] = [];
122+
private _queries: ProtoNamedQuery[] = [];
123123
/** Batched documents to be saved into storage */
124-
private documents: BundledDocuments = [];
124+
private _documents: BundledDocuments = [];
125125
/** The collection groups affected by this bundle. */
126126
private collectionGroups = new Set<string>();
127127

128128
constructor(
129129
private bundleMetadata: ProtoBundleMetadata,
130-
private localStore: LocalStore,
131130
private serializer: JsonProtoSerializer
132131
) {
133132
this.progress = bundleInitialProgress(bundleMetadata);
134133
}
135134

135+
/**
136+
* Returns the named queries that have been parsed from the SizeBundleElements added by
137+
* calling {@link adSizedElement}.
138+
*/
139+
get queries(): ProtoNamedQuery[] {
140+
return this._queries;
141+
}
142+
143+
/**
144+
* Returns the BundledDocuments that have been parsed from the SizeBundleElements added by
145+
* calling {@link addSizedElement}.
146+
*/
147+
get documents(): BundledDocuments {
148+
return this._documents;
149+
}
150+
136151
/**
137152
* Adds an element from the bundle to the loader.
138153
*
@@ -147,9 +162,9 @@ export class BundleLoader {
147162
let documentsLoaded = this.progress.documentsLoaded;
148163

149164
if (element.payload.namedQuery) {
150-
this.queries.push(element.payload.namedQuery);
165+
this._queries.push(element.payload.namedQuery);
151166
} else if (element.payload.documentMetadata) {
152-
this.documents.push({ metadata: element.payload.documentMetadata });
167+
this._documents.push({ metadata: element.payload.documentMetadata });
153168
if (!element.payload.documentMetadata.exists) {
154169
++documentsLoaded;
155170
}
@@ -163,12 +178,12 @@ export class BundleLoader {
163178
this.collectionGroups.add(path.get(path.length - 2));
164179
} else if (element.payload.document) {
165180
debugAssert(
166-
this.documents.length > 0 &&
167-
this.documents[this.documents.length - 1].metadata.name ===
181+
this._documents.length > 0 &&
182+
this._documents[this._documents.length - 1].metadata.name ===
168183
element.payload.document.name,
169184
'The document being added does not match the stored metadata.'
170185
);
171-
this.documents[this.documents.length - 1].document =
186+
this._documents[this._documents.length - 1].document =
172187
element.payload.document;
173188
++documentsLoaded;
174189
}
@@ -206,26 +221,28 @@ export class BundleLoader {
206221
/**
207222
* Update the progress to 'Success' and return the updated progress.
208223
*/
209-
async complete(): Promise<BundleLoadResult> {
224+
async completeAndStoreAsync(
225+
localStore: LocalStore
226+
): Promise<BundleLoadResult> {
210227
debugAssert(
211-
this.documents[this.documents.length - 1]?.metadata.exists !== true ||
212-
!!this.documents[this.documents.length - 1].document,
228+
this._documents[this._documents.length - 1]?.metadata.exists !== true ||
229+
!!this._documents[this._documents.length - 1].document,
213230
'Bundled documents end with a document metadata element instead of a document.'
214231
);
215232
debugAssert(!!this.bundleMetadata.id, 'Bundle ID must be set.');
216233

217234
const changedDocs = await localStoreApplyBundledDocuments(
218-
this.localStore,
235+
localStore,
219236
new BundleConverterImpl(this.serializer),
220-
this.documents,
237+
this._documents,
221238
this.bundleMetadata.id!
222239
);
223240

224241
const queryDocumentMap = this.getQueryDocumentMapping(this.documents);
225242

226-
for (const q of this.queries) {
243+
for (const q of this._queries) {
227244
await localStoreSaveNamedQuery(
228-
this.localStore,
245+
localStore,
229246
q,
230247
queryDocumentMap.get(q.name!)
231248
);

0 commit comments

Comments
 (0)