1515 * limitations under the License.
1616 */
1717
18- import {
19- LoadBundleTask as ApiLoadBundleTask ,
20- LoadBundleTaskProgress
21- } from '@firebase/firestore-types' ;
22-
23- import { ensureFirestoreConfigured } from '../../src/exp/database' ;
24- import { Query as ExpQuery } from '../../src/exp/reference' ;
25- import {
26- firestoreClientGetNamedQuery ,
27- firestoreClientLoadBundle
28- } from '../core/firestore_client' ;
18+ import { PartialObserver } from '../api/observer' ;
2919import { debugAssert } from '../util/assert' ;
3020import { FirestoreError } from '../util/error' ;
3121import { Deferred } from '../util/promise' ;
3222
33- import { Query , Firestore } from './database' ;
34- import { PartialObserver } from './observer' ;
23+ /**
24+ * Represents the state of bundle loading tasks.
25+ *
26+ * Both 'Error' and 'Success' are sinking state: task will abort or complete and there will
27+ * be no more updates after they are reported.
28+ */
29+ export type TaskState = 'Error' | 'Running' | 'Success' ;
3530
36- export class LoadBundleTask
37- implements ApiLoadBundleTask , PromiseLike < LoadBundleTaskProgress > {
31+ /**
32+ * Represents a progress update or a final state from loading bundles.
33+ */
34+ export interface LoadBundleTaskProgress {
35+ /** How many documents have been loaded. */
36+ documentsLoaded : number ;
37+ /** How many documents are in the bundle being loaded. */
38+ totalDocuments : number ;
39+ /** How many bytes have been loaded. */
40+ bytesLoaded : number ;
41+ /** How many bytes are in the bundle being loaded. */
42+ totalBytes : number ;
43+ /** Current task state. */
44+ taskState : TaskState ;
45+ }
46+
47+ /**
48+ * Represents the task of loading a Firestore bundle. It provides progress of bundle
49+ * loading, as well as task completion and error events.
50+ *
51+ * The API is compatible with `Promise<LoadBundleTaskProgress>`.
52+ */
53+ export class LoadBundleTask implements PromiseLike < LoadBundleTaskProgress > {
3854 private _progressObserver : PartialObserver < LoadBundleTaskProgress > = { } ;
3955 private _taskCompletionResolver = new Deferred < LoadBundleTaskProgress > ( ) ;
4056
@@ -46,6 +62,17 @@ export class LoadBundleTask
4662 documentsLoaded : 0
4763 } ;
4864
65+ /**
66+ * Registers functions to listen to bundle loading progress events.
67+ * @param next
68+ * Called when there is a progress update from bundle loading. Typically `next` calls occur
69+ * each time a Firestore document is loaded from the bundle.
70+ * @param error
71+ * Called when an error occurs during bundle loading. The task aborts after reporting the
72+ * error, and there should be no more updates after this.
73+ * @param complete
74+ * Called when the loading task is complete.
75+ */
4976 onProgress (
5077 next ?: ( progress : LoadBundleTaskProgress ) => unknown ,
5178 error ?: ( err : Error ) => unknown ,
@@ -58,12 +85,27 @@ export class LoadBundleTask
5885 } ;
5986 }
6087
88+ /**
89+ * Implements the `Promise<LoadBundleTaskProgress>.catch` interface.
90+ *
91+ * @param onRejected
92+ * Called when an error occurs during bundle loading.
93+ */
6194 catch < R > (
6295 onRejected : ( a : Error ) => R | PromiseLike < R >
6396 ) : Promise < R | LoadBundleTaskProgress > {
6497 return this . _taskCompletionResolver . promise . catch ( onRejected ) ;
6598 }
6699
100+ /**
101+ * Implements the `Promise<LoadBundleTaskProgress>.then` interface.
102+ *
103+ * @param onFulfilled
104+ * Called on the completion of the loading task with a final `LoadBundleTaskProgress` update.
105+ * The update will always have its `taskState` set to `"Success"`.
106+ * @param onRejected
107+ * Called when an error occurs during bundle loading.
108+ */
67109 then < T , R > (
68110 onFulfilled ?: ( a : LoadBundleTaskProgress ) => T | PromiseLike < T > ,
69111 onRejected ?: ( a : Error ) => R | PromiseLike < R >
@@ -122,30 +164,3 @@ export class LoadBundleTask
122164 }
123165 }
124166}
125-
126- export function loadBundle (
127- db : Firestore ,
128- bundleData : ArrayBuffer | ReadableStream < Uint8Array > | string
129- ) : LoadBundleTask {
130- const resultTask = new LoadBundleTask ( ) ;
131- firestoreClientLoadBundle (
132- ensureFirestoreConfigured ( db . _delegate ) ,
133- db . _databaseId ,
134- bundleData ,
135- resultTask
136- ) ;
137- return resultTask ;
138- }
139-
140- export function namedQuery ( db : Firestore , name : string ) : Promise < Query | null > {
141- return firestoreClientGetNamedQuery (
142- ensureFirestoreConfigured ( db . _delegate ) ,
143- name
144- ) . then ( namedQuery => {
145- if ( ! namedQuery ) {
146- return null ;
147- }
148-
149- return new Query ( db , new ExpQuery ( db . _delegate , null , namedQuery . query ) ) ;
150- } ) ;
151- }
0 commit comments