Skip to content

Commit 313478c

Browse files
Cleanup
1 parent 026fa9a commit 313478c

File tree

1 file changed

+70
-80
lines changed

1 file changed

+70
-80
lines changed

packages/firestore/src/core/firestore_client.ts

Lines changed: 70 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,15 @@ export class FirestoreClient {
9393

9494
private readonly clientId = AutoId.newId();
9595

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+
96105
constructor(
97106
private credentials: CredentialsProvider,
98107
/**
@@ -156,15 +165,6 @@ export class FirestoreClient {
156165

157166
this.databaseInfo = databaseInfo;
158167

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-
168168
// If usePersistence is true, certain classes of errors while starting are
169169
// recoverable but only by falling back to persistence disabled.
170170
//
@@ -186,7 +186,7 @@ export class FirestoreClient {
186186
persistenceSettings,
187187
user,
188188
persistenceResult
189-
).then(initializationDone.resolve, initializationDone.reject);
189+
).then(this.initializationDone.resolve, this.initializationDone.reject);
190190
} else {
191191
this.asyncQueue.enqueueRetryable(() =>
192192
this.remoteStore.handleCredentialChange(user)
@@ -196,7 +196,7 @@ export class FirestoreClient {
196196

197197
// Block the async queue until initialization is done
198198
this.asyncQueue.enqueueAndForget(() => {
199-
return initializationDone.promise;
199+
return this.initializationDone.promise;
200200
});
201201

202202
// Return only the result of enabling persistence. Note that this does not
@@ -409,20 +409,22 @@ export class FirestoreClient {
409409
docKey: DocumentKey
410410
): Promise<Document | null> {
411411
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
415417
);
416-
return deferred.promise;
417418
}
418419

419420
async getDocumentsFromLocalCache(query: Query): Promise<ViewSnapshot> {
420421
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
424427
);
425-
return deferred.promise;
426428
}
427429

428430
write(mutations: Mutation[]): Promise<void> {
@@ -538,79 +540,67 @@ export function enqueueSnapshotsInSyncListen(
538540
};
539541
}
540542

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(
573544
asyncQueue: AsyncQueue,
574545
localStore: LocalStore,
575546
docKey: DocumentKey
576547
): Promise<Document | null> {
577548
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+
});
579575
return deferred.promise;
580576
}
581577

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(
609579
asyncQueue: AsyncQueue,
610580
localStore: LocalStore,
611581
query: Query
612582
): Promise<ViewSnapshot> {
613583
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+
});
615605
return deferred.promise;
616606
}

0 commit comments

Comments
 (0)