15
15
* limitations under the License.
16
16
*/
17
17
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' ;
29
19
import { debugAssert } from '../util/assert' ;
30
20
import { FirestoreError } from '../util/error' ;
31
21
import { Deferred } from '../util/promise' ;
32
22
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' ;
35
30
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 > {
38
54
private _progressObserver : PartialObserver < LoadBundleTaskProgress > = { } ;
39
55
private _taskCompletionResolver = new Deferred < LoadBundleTaskProgress > ( ) ;
40
56
@@ -46,6 +62,14 @@ export class LoadBundleTask
46
62
documentsLoaded : 0
47
63
} ;
48
64
65
+ /**
66
+ * Registers functions to listen to bundle loading progress events.
67
+ * @param next - Called when there is a progress update from bundle loading. Typically `next` calls occur
68
+ * each time a Firestore document is loaded from the bundle.
69
+ * @param error - Called when an error occurs during bundle loading. The task aborts after reporting the
70
+ * error, and there should be no more updates after this.
71
+ * @param complete - Called when the loading task is complete.
72
+ */
49
73
onProgress (
50
74
next ?: ( progress : LoadBundleTaskProgress ) => unknown ,
51
75
error ?: ( err : Error ) => unknown ,
@@ -58,12 +82,24 @@ export class LoadBundleTask
58
82
} ;
59
83
}
60
84
85
+ /**
86
+ * Implements the `Promise<LoadBundleTaskProgress>.catch` interface.
87
+ *
88
+ * @param onRejected - Called when an error occurs during bundle loading.
89
+ */
61
90
catch < R > (
62
91
onRejected : ( a : Error ) => R | PromiseLike < R >
63
92
) : Promise < R | LoadBundleTaskProgress > {
64
93
return this . _taskCompletionResolver . promise . catch ( onRejected ) ;
65
94
}
66
95
96
+ /**
97
+ * Implements the `Promise<LoadBundleTaskProgress>.then` interface.
98
+ *
99
+ * @param onFulfilled - Called on the completion of the loading task with a final `LoadBundleTaskProgress` update.
100
+ * The update will always have its `taskState` set to `"Success"`.
101
+ * @param onRejected - Called when an error occurs during bundle loading.
102
+ */
67
103
then < T , R > (
68
104
onFulfilled ?: ( a : LoadBundleTaskProgress ) => T | PromiseLike < T > ,
69
105
onRejected ?: ( a : Error ) => R | PromiseLike < R >
@@ -74,6 +110,8 @@ export class LoadBundleTask
74
110
/**
75
111
* Notifies all observers that bundle loading has completed, with a provided
76
112
* `LoadBundleTaskProgress` object.
113
+ *
114
+ * @private
77
115
*/
78
116
_completeWith ( progress : LoadBundleTaskProgress ) : void {
79
117
debugAssert (
@@ -91,6 +129,8 @@ export class LoadBundleTask
91
129
/**
92
130
* Notifies all observers that bundle loading has failed, with a provided
93
131
* `Error` as the reason.
132
+ *
133
+ * @private
94
134
*/
95
135
_failWith ( error : FirestoreError ) : void {
96
136
this . _lastProgress . taskState = 'Error' ;
@@ -109,6 +149,8 @@ export class LoadBundleTask
109
149
/**
110
150
* Notifies a progress update of loading a bundle.
111
151
* @param progress - The new progress.
152
+ *
153
+ * @private
112
154
*/
113
155
_updateProgress ( progress : LoadBundleTaskProgress ) : void {
114
156
debugAssert (
@@ -122,30 +164,3 @@ export class LoadBundleTask
122
164
}
123
165
}
124
166
}
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