@@ -93,12 +93,13 @@ export class FirestoreClient {
93
93
// undefined checks.
94
94
private databaseInfo ! : DatabaseInfo ;
95
95
private user = User . UNAUTHENTICATED ;
96
- private readonly clientId = AutoId . newId ( ) ;
97
96
private credentialListener : CredentialChangeListener = ( ) => { } ;
98
97
99
98
offlineComponents ?: OfflineComponentProvider ;
100
99
onlineComponents ?: OnlineComponentProvider ;
101
100
101
+ private readonly clientId = AutoId . newId ( ) ;
102
+
102
103
// We defer our initialization until we get the current user from
103
104
// setChangeListener(). We block the async queue until we got the initial
104
105
// user and the initialization is completed. This will prevent any scheduled
@@ -154,18 +155,13 @@ export class FirestoreClient {
154
155
start ( databaseInfo : DatabaseInfo ) : void {
155
156
this . databaseInfo = databaseInfo ;
156
157
157
- let initialized = false ;
158
158
this . credentials . setChangeListener ( user => {
159
- if ( ! initialized ) {
160
- initialized = true ;
161
-
162
- logDebug ( LOG_TAG , 'Initializing. user=' , user . uid ) ;
163
- this . initializationDone . resolve ( ) ;
164
- }
165
- if ( ! user . isEqual ( this . user ) ) {
159
+ logDebug ( LOG_TAG , 'Received user=' , user . uid ) ;
160
+ if ( ! this . user . isEqual ( user ) ) {
166
161
this . user = user ;
167
162
this . credentialListener ( user ) ;
168
163
}
164
+ this . initializationDone . resolve ( ) ;
169
165
} ) ;
170
166
171
167
// Block the async queue until initialization is done
@@ -186,38 +182,24 @@ export class FirestoreClient {
186
182
}
187
183
188
184
setCredentialChangeListener ( listener : ( user : User ) => void ) : void {
189
- logDebug ( 'FirebaseFirestore' , 'Registering credential change listener' ) ;
190
185
this . credentialListener = listener ;
191
186
// eslint-disable-next-line @typescript-eslint/no-floating-promises
192
187
this . initializationDone . promise . then ( ( ) =>
193
188
this . credentialListener ( this . user )
194
189
) ;
195
190
}
196
191
197
- /**
198
- * Checks that the client has not been terminated. Ensures that other methods on
199
- * this class cannot be called after the client is terminated.
200
- */
201
- verifyNotTerminated ( ) : void {
202
- if ( this . asyncQueue . isShuttingDown ) {
203
- throw new FirestoreError (
204
- Code . FAILED_PRECONDITION ,
205
- 'The client has already been terminated.'
206
- ) ;
207
- }
208
- }
209
-
210
- databaseId ( ) : DatabaseId {
211
- return this . databaseInfo . databaseId ;
212
- }
213
-
214
192
terminate ( ) : Promise < void > {
215
193
this . asyncQueue . enterRestrictedMode ( ) ;
216
194
const deferred = new Deferred ( ) ;
217
195
this . asyncQueue . enqueueAndForgetEvenWhileRestricted ( async ( ) => {
218
196
try {
219
- await this . onlineComponents ?. terminate ( ) ;
220
- await this . offlineComponents ?. terminate ( ) ;
197
+ if ( this . onlineComponents ) {
198
+ await this . onlineComponents . terminate ( ) ;
199
+ }
200
+ if ( this . offlineComponents ) {
201
+ await this . offlineComponents . terminate ( ) ;
202
+ }
221
203
222
204
// `removeChangeListener` must be called after shutting down the
223
205
// RemoteStore as it will prevent the RemoteStore from retrieving
@@ -236,12 +218,9 @@ export class FirestoreClient {
236
218
}
237
219
}
238
220
239
- // TODO(firestore-compat): Remove `export` once compat migration is complete.
240
- export async function ensureOfflineComponents (
221
+ async function ensureOfflineComponents (
241
222
firestoreClient : FirestoreClient
242
223
) : Promise < OfflineComponentProvider > {
243
- firestoreClient . asyncQueue . verifyOperationInProgress ( ) ;
244
-
245
224
if ( ! firestoreClient . offlineComponents ) {
246
225
logDebug ( LOG_TAG , 'Using default OfflineComponentProvider' ) ;
247
226
await setOfflineComponentProvider (
@@ -253,12 +232,9 @@ export async function ensureOfflineComponents(
253
232
return firestoreClient . offlineComponents ! ;
254
233
}
255
234
256
- // TODO(firestore-compat): Remove `export` once compat migration is complete.
257
- export async function ensureOnlineComponents (
235
+ async function ensureOnlineComponents (
258
236
firestoreClient : FirestoreClient
259
237
) : Promise < OnlineComponentProvider > {
260
- firestoreClient . asyncQueue . verifyOperationInProgress ( ) ;
261
-
262
238
if ( ! firestoreClient . onlineComponents ) {
263
239
logDebug ( LOG_TAG , 'Using default OnlineComponentProvider' ) ;
264
240
await setOnlineComponentProvider (
@@ -332,7 +308,6 @@ export function getPersistence(
332
308
export async function firestoreClientEnableNetwork (
333
309
firestoreClient : FirestoreClient
334
310
) : Promise < void > {
335
- firestoreClient . verifyNotTerminated ( ) ;
336
311
return firestoreClient . asyncQueue . enqueue ( async ( ) => {
337
312
const persistence = await getPersistence ( firestoreClient ) ;
338
313
const remoteStore = await getRemoteStore ( firestoreClient ) ;
@@ -351,7 +326,6 @@ export function getRemoteStore(
351
326
export async function firestoreClientDisableNetwork (
352
327
firestoreClient : FirestoreClient
353
328
) : Promise < void > {
354
- firestoreClient . verifyNotTerminated ( ) ;
355
329
return firestoreClient . asyncQueue . enqueue ( async ( ) => {
356
330
const persistence = await getPersistence ( firestoreClient ) ;
357
331
const remoteStore = await getRemoteStore ( firestoreClient ) ;
@@ -374,8 +348,6 @@ export function getSyncEngine(
374
348
export async function firestoreClientWaitForPendingWrites (
375
349
firestoreClient : FirestoreClient
376
350
) : Promise < void > {
377
- firestoreClient . verifyNotTerminated ( ) ;
378
-
379
351
const deferred = new Deferred < void > ( ) ;
380
352
firestoreClient . asyncQueue . enqueueAndForget ( async ( ) => {
381
353
const syncEngine = await getSyncEngine ( firestoreClient ) ;
@@ -406,7 +378,6 @@ export function firestoreClientListen(
406
378
options : ListenOptions ,
407
379
observer : Partial < Observer < ViewSnapshot > >
408
380
) : ( ) => void {
409
- firestoreClient . verifyNotTerminated ( ) ;
410
381
const wrappedObserver = new AsyncObserver ( observer ) ;
411
382
const listener = new QueryListener ( query , wrappedObserver , options ) ;
412
383
firestoreClient . asyncQueue . enqueueAndForget ( async ( ) => {
@@ -426,7 +397,6 @@ export function firestoreClientGetDocumentFromLocalCache(
426
397
firestoreClient : FirestoreClient ,
427
398
docKey : DocumentKey
428
399
) : Promise < Document | null > {
429
- firestoreClient . verifyNotTerminated ( ) ;
430
400
const deferred = new Deferred < Document | null > ( ) ;
431
401
firestoreClient . asyncQueue . enqueueAndForget ( async ( ) => {
432
402
const localStore = await getLocalStore ( firestoreClient ) ;
@@ -440,7 +410,6 @@ export function firestoreClientGetDocumentViaSnapshotListener(
440
410
key : DocumentKey ,
441
411
options : GetOptions = { }
442
412
) : Promise < ViewSnapshot > {
443
- firestoreClient . verifyNotTerminated ( ) ;
444
413
const deferred = new Deferred < ViewSnapshot > ( ) ;
445
414
firestoreClient . asyncQueue . enqueueAndForget ( async ( ) => {
446
415
const eventManager = await getEventManager ( firestoreClient ) ;
@@ -456,14 +425,13 @@ export function firestoreClientGetDocumentViaSnapshotListener(
456
425
}
457
426
458
427
export async function getLocalStore ( firestoreClient : FirestoreClient ) {
459
- return ( await ensureOfflineComponents ( firestoreClient ) ) . localStore ;
428
+ return ensureOfflineComponents ( firestoreClient ) . then ( c => c . localStore ) ;
460
429
}
461
430
462
431
export function firestoreClientGetDocumentsFromLocalCache (
463
432
firestoreClient : FirestoreClient ,
464
433
query : Query
465
434
) : Promise < ViewSnapshot > {
466
- firestoreClient . verifyNotTerminated ( ) ;
467
435
const deferred = new Deferred < ViewSnapshot > ( ) ;
468
436
firestoreClient . asyncQueue . enqueueAndForget ( async ( ) => {
469
437
const localStore = await getLocalStore ( firestoreClient ) ;
@@ -477,7 +445,6 @@ export function firestoreClientGetDocumentsViaSnapshotListener(
477
445
query : Query ,
478
446
options : GetOptions = { }
479
447
) : Promise < ViewSnapshot > {
480
- firestoreClient . verifyNotTerminated ( ) ;
481
448
const deferred = new Deferred < ViewSnapshot > ( ) ;
482
449
firestoreClient . asyncQueue . enqueueAndForget ( async ( ) => {
483
450
const eventManager = await getEventManager ( firestoreClient ) ;
@@ -496,7 +463,6 @@ export function firestoreClientWrite(
496
463
firestoreClient : FirestoreClient ,
497
464
mutations : Mutation [ ]
498
465
) : Promise < void > {
499
- firestoreClient . verifyNotTerminated ( ) ;
500
466
const deferred = new Deferred < void > ( ) ;
501
467
firestoreClient . asyncQueue . enqueueAndForget ( async ( ) => {
502
468
const syncEngine = await getSyncEngine ( firestoreClient ) ;
@@ -509,7 +475,6 @@ export function firestoreClientAddSnapshotsInSyncListener(
509
475
firestoreClient : FirestoreClient ,
510
476
observer : Partial < Observer < void > >
511
477
) : ( ) => void {
512
- firestoreClient . verifyNotTerminated ( ) ;
513
478
const wrappedObserver = new AsyncObserver ( observer ) ;
514
479
firestoreClient . asyncQueue . enqueueAndForget ( async ( ) => {
515
480
const eventManager = await getEventManager ( firestoreClient ) ;
@@ -524,10 +489,10 @@ export function firestoreClientAddSnapshotsInSyncListener(
524
489
} ;
525
490
}
526
491
527
- export async function getDatastore (
492
+ export function getDatastore (
528
493
firestoreClient : FirestoreClient
529
494
) : Promise < Datastore > {
530
- return ( await ensureOnlineComponents ( firestoreClient ) ) . datastore ;
495
+ return ensureOnlineComponents ( firestoreClient ) . then ( c => c . datastore ) ;
531
496
}
532
497
533
498
/**
@@ -549,7 +514,6 @@ export function firestoreClientTransaction<T>(
549
514
firestoreClient : FirestoreClient ,
550
515
updateFunction : ( transaction : Transaction ) => Promise < T >
551
516
) : Promise < T > {
552
- firestoreClient . verifyNotTerminated ( ) ;
553
517
const deferred = new Deferred < T > ( ) ;
554
518
firestoreClient . asyncQueue . enqueueAndForget ( async ( ) => {
555
519
const datastore = await getDatastore ( firestoreClient ) ;
0 commit comments