18
18
import * as firestore from '../../index' ;
19
19
20
20
import { _getProvider , _removeServiceInstance } from '@firebase/app-exp' ;
21
- import { FirebaseApp , _FirebaseService } from '@firebase/app-types-exp' ;
21
+ import { _FirebaseService , FirebaseApp } from '@firebase/app-types-exp' ;
22
22
import { Provider } from '@firebase/component' ;
23
23
24
24
import { FirebaseAuthInternalName } from '@firebase/auth-interop-types' ;
25
- import {
26
- FirestoreClient ,
27
- PersistenceSettings
28
- } from '../../../src/core/firestore_client' ;
29
25
import { AsyncQueue } from '../../../src/util/async_queue' ;
30
26
import {
31
- ComponentProvider ,
32
27
IndexedDbComponentProvider ,
33
- MemoryComponentProvider ,
34
28
MultiTabIndexedDbComponentProvider
35
29
} from '../../../src/core/component_provider' ;
36
30
37
- import {
38
- DEFAULT_FORCE_LONG_POLLING ,
39
- DEFAULT_HOST ,
40
- DEFAULT_SSL ,
41
- Firestore as LiteFirestore
42
- } from '../../../lite/src/api/database' ;
31
+ import { Firestore as LiteFirestore } from '../../../lite/src/api/database' ;
43
32
import { cast } from '../../../lite/src/api/util' ;
44
33
import { Code , FirestoreError } from '../../../src/util/error' ;
45
34
import { Deferred } from '../../../src/util/promise' ;
46
35
import { LruParams } from '../../../src/local/lru_garbage_collector' ;
47
36
import { CACHE_SIZE_UNLIMITED } from '../../../src/api/database' ;
48
- import { DatabaseId , DatabaseInfo } from '../../../src/core/database_info' ;
37
+ import { DatabaseId } from '../../../src/core/database_info' ;
49
38
import {
50
- indexedDbStoragePrefix ,
51
- indexedDbClearPersistence
39
+ indexedDbClearPersistence ,
40
+ indexedDbStoragePrefix
52
41
} from '../../../src/local/indexeddb_persistence' ;
42
+ import {
43
+ getFirestoreClient ,
44
+ hasFirestoreClient ,
45
+ initializeFirestoreClient ,
46
+ removeFirestoreClient
47
+ } from './components' ;
53
48
54
49
/**
55
50
* The root reference to the Firestore database and the entry point for the
56
51
* tree-shakeable SDK.
57
52
*/
58
53
export class Firestore extends LiteFirestore
59
54
implements firestore . FirebaseFirestore , _FirebaseService {
60
- private readonly _queue = new AsyncQueue ( ) ;
61
- private readonly _firestoreClient : FirestoreClient ;
62
- private readonly _persistenceKey : string ;
63
- private _componentProvider : ComponentProvider = new MemoryComponentProvider ( ) ;
55
+ readonly _queue = new AsyncQueue ( ) ;
56
+ readonly _persistenceKey : string ;
64
57
65
- // Assigned via _getFirestoreClient()
66
- private _deferredInitialization ?: Promise < void > ;
67
-
68
- protected _persistenceSettings : PersistenceSettings = { durable : false } ;
69
58
// We override the Settings property of the Lite SDK since the full Firestore
70
59
// SDK supports more settings.
71
60
protected _settings ?: firestore . Settings ;
72
61
73
- private _terminated : boolean = false ;
74
-
75
62
constructor (
76
63
app : FirebaseApp ,
77
64
authProvider : Provider < FirebaseAuthInternalName >
78
65
) {
79
66
super ( app , authProvider ) ;
80
67
this . _persistenceKey = app . name ;
81
- this . _firestoreClient = new FirestoreClient ( this . _credentials , this . _queue ) ;
82
68
}
83
69
84
70
_getSettings ( ) : firestore . Settings {
@@ -88,65 +74,6 @@ export class Firestore extends LiteFirestore
88
74
return this . _settings ;
89
75
}
90
76
91
- _getFirestoreClient ( ) : Promise < FirestoreClient > {
92
- if ( this . _terminated ) {
93
- throw new FirestoreError (
94
- Code . FAILED_PRECONDITION ,
95
- 'The client has already been terminated.'
96
- ) ;
97
- }
98
-
99
- if ( ! this . _deferredInitialization ) {
100
- const settings = this . _getSettings ( ) ;
101
- const databaseInfo = this . _makeDatabaseInfo (
102
- settings . host ,
103
- settings . ssl ,
104
- settings . experimentalForceLongPolling
105
- ) ;
106
-
107
- this . _deferredInitialization = this . _firestoreClient . start (
108
- databaseInfo ,
109
- this . _componentProvider ,
110
- this . _persistenceSettings
111
- ) ;
112
- }
113
-
114
- return this . _deferredInitialization . then ( ( ) => this . _firestoreClient ) ;
115
- }
116
-
117
- // TODO(firestorexp): Factor out MultiTabComponentProvider and remove
118
- // `synchronizeTabs` argument
119
- _enablePersistence (
120
- persistenceProvider : ComponentProvider ,
121
- synchronizeTabs : boolean
122
- ) : Promise < void > {
123
- if ( this . _deferredInitialization ) {
124
- throw new FirestoreError (
125
- Code . FAILED_PRECONDITION ,
126
- 'Firestore has already been started and persistence can no longer ' +
127
- 'be enabled. You can only enable persistence before calling ' +
128
- 'any other methods on a Firestore object.'
129
- ) ;
130
- }
131
-
132
- const settings = this . _getSettings ( ) ;
133
- this . _persistenceSettings = {
134
- durable : true ,
135
- synchronizeTabs,
136
- forceOwningTab : false ,
137
- cacheSizeBytes :
138
- settings . cacheSizeBytes ?? LruParams . DEFAULT_CACHE_SIZE_BYTES
139
- } ;
140
- this . _componentProvider = persistenceProvider ;
141
-
142
- // TODO(firestorexp): Add support for Persistence fallback
143
- return this . _getFirestoreClient ( ) . then ( ( ) => { } ) ;
144
- }
145
-
146
- delete ( ) : Promise < void > {
147
- return terminate ( this ) ;
148
- }
149
-
150
77
/**
151
78
* Verifies that the client is not running and clears persistence by invoking
152
79
* `delegate` on the async queue.
@@ -157,7 +84,7 @@ export class Firestore extends LiteFirestore
157
84
_clearPersistence (
158
85
delegate : ( databaseId : DatabaseId , persistenceKey : string ) => Promise < void >
159
86
) : Promise < void > {
160
- if ( this . _deferredInitialization !== undefined && ! this . _terminated ) {
87
+ if ( hasFirestoreClient ( this ) ) {
161
88
throw new FirestoreError (
162
89
Code . FAILED_PRECONDITION ,
163
90
'Persistence can only be cleared before a Firestore instance is ' +
@@ -177,28 +104,9 @@ export class Firestore extends LiteFirestore
177
104
return deferred . promise ;
178
105
}
179
106
180
- protected _makeDatabaseInfo (
181
- host ?: string ,
182
- ssl ?: boolean ,
183
- forceLongPolling ?: boolean
184
- ) : DatabaseInfo {
185
- return new DatabaseInfo (
186
- this . _databaseId ,
187
- this . _persistenceKey ,
188
- host ?? DEFAULT_HOST ,
189
- ssl ?? DEFAULT_SSL ,
190
- forceLongPolling ?? DEFAULT_FORCE_LONG_POLLING
191
- ) ;
192
- }
193
-
194
- _terminate ( ) : Promise < void > {
195
- this . _terminated = true ;
196
- if ( this . _deferredInitialization ) {
197
- return this . _deferredInitialization . then ( ( ) =>
198
- this . _firestoreClient . terminate ( )
199
- ) ;
200
- }
201
- return Promise . resolve ( ) ;
107
+ async _terminate ( ) : Promise < void > {
108
+ await super . _terminate ( ) ;
109
+ await removeFirestoreClient ( this ) ;
202
110
}
203
111
}
204
112
@@ -234,20 +142,33 @@ export function enableIndexedDbPersistence(
234
142
firestore : firestore . FirebaseFirestore
235
143
) : Promise < void > {
236
144
const firestoreImpl = cast ( firestore , Firestore ) ;
237
- return firestoreImpl . _enablePersistence (
145
+ const settings = firestoreImpl . _getSettings ( ) ;
146
+ return initializeFirestoreClient (
147
+ firestoreImpl ,
238
148
new IndexedDbComponentProvider ( ) ,
239
- /*synchronizeTabs=*/ false
149
+ {
150
+ durable : true ,
151
+ cacheSizeBytes :
152
+ settings . cacheSizeBytes || LruParams . DEFAULT_CACHE_SIZE_BYTES
153
+ }
240
154
) ;
241
155
}
242
156
243
157
export function enableMultiTabIndexedDbPersistence (
244
158
firestore : firestore . FirebaseFirestore
245
159
) : Promise < void > {
246
160
const firestoreImpl = cast ( firestore , Firestore ) ;
247
- return firestoreImpl . _enablePersistence (
161
+ const settings = firestoreImpl . _getSettings ( ) ;
162
+ return initializeFirestoreClient (
163
+ firestoreImpl ,
248
164
new MultiTabIndexedDbComponentProvider ( ) ,
249
- /*synchronizeTabs=*/ false
250
- ) ;
165
+ {
166
+ durable : true ,
167
+ synchronizeTabs : true ,
168
+ cacheSizeBytes :
169
+ settings . cacheSizeBytes || LruParams . DEFAULT_CACHE_SIZE_BYTES
170
+ }
171
+ ) . then ( ( ) => { } ) ;
251
172
}
252
173
253
174
export function clearIndexedDbPersistence (
@@ -265,33 +186,33 @@ export function waitForPendingWrites(
265
186
firestore : firestore . FirebaseFirestore
266
187
) : Promise < void > {
267
188
const firestoreImpl = cast ( firestore , Firestore ) ;
268
- return firestoreImpl
269
- . _getFirestoreClient ( )
270
- . then ( firestoreClient => firestoreClient . waitForPendingWrites ( ) ) ;
189
+ return getFirestoreClient ( firestoreImpl ) . then ( firestoreClient =>
190
+ firestoreClient . waitForPendingWrites ( )
191
+ ) ;
271
192
}
272
193
273
194
export function enableNetwork (
274
195
firestore : firestore . FirebaseFirestore
275
196
) : Promise < void > {
276
197
const firestoreImpl = cast ( firestore , Firestore ) ;
277
- return firestoreImpl
278
- . _getFirestoreClient ( )
279
- . then ( firestoreClient => firestoreClient . enableNetwork ( ) ) ;
198
+ return getFirestoreClient ( firestoreImpl ) . then ( firestoreClient =>
199
+ firestoreClient . enableNetwork ( )
200
+ ) ;
280
201
}
281
202
282
203
export function disableNetwork (
283
204
firestore : firestore . FirebaseFirestore
284
205
) : Promise < void > {
285
206
const firestoreImpl = cast ( firestore , Firestore ) ;
286
- return firestoreImpl
287
- . _getFirestoreClient ( )
288
- . then ( firestoreClient => firestoreClient . disableNetwork ( ) ) ;
207
+ return getFirestoreClient ( firestoreImpl ) . then ( firestoreClient =>
208
+ firestoreClient . disableNetwork ( )
209
+ ) ;
289
210
}
290
211
291
212
export function terminate (
292
213
firestore : firestore . FirebaseFirestore
293
214
) : Promise < void > {
294
215
_removeServiceInstance ( firestore . app , 'firestore/lite' ) ;
295
216
const firestoreImpl = cast ( firestore , Firestore ) ;
296
- return firestoreImpl . _terminate ( ) ;
217
+ return firestoreImpl . delete ( ) ;
297
218
}
0 commit comments