Skip to content

Commit 7cd6653

Browse files
Cleanup
1 parent 7ce22d5 commit 7cd6653

File tree

3 files changed

+15
-64
lines changed

3 files changed

+15
-64
lines changed

packages/firestore/exp/src/api/database.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,9 @@ export class FirebaseFirestore
126126
);
127127
this._firestoreClient = new FirestoreClient(
128128
this._credentials,
129-
this._queue
129+
this._queue,
130+
databaseInfo
130131
);
131-
this._firestoreClient.start(databaseInfo);
132132
}
133133
return this._firestoreClient!;
134134
}

packages/firestore/src/api/database.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -546,8 +546,11 @@ export class Firestore
546546
);
547547

548548
const databaseInfo = this.makeDatabaseInfo();
549-
this._firestoreClient = new FirestoreClient(this._credentials, this._queue);
550-
this._firestoreClient.start(databaseInfo);
549+
this._firestoreClient = new FirestoreClient(
550+
this._credentials,
551+
this._queue,
552+
databaseInfo
553+
);
551554
}
552555

553556
private static databaseIdFromApp(app: FirebaseApp): DatabaseId {

packages/firestore/src/core/firestore_client.ts

Lines changed: 8 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -85,30 +85,14 @@ export const MAX_CONCURRENT_LIMBO_RESOLUTIONS = 100;
8585
* async queue that is shared by all of the other components in the system.
8686
*/
8787
export class FirestoreClient {
88-
// NOTE: These should technically have '|undefined' in the types, since
89-
// they're initialized asynchronously rather than in the constructor, but
90-
// given that all work is done on the async queue and we assert that
91-
// initialization completes before any other work is queued, we're cheating
92-
// with the types rather than littering the code with '!' or unnecessary
93-
// undefined checks.
94-
private databaseInfo!: DatabaseInfo;
9588
private user = User.UNAUTHENTICATED;
89+
private readonly clientId = AutoId.newId();
9690
private credentialListener: CredentialChangeListener = () => {};
91+
private readonly receivedInitialUser = new Deferred<void>();
9792

9893
offlineComponents?: OfflineComponentProvider;
9994
onlineComponents?: OnlineComponentProvider;
10095

101-
private readonly clientId = AutoId.newId();
102-
103-
// We defer our initialization until we get the current user from
104-
// setChangeListener(). We block the async queue until we got the initial
105-
// user and the initialization is completed. This will prevent any scheduled
106-
// work from happening before initialization is completed.
107-
//
108-
// If initializationDone resolved then the FirestoreClient is in a usable
109-
// state.
110-
private readonly initializationDone = new Deferred<void>();
111-
11296
constructor(
11397
private credentials: CredentialsProvider,
11498
/**
@@ -119,57 +103,21 @@ export class FirestoreClient {
119103
* start processing a new operation while the previous one is waiting for
120104
* an async I/O to complete).
121105
*/
122-
public asyncQueue: AsyncQueue
123-
) {}
124-
125-
/**
126-
* Starts up the FirestoreClient, returning only whether or not enabling
127-
* persistence succeeded.
128-
*
129-
* The intent here is to "do the right thing" as far as users are concerned.
130-
* Namely, in cases where offline persistence is requested and possible,
131-
* enable it, but otherwise fall back to persistence disabled. For the most
132-
* part we expect this to succeed one way or the other so we don't expect our
133-
* users to actually wait on the firestore.enablePersistence Promise since
134-
* they generally won't care.
135-
*
136-
* Of course some users actually do care about whether or not persistence
137-
* was successfully enabled, so the Promise returned from this method
138-
* indicates this outcome.
139-
*
140-
* This presents a problem though: even before enablePersistence resolves or
141-
* rejects, users may have made calls to e.g. firestore.collection() which
142-
* means that the FirestoreClient in there will be available and will be
143-
* enqueuing actions on the async queue.
144-
*
145-
* Meanwhile any failure of an operation on the async queue causes it to
146-
* panic and reject any further work, on the premise that unhandled errors
147-
* are fatal.
148-
*
149-
* Consequently the fallback is handled internally here in start, and if the
150-
* fallback succeeds we signal success to the async queue even though the
151-
* start() itself signals failure.
152-
*
153-
* @param databaseInfo The connection information for the current instance.
154-
*/
155-
start(databaseInfo: DatabaseInfo): void {
156-
this.databaseInfo = databaseInfo;
157-
106+
public asyncQueue: AsyncQueue,
107+
private databaseInfo: DatabaseInfo
108+
) {
158109
this.credentials.setChangeListener(user => {
159110
logDebug(LOG_TAG, 'Received user=', user.uid);
160111
if (!this.user.isEqual(user)) {
161112
this.user = user;
162113
this.credentialListener(user);
163114
}
164-
this.initializationDone.resolve();
115+
this.receivedInitialUser.resolve();
165116
});
166-
167-
// Block the async queue until initialization is done
168-
this.asyncQueue.enqueueAndForget(() => this.initializationDone.promise);
169117
}
170118

171119
async getConfiguration(): Promise<ComponentConfiguration> {
172-
await this.initializationDone.promise;
120+
await this.receivedInitialUser.promise;
173121

174122
return {
175123
asyncQueue: this.asyncQueue,
@@ -184,7 +132,7 @@ export class FirestoreClient {
184132
setCredentialChangeListener(listener: (user: User) => void): void {
185133
this.credentialListener = listener;
186134
// eslint-disable-next-line @typescript-eslint/no-floating-promises
187-
this.initializationDone.promise.then(() =>
135+
this.receivedInitialUser.promise.then(() =>
188136
this.credentialListener(this.user)
189137
);
190138
}

0 commit comments

Comments
 (0)