15
15
* limitations under the License.
16
16
*/
17
17
18
+ import {
19
+ LoadBundleTask as ApiLoadBundleTask ,
20
+ LoadBundleTaskProgress
21
+ } from '@firebase/firestore-types' ;
18
22
import { Deferred } from '../util/promise' ;
19
23
import { PartialObserver } from './observer' ;
20
24
import { debugAssert } from '../util/assert' ;
21
25
import { FirestoreError } from '../util/error' ;
22
-
23
- export interface ApiLoadBundleTask {
24
- onProgress (
25
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
26
- next ?: ( progress : ApiLoadBundleTaskProgress ) => any ,
27
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
28
- error ?: ( error : Error ) => any ,
29
- complete ?: ( ) => void
30
- ) : void ;
31
-
32
- then < T , R > (
33
- onFulfilled ?: ( a : ApiLoadBundleTaskProgress ) => T | PromiseLike < T > ,
34
- onRejected ?: ( a : Error ) => R | PromiseLike < R >
35
- ) : Promise < T | R > ;
36
-
37
- catch < R > (
38
- onRejected : ( a : Error ) => R | PromiseLike < R >
39
- ) : Promise < R | ApiLoadBundleTaskProgress > ;
40
- }
41
-
42
- export interface ApiLoadBundleTaskProgress {
43
- documentsLoaded : number ;
44
- totalDocuments : number ;
45
- bytesLoaded : number ;
46
- totalBytes : number ;
47
- taskState : TaskState ;
48
- }
49
-
50
- export type TaskState = 'Error' | 'Running' | 'Success' ;
26
+ import { ensureFirestoreConfigured , Query , Firestore } from './database' ;
27
+ import { Query as ExpQuery } from '../../exp/src/api/reference' ;
28
+ import {
29
+ firestoreClientGetNamedQuery ,
30
+ firestoreClientLoadBundle
31
+ } from '../core/firestore_client' ;
51
32
52
33
export class LoadBundleTask
53
- implements ApiLoadBundleTask , PromiseLike < ApiLoadBundleTaskProgress > {
54
- private _progressObserver : PartialObserver < ApiLoadBundleTaskProgress > = { } ;
55
- private _taskCompletionResolver = new Deferred < ApiLoadBundleTaskProgress > ( ) ;
34
+ implements ApiLoadBundleTask , PromiseLike < LoadBundleTaskProgress > {
35
+ private _progressObserver : PartialObserver < LoadBundleTaskProgress > = { } ;
36
+ private _taskCompletionResolver = new Deferred < LoadBundleTaskProgress > ( ) ;
56
37
57
- private _lastProgress : ApiLoadBundleTaskProgress = {
38
+ private _lastProgress : LoadBundleTaskProgress = {
58
39
taskState : 'Running' ,
59
40
totalBytes : 0 ,
60
41
totalDocuments : 0 ,
@@ -63,7 +44,7 @@ export class LoadBundleTask
63
44
} ;
64
45
65
46
onProgress (
66
- next ?: ( progress : ApiLoadBundleTaskProgress ) => unknown ,
47
+ next ?: ( progress : LoadBundleTaskProgress ) => unknown ,
67
48
error ?: ( err : Error ) => unknown ,
68
49
complete ?: ( ) => void
69
50
) : void {
@@ -76,12 +57,12 @@ export class LoadBundleTask
76
57
77
58
catch < R > (
78
59
onRejected : ( a : Error ) => R | PromiseLike < R >
79
- ) : Promise < R | ApiLoadBundleTaskProgress > {
60
+ ) : Promise < R | LoadBundleTaskProgress > {
80
61
return this . _taskCompletionResolver . promise . catch ( onRejected ) ;
81
62
}
82
63
83
64
then < T , R > (
84
- onFulfilled ?: ( a : ApiLoadBundleTaskProgress ) => T | PromiseLike < T > ,
65
+ onFulfilled ?: ( a : LoadBundleTaskProgress ) => T | PromiseLike < T > ,
85
66
onRejected ?: ( a : Error ) => R | PromiseLike < R >
86
67
) : Promise < T | R > {
87
68
return this . _taskCompletionResolver . promise . then ( onFulfilled , onRejected ) ;
@@ -91,7 +72,7 @@ export class LoadBundleTask
91
72
* Notifies all observers that bundle loading has completed, with a provided
92
73
* `LoadBundleTaskProgress` object.
93
74
*/
94
- _completeWith ( progress : ApiLoadBundleTaskProgress ) : void {
75
+ _completeWith ( progress : LoadBundleTaskProgress ) : void {
95
76
debugAssert (
96
77
progress . taskState === 'Success' ,
97
78
'Task is not completed with Success.'
@@ -126,7 +107,7 @@ export class LoadBundleTask
126
107
* Notifies a progress update of loading a bundle.
127
108
* @param progress - The new progress.
128
109
*/
129
- _updateProgress ( progress : ApiLoadBundleTaskProgress ) : void {
110
+ _updateProgress ( progress : LoadBundleTaskProgress ) : void {
130
111
debugAssert (
131
112
this . _lastProgress . taskState === 'Running' ,
132
113
'Cannot update progress on a completed or failed task'
@@ -138,3 +119,30 @@ export class LoadBundleTask
138
119
}
139
120
}
140
121
}
122
+
123
+ export function loadBundle (
124
+ db : Firestore ,
125
+ bundleData : ArrayBuffer | ReadableStream < Uint8Array > | string
126
+ ) : LoadBundleTask {
127
+ const resultTask = new LoadBundleTask ( ) ;
128
+ firestoreClientLoadBundle (
129
+ ensureFirestoreConfigured ( db . _delegate ) ,
130
+ db . _databaseId ,
131
+ bundleData ,
132
+ resultTask
133
+ ) ;
134
+ return resultTask ;
135
+ }
136
+
137
+ export function namedQuery ( db : Firestore , name : string ) : Promise < Query | null > {
138
+ return firestoreClientGetNamedQuery (
139
+ ensureFirestoreConfigured ( db . _delegate ) ,
140
+ name
141
+ ) . then ( namedQuery => {
142
+ if ( ! namedQuery ) {
143
+ return null ;
144
+ }
145
+
146
+ return new Query ( db , new ExpQuery ( db . _delegate , null , namedQuery . query ) ) ;
147
+ } ) ;
148
+ }
0 commit comments