@@ -31,28 +31,18 @@ import {
31
31
import * as api from '../protos/firestore_proto_api' ;
32
32
import { DocumentKey } from '../model/document_key' ;
33
33
import { MaybeDocument , NoDocument } from '../model/document' ;
34
- import { debugAssert , debugCast } from '../util/assert' ;
35
- import { LocalStore } from '../local/local_store' ;
36
- import { BundleReader , SizedBundleElement } from '../util/bundle_reader' ;
34
+ import { debugAssert } from '../util/assert' ;
35
+ import {
36
+ applyBundleDocuments ,
37
+ LocalStore ,
38
+ saveNamedQuery
39
+ } from '../local/local_store' ;
40
+ import { SizedBundleElement } from '../util/bundle_reader' ;
37
41
import {
38
42
documentKeySet ,
39
43
DocumentKeySet ,
40
44
MaybeDocumentMap
41
45
} from '../model/collections' ;
42
- import { LoadBundleTask } from '../api/bundle' ;
43
- import {
44
- emitNewSnapsAndNotifyLocalStore ,
45
- SyncEngine ,
46
- SyncEngineImpl
47
- } from './sync_engine' ;
48
- import { logWarn } from '../util/log' ;
49
- import { LOG_TAG } from '../../lite/src/api/components' ;
50
- import {
51
- applyBundleDocuments ,
52
- hasNewerBundle ,
53
- saveBundle ,
54
- saveNamedQuery
55
- } from '../local/local_store_bundle' ;
56
46
57
47
/**
58
48
* Represents a bundled document, including the metadata and the document
@@ -105,7 +95,7 @@ export class BundleConverter {
105
95
* Returns a `LoadBundleTaskProgress` representing the initial progress of
106
96
* loading a bundle.
107
97
*/
108
- function bundleInitialProgress (
98
+ export function bundleInitialProgress (
109
99
metadata : ProtoBundleMetadata
110
100
) : LoadBundleTaskProgress {
111
101
return {
@@ -121,7 +111,7 @@ function bundleInitialProgress(
121
111
* Returns a `LoadBundleTaskProgress` representing the progress that the loading
122
112
* has succeeded.
123
113
*/
124
- function bundleSuccessProgress (
114
+ export function bundleSuccessProgress (
125
115
metadata : ProtoBundleMetadata
126
116
) : LoadBundleTaskProgress {
127
117
return {
@@ -133,7 +123,7 @@ function bundleSuccessProgress(
133
123
} ;
134
124
}
135
125
136
- class BundleLoadResult {
126
+ export class BundleLoadResult {
137
127
constructor (
138
128
readonly progress : LoadBundleTaskProgress ,
139
129
readonly changedDocs : MaybeDocumentMap
@@ -144,7 +134,7 @@ class BundleLoadResult {
144
134
* A class to process the elements from a bundle, load them into local
145
135
* storage and provide progress update while loading.
146
136
*/
147
- class BundleLoader {
137
+ export class BundleLoader {
148
138
/** The current progress of loading */
149
139
private progress : LoadBundleTaskProgress ;
150
140
/** Batched queries to be saved into storage */
@@ -249,77 +239,3 @@ class BundleLoader {
249
239
return new BundleLoadResult ( { ...this . progress } , changedDocuments ) ;
250
240
}
251
241
}
252
-
253
- /**
254
- * Loads a Firestore bundle into the SDK. The returned promise resolves when
255
- * the bundle finished loading.
256
- *
257
- * @param bundleReader Bundle to load into the SDK.
258
- * @param task LoadBundleTask used to update the loading progress to public API.
259
- */
260
- export function syncEngineLoadBundle (
261
- syncEngine : SyncEngine ,
262
- bundleReader : BundleReader ,
263
- task : LoadBundleTask
264
- ) : void {
265
- const syncEngineImpl = debugCast ( syncEngine , SyncEngineImpl ) ;
266
-
267
- // eslint-disable-next-line @typescript-eslint/no-floating-promises
268
- loadBundleImpl ( syncEngineImpl , bundleReader , task ) . then ( ( ) => {
269
- syncEngineImpl . sharedClientState . notifyBundleLoaded ( ) ;
270
- } ) ;
271
- }
272
-
273
- async function loadBundleImpl (
274
- syncEngine : SyncEngineImpl ,
275
- reader : BundleReader ,
276
- task : LoadBundleTask
277
- ) : Promise < void > {
278
- try {
279
- const metadata = await reader . getMetadata ( ) ;
280
- const skip = await hasNewerBundle ( syncEngine . localStore , metadata ) ;
281
- if ( skip ) {
282
- await reader . close ( ) ;
283
- task . _completeWith ( bundleSuccessProgress ( metadata ) ) ;
284
- return ;
285
- }
286
-
287
- task . _updateProgress ( bundleInitialProgress ( metadata ) ) ;
288
-
289
- const loader = new BundleLoader (
290
- metadata ,
291
- syncEngine . localStore ,
292
- reader . serializer
293
- ) ;
294
- let element = await reader . nextElement ( ) ;
295
- while ( element ) {
296
- debugAssert (
297
- ! element . payload . metadata ,
298
- 'Unexpected BundleMetadata element.'
299
- ) ;
300
- const progress = await loader . addSizedElement ( element ) ;
301
- if ( progress ) {
302
- task . _updateProgress ( progress ) ;
303
- }
304
-
305
- element = await reader . nextElement ( ) ;
306
- }
307
-
308
- const result = await loader . complete ( ) ;
309
- // TODO(b/160876443): This currently raises snapshots with
310
- // `fromCache=false` if users already listen to some queries and bundles
311
- // has newer version.
312
- await emitNewSnapsAndNotifyLocalStore (
313
- syncEngine ,
314
- result . changedDocs ,
315
- /* remoteEvent */ undefined
316
- ) ;
317
-
318
- // Save metadata, so loading the same bundle will skip.
319
- await saveBundle ( syncEngine . localStore , metadata ) ;
320
- task . _completeWith ( result . progress ) ;
321
- } catch ( e ) {
322
- logWarn ( LOG_TAG , `Loading bundle failed with ${ e } ` ) ;
323
- task . _failWith ( e ) ;
324
- }
325
- }
0 commit comments