@@ -28,11 +28,20 @@ import {
28
28
OnlineComponentProvider
29
29
} from '../../../src/core/component_provider' ;
30
30
import { DEFAULT_HOST , DEFAULT_SSL } from '../../../lite/src/api/components' ;
31
+ import { LocalStore } from '../../../src/local/local_store' ;
32
+ import { Deferred } from '../../../src/util/promise' ;
31
33
32
34
// The components module manages the lifetime of dependencies of the Firestore
33
35
// client. Dependencies can be lazily constructed and only one exists per
34
36
// Firestore instance.
35
37
38
+ // TODO: These should be promises too
39
+ const offlineComponentProviders = new Map <
40
+ Firestore ,
41
+ OfflineComponentProvider
42
+ > ( ) ;
43
+ const onlineComponentProviders = new Map < Firestore , OnlineComponentProvider > ( ) ;
44
+
36
45
/**
37
46
* An instance map that ensures only one FirestoreClient exists per Firestore
38
47
* instance.
@@ -56,18 +65,58 @@ export function getFirestoreClient(
56
65
}
57
66
if ( ! firestoreClientInstances . has ( firestore ) ) {
58
67
// eslint-disable-next-line @typescript-eslint/no-floating-promises
59
- initializeFirestoreClient (
60
- firestore ,
61
- new MemoryOfflineComponentProvider ( ) ,
62
- new OnlineComponentProvider ( ) ,
63
- {
64
- durable : false
65
- }
66
- ) ;
68
+ initializeFirestoreClient ( firestore , {
69
+ durable : false
70
+ } ) ;
67
71
}
68
72
return firestoreClientInstances . get ( firestore ) ! ;
69
73
}
70
74
75
+ async function getOfflineComponentProvider (
76
+ firestore : Firestore
77
+ ) : Promise < OfflineComponentProvider > {
78
+ let offlineComponentProvider = offlineComponentProviders . get ( firestore ) ;
79
+ if ( ! offlineComponentProvider ) {
80
+ offlineComponentProvider = new MemoryOfflineComponentProvider ( ) ;
81
+ offlineComponentProviders . set ( firestore , offlineComponentProvider ) ;
82
+ const componentConfiguration = {
83
+ asyncQueue : firestore . _queue ,
84
+ databaseInfo : firestore . _databaseId ,
85
+ clientId : null as any ,
86
+ credentials : null as any ,
87
+ initialUser : null as any ,
88
+ maxConcurrentLimboResolutions : null as any ,
89
+ persistenceSettings : null as any
90
+ } ;
91
+ await offlineComponentProvider . initialize ( componentConfiguration as any ) ;
92
+ }
93
+ return offlineComponentProvider ;
94
+ }
95
+
96
+ async function getOnlineComponentProvider (
97
+ firestore : Firestore
98
+ ) : Promise < OnlineComponentProvider > {
99
+ let onlineComponentProvider = onlineComponentProviders . get ( firestore ) ;
100
+ if ( ! onlineComponentProvider ) {
101
+ onlineComponentProvider = new OnlineComponentProvider ( ) ;
102
+ onlineComponentProviders . set ( firestore , onlineComponentProvider ) ;
103
+ const componentConfiguration = {
104
+ asyncQueue : firestore . _queue ,
105
+ databaseInfo : firestore . _databaseId ,
106
+ clientId : null as any ,
107
+ credentials : null as any ,
108
+ initialUser : null as any ,
109
+ maxConcurrentLimboResolutions : null as any ,
110
+ persistenceSettings : null as any
111
+ } ;
112
+ await onlineComponentProvider . initialize (
113
+ await getOfflineComponentProvider ( firestore ) ,
114
+ componentConfiguration as any
115
+ ) ;
116
+ }
117
+ return onlineComponentProvider ;
118
+ }
119
+
71
120
/**
72
121
* Creates a new FirestoreClient for the given Firestore instance. Throws if the
73
122
* instance exists.
@@ -79,8 +128,6 @@ export function getFirestoreClient(
79
128
*/
80
129
export function initializeFirestoreClient (
81
130
firestore : Firestore ,
82
- offlineComponentProvider : OfflineComponentProvider ,
83
- onlineComponentProvider : OnlineComponentProvider ,
84
131
persistenceSettings : PersistenceSettings
85
132
) : Promise < void > {
86
133
if ( firestore . _initialized ) {
@@ -104,17 +151,23 @@ export function initializeFirestoreClient(
104
151
firestore . _credentials ,
105
152
firestore . _queue
106
153
) ;
107
- const initializationPromise = firestoreClient . start (
108
- databaseInfo ,
109
- offlineComponentProvider ,
110
- onlineComponentProvider ,
111
- persistenceSettings
112
- ) ;
113
- firestoreClientInstances . set (
114
- firestore ,
115
- initializationPromise . then ( ( ) => firestoreClient )
116
- ) ;
117
- return initializationPromise ;
154
+ const initializationDeferred = new Deferred < FirestoreClient > ( ) ;
155
+ firestoreClientInstances . set ( firestore , initializationDeferred . promise ) ;
156
+
157
+ Promise . all ( [
158
+ getOfflineComponentProvider ( firestore ) ,
159
+ getOnlineComponentProvider ( firestore )
160
+ ] ) . then ( ( [ offlineComponentProvider , onlineComponentProvider ] ) => {
161
+ firestoreClient
162
+ . start (
163
+ databaseInfo ,
164
+ offlineComponentProvider ,
165
+ onlineComponentProvider ,
166
+ persistenceSettings
167
+ )
168
+ . then ( ( ) => initializationDeferred . resolve ( firestoreClient ) ) ;
169
+ } ) ;
170
+ return initializationDeferred . promise . then ( ( ) => { } ) ;
118
171
}
119
172
120
173
/**
@@ -130,3 +183,39 @@ export async function removeFirestoreClient(
130
183
return firestoreClient . terminate ( ) ;
131
184
}
132
185
}
186
+
187
+ export async function setComponentProviders (
188
+ firestore : Firestore ,
189
+ offlineComponentProvider : OfflineComponentProvider ,
190
+ onlineComponentProvider : OnlineComponentProvider
191
+ ) : Promise < void > {
192
+ // TODO: Move this to Firestore
193
+ const componentConfiguration = {
194
+ asyncQueue : firestore . _queue ,
195
+ databaseInfo : firestore . _databaseId ,
196
+ clientId : null as any ,
197
+ credentials : null as any ,
198
+ initialUser : null as any ,
199
+ maxConcurrentLimboResolutions : null as any ,
200
+ persistenceSettings : null as any
201
+ } ;
202
+ await offlineComponentProvider . initialize ( componentConfiguration as any ) ;
203
+ await onlineComponentProvider . initialize (
204
+ offlineComponentProvider ,
205
+ componentConfiguration as any
206
+ ) ;
207
+ offlineComponentProviders . set ( firestore , offlineComponentProvider ) ;
208
+ onlineComponentProviders . set ( firestore , onlineComponentProvider ) ;
209
+ }
210
+
211
+ export function getLocalStore ( firestore : Firestore ) : Promise < LocalStore > {
212
+ if ( firestore . _terminated ) {
213
+ throw new FirestoreError (
214
+ Code . FAILED_PRECONDITION ,
215
+ 'The client has already been terminated.'
216
+ ) ;
217
+ }
218
+ return getOfflineComponentProvider ( firestore ) . then (
219
+ provider => provider . localStore
220
+ ) ;
221
+ }
0 commit comments