@@ -93,6 +93,15 @@ export class FirestoreClient {
93
93
94
94
private readonly clientId = AutoId . newId ( ) ;
95
95
96
+ // We defer our initialization until we get the current user from
97
+ // setChangeListener(). We block the async queue until we got the initial
98
+ // user and the initialization is completed. This will prevent any scheduled
99
+ // work from happening before initialization is completed.
100
+ //
101
+ // If initializationDone resolved then the FirestoreClient is in a usable
102
+ // state.
103
+ private readonly initializationDone = new Deferred < void > ( ) ;
104
+
96
105
constructor (
97
106
private credentials : CredentialsProvider ,
98
107
/**
@@ -156,15 +165,6 @@ export class FirestoreClient {
156
165
157
166
this . databaseInfo = databaseInfo ;
158
167
159
- // We defer our initialization until we get the current user from
160
- // setChangeListener(). We block the async queue until we got the initial
161
- // user and the initialization is completed. This will prevent any scheduled
162
- // work from happening before initialization is completed.
163
- //
164
- // If initializationDone resolved then the FirestoreClient is in a usable
165
- // state.
166
- const initializationDone = new Deferred < void > ( ) ;
167
-
168
168
// If usePersistence is true, certain classes of errors while starting are
169
169
// recoverable but only by falling back to persistence disabled.
170
170
//
@@ -186,7 +186,7 @@ export class FirestoreClient {
186
186
persistenceSettings ,
187
187
user ,
188
188
persistenceResult
189
- ) . then ( initializationDone . resolve , initializationDone . reject ) ;
189
+ ) . then ( this . initializationDone . resolve , this . initializationDone . reject ) ;
190
190
} else {
191
191
this . asyncQueue . enqueueRetryable ( ( ) =>
192
192
this . remoteStore . handleCredentialChange ( user )
@@ -196,7 +196,7 @@ export class FirestoreClient {
196
196
197
197
// Block the async queue until initialization is done
198
198
this . asyncQueue . enqueueAndForget ( ( ) => {
199
- return initializationDone . promise ;
199
+ return this . initializationDone . promise ;
200
200
} ) ;
201
201
202
202
// Return only the result of enabling persistence. Note that this does not
@@ -409,20 +409,22 @@ export class FirestoreClient {
409
409
docKey : DocumentKey
410
410
) : Promise < Document | null > {
411
411
this . verifyNotTerminated ( ) ;
412
- const deferred = new Deferred < Document | null > ( ) ;
413
- await this . asyncQueue . enqueue ( ( ) =>
414
- readDocument ( this . localStore , docKey , deferred )
412
+ await this . initializationDone ;
413
+ return enqueueReadDocumentFromLocalCache (
414
+ this . asyncQueue ,
415
+ this . localStore ,
416
+ docKey
415
417
) ;
416
- return deferred . promise ;
417
418
}
418
419
419
420
async getDocumentsFromLocalCache ( query : Query ) : Promise < ViewSnapshot > {
420
421
this . verifyNotTerminated ( ) ;
421
- const deferred = new Deferred < ViewSnapshot > ( ) ;
422
- await this . asyncQueue . enqueue ( ( ) =>
423
- executeQuery ( this . localStore , query , deferred )
422
+ await this . initializationDone ;
423
+ return enqueueExecuteQueryFromLocalCache (
424
+ this . asyncQueue ,
425
+ this . localStore ,
426
+ query
424
427
) ;
425
- return deferred . promise ;
426
428
}
427
429
428
430
write ( mutations : Mutation [ ] ) : Promise < void > {
@@ -538,79 +540,67 @@ export function enqueueSnapshotsInSyncListen(
538
540
} ;
539
541
}
540
542
541
- async function readDocument (
542
- localStore : LocalStore ,
543
- docKey : DocumentKey ,
544
- deferred : Deferred < Document | null >
545
- ) : Promise < void > {
546
- try {
547
- const maybeDoc = await localStore . readDocument ( docKey ) ;
548
- if ( maybeDoc instanceof Document ) {
549
- deferred . resolve ( maybeDoc ) ;
550
- } else if ( maybeDoc instanceof NoDocument ) {
551
- deferred . resolve ( null ) ;
552
- } else {
553
- deferred . reject (
554
- new FirestoreError (
555
- Code . UNAVAILABLE ,
556
- 'Failed to get document from cache. (However, this document may ' +
557
- "exist on the server. Run again without setting 'source' in " +
558
- 'the GetOptions to attempt to retrieve the document from the ' +
559
- 'server.)'
560
- )
561
- ) ;
562
- }
563
- } catch ( e ) {
564
- const firestoreError = wrapInUserErrorIfRecoverable (
565
- e ,
566
- `Failed to get document '${ docKey } from cache`
567
- ) ;
568
- deferred . reject ( firestoreError ) ;
569
- }
570
- }
571
-
572
- export async function enqueueReadDocument (
543
+ export async function enqueueReadDocumentFromLocalCache (
573
544
asyncQueue : AsyncQueue ,
574
545
localStore : LocalStore ,
575
546
docKey : DocumentKey
576
547
) : Promise < Document | null > {
577
548
const deferred = new Deferred < Document | null > ( ) ;
578
- await asyncQueue . enqueue ( ( ) => readDocument ( localStore , docKey , deferred ) ) ;
549
+ await asyncQueue . enqueue ( async ( ) => {
550
+ try {
551
+ const maybeDoc = await localStore . readDocument ( docKey ) ;
552
+ if ( maybeDoc instanceof Document ) {
553
+ deferred . resolve ( maybeDoc ) ;
554
+ } else if ( maybeDoc instanceof NoDocument ) {
555
+ deferred . resolve ( null ) ;
556
+ } else {
557
+ deferred . reject (
558
+ new FirestoreError (
559
+ Code . UNAVAILABLE ,
560
+ 'Failed to get document from cache. (However, this document may ' +
561
+ "exist on the server. Run again without setting 'source' in " +
562
+ 'the GetOptions to attempt to retrieve the document from the ' +
563
+ 'server.)'
564
+ )
565
+ ) ;
566
+ }
567
+ } catch ( e ) {
568
+ const firestoreError = wrapInUserErrorIfRecoverable (
569
+ e ,
570
+ `Failed to get document '${ docKey } from cache`
571
+ ) ;
572
+ deferred . reject ( firestoreError ) ;
573
+ }
574
+ } ) ;
579
575
return deferred . promise ;
580
576
}
581
577
582
- async function executeQuery (
583
- localStore : LocalStore ,
584
- query : Query ,
585
- deferred : Deferred < ViewSnapshot >
586
- ) : Promise < void > {
587
- try {
588
- const queryResult = await localStore . executeQuery (
589
- query ,
590
- /* usePreviousResults= */ true
591
- ) ;
592
- const view = new View ( query , queryResult . remoteKeys ) ;
593
- const viewDocChanges = view . computeDocChanges ( queryResult . documents ) ;
594
- const viewChange = view . applyChanges (
595
- viewDocChanges ,
596
- /* updateLimboDocuments= */ false
597
- ) ;
598
- deferred . resolve ( viewChange . snapshot ! ) ;
599
- } catch ( e ) {
600
- const firestoreError = wrapInUserErrorIfRecoverable (
601
- e ,
602
- `Failed to execute query '${ query } against cache`
603
- ) ;
604
- deferred . reject ( firestoreError ) ;
605
- }
606
- }
607
-
608
- export async function enqueueExecuteQuery (
578
+ export async function enqueueExecuteQueryFromLocalCache (
609
579
asyncQueue : AsyncQueue ,
610
580
localStore : LocalStore ,
611
581
query : Query
612
582
) : Promise < ViewSnapshot > {
613
583
const deferred = new Deferred < ViewSnapshot > ( ) ;
614
- await asyncQueue . enqueue ( ( ) => executeQuery ( localStore , query , deferred ) ) ;
584
+ await asyncQueue . enqueue ( async ( ) => {
585
+ try {
586
+ const queryResult = await localStore . executeQuery (
587
+ query ,
588
+ /* usePreviousResults= */ true
589
+ ) ;
590
+ const view = new View ( query , queryResult . remoteKeys ) ;
591
+ const viewDocChanges = view . computeDocChanges ( queryResult . documents ) ;
592
+ const viewChange = view . applyChanges (
593
+ viewDocChanges ,
594
+ /* updateLimboDocuments= */ false
595
+ ) ;
596
+ deferred . resolve ( viewChange . snapshot ! ) ;
597
+ } catch ( e ) {
598
+ const firestoreError = wrapInUserErrorIfRecoverable (
599
+ e ,
600
+ `Failed to execute query '${ query } against cache`
601
+ ) ;
602
+ deferred . reject ( firestoreError ) ;
603
+ }
604
+ } ) ;
615
605
return deferred . promise ;
616
606
}
0 commit comments