Skip to content

Commit 7ce22d5

Browse files
Cleanup
1 parent 3ce2fe4 commit 7ce22d5

File tree

5 files changed

+27
-68
lines changed

5 files changed

+27
-68
lines changed

packages/database/src/api/Database.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -157,17 +157,14 @@ export class Database implements FirebaseService {
157157
validateUrl(apiName, 1, parsedURL);
158158

159159
const repoInfo = parsedURL.repoInfo;
160-
if (
161-
!repoInfo.isCustomHost() &&
162-
repoInfo.host !== this.repo_.repoInfo_.host
163-
) {
160+
if (!repoInfo.isCustomHost() && repoInfo.host !== this.repo_.repoInfo_.host) {
164161
fatal(
165162
apiName +
166163
': Host name does not match the current database: ' +
167164
'(found ' +
168165
repoInfo.host +
169166
' but expected ' +
170-
this.repo_.repoInfo_.host +
167+
this.repo_.repoInfo_.host+
171168
')'
172169
);
173170
}

packages/database/src/api/Reference.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ import { SyncPoint } from '../core/SyncPoint';
3838
import { Database } from './Database';
3939
import { DataSnapshot } from './DataSnapshot';
4040
import * as types from '@firebase/database-types';
41-
import { ThenableReference } from '@firebase/database-types';
4241

4342
export interface ReferenceConstructor {
4443
new (repo: Repo, path: Path): Reference;
@@ -332,10 +331,7 @@ export class Reference extends Query {
332331
* @param {function(?Error)=} onComplete
333332
* @return {!Reference}
334333
*/
335-
push(
336-
value?: unknown,
337-
onComplete?: (a: Error | null) => void
338-
): ThenableReference {
334+
push(value?: unknown, onComplete?: (a: Error | null) => void): Reference {
339335
validateArgCount('Reference.push', 0, 2, arguments.length);
340336
validateWritablePath('Reference.push', this.path);
341337
validateFirebaseDataArg('Reference.push', 1, value, this.path, true);
@@ -373,7 +369,6 @@ export class Reference extends Query {
373369
* @return {!OnDisconnect}
374370
*/
375371
onDisconnect(): OnDisconnect {
376-
this.push().startAt('ff');
377372
validateWritablePath('Reference.onDisconnect', this.path);
378373
return new OnDisconnect(this.repo, this.path);
379374
}

packages/firestore/src/api/database.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -524,7 +524,6 @@ export class Firestore
524524
// eslint-disable-next-line @typescript-eslint/no-floating-promises
525525
this.configureClient();
526526
}
527-
this._firestoreClient!.verifyNotTerminated();
528527
return this._firestoreClient as FirestoreClient;
529528
}
530529

@@ -584,10 +583,8 @@ export class Firestore
584583
delete: async (): Promise<void> => {
585584
// The client must be initialized to ensure that all subsequent API usage
586585
// throws an exception.
587-
if (!this._terminated) {
588-
this._ensureClientConfigured();
589-
await this._firestoreClient!.terminate();
590-
}
586+
this._ensureClientConfigured();
587+
await this._firestoreClient!.terminate();
591588
}
592589
};
593590

packages/firestore/src/core/firestore_client.ts

Lines changed: 16 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -93,12 +93,13 @@ export class FirestoreClient {
9393
// undefined checks.
9494
private databaseInfo!: DatabaseInfo;
9595
private user = User.UNAUTHENTICATED;
96-
private readonly clientId = AutoId.newId();
9796
private credentialListener: CredentialChangeListener = () => {};
9897

9998
offlineComponents?: OfflineComponentProvider;
10099
onlineComponents?: OnlineComponentProvider;
101100

101+
private readonly clientId = AutoId.newId();
102+
102103
// We defer our initialization until we get the current user from
103104
// setChangeListener(). We block the async queue until we got the initial
104105
// user and the initialization is completed. This will prevent any scheduled
@@ -154,18 +155,13 @@ export class FirestoreClient {
154155
start(databaseInfo: DatabaseInfo): void {
155156
this.databaseInfo = databaseInfo;
156157

157-
let initialized = false;
158158
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)) {
166161
this.user = user;
167162
this.credentialListener(user);
168163
}
164+
this.initializationDone.resolve();
169165
});
170166

171167
// Block the async queue until initialization is done
@@ -186,38 +182,24 @@ export class FirestoreClient {
186182
}
187183

188184
setCredentialChangeListener(listener: (user: User) => void): void {
189-
logDebug('FirebaseFirestore', 'Registering credential change listener');
190185
this.credentialListener = listener;
191186
// eslint-disable-next-line @typescript-eslint/no-floating-promises
192187
this.initializationDone.promise.then(() =>
193188
this.credentialListener(this.user)
194189
);
195190
}
196191

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-
214192
terminate(): Promise<void> {
215193
this.asyncQueue.enterRestrictedMode();
216194
const deferred = new Deferred();
217195
this.asyncQueue.enqueueAndForgetEvenWhileRestricted(async () => {
218196
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+
}
221203

222204
// `removeChangeListener` must be called after shutting down the
223205
// RemoteStore as it will prevent the RemoteStore from retrieving
@@ -236,12 +218,9 @@ export class FirestoreClient {
236218
}
237219
}
238220

239-
// TODO(firestore-compat): Remove `export` once compat migration is complete.
240-
export async function ensureOfflineComponents(
221+
async function ensureOfflineComponents(
241222
firestoreClient: FirestoreClient
242223
): Promise<OfflineComponentProvider> {
243-
firestoreClient.asyncQueue.verifyOperationInProgress();
244-
245224
if (!firestoreClient.offlineComponents) {
246225
logDebug(LOG_TAG, 'Using default OfflineComponentProvider');
247226
await setOfflineComponentProvider(
@@ -253,12 +232,9 @@ export async function ensureOfflineComponents(
253232
return firestoreClient.offlineComponents!;
254233
}
255234

256-
// TODO(firestore-compat): Remove `export` once compat migration is complete.
257-
export async function ensureOnlineComponents(
235+
async function ensureOnlineComponents(
258236
firestoreClient: FirestoreClient
259237
): Promise<OnlineComponentProvider> {
260-
firestoreClient.asyncQueue.verifyOperationInProgress();
261-
262238
if (!firestoreClient.onlineComponents) {
263239
logDebug(LOG_TAG, 'Using default OnlineComponentProvider');
264240
await setOnlineComponentProvider(
@@ -332,7 +308,6 @@ export function getPersistence(
332308
export async function firestoreClientEnableNetwork(
333309
firestoreClient: FirestoreClient
334310
): Promise<void> {
335-
firestoreClient.verifyNotTerminated();
336311
return firestoreClient.asyncQueue.enqueue(async () => {
337312
const persistence = await getPersistence(firestoreClient);
338313
const remoteStore = await getRemoteStore(firestoreClient);
@@ -351,7 +326,6 @@ export function getRemoteStore(
351326
export async function firestoreClientDisableNetwork(
352327
firestoreClient: FirestoreClient
353328
): Promise<void> {
354-
firestoreClient.verifyNotTerminated();
355329
return firestoreClient.asyncQueue.enqueue(async () => {
356330
const persistence = await getPersistence(firestoreClient);
357331
const remoteStore = await getRemoteStore(firestoreClient);
@@ -374,8 +348,6 @@ export function getSyncEngine(
374348
export async function firestoreClientWaitForPendingWrites(
375349
firestoreClient: FirestoreClient
376350
): Promise<void> {
377-
firestoreClient.verifyNotTerminated();
378-
379351
const deferred = new Deferred<void>();
380352
firestoreClient.asyncQueue.enqueueAndForget(async () => {
381353
const syncEngine = await getSyncEngine(firestoreClient);
@@ -406,7 +378,6 @@ export function firestoreClientListen(
406378
options: ListenOptions,
407379
observer: Partial<Observer<ViewSnapshot>>
408380
): () => void {
409-
firestoreClient.verifyNotTerminated();
410381
const wrappedObserver = new AsyncObserver(observer);
411382
const listener = new QueryListener(query, wrappedObserver, options);
412383
firestoreClient.asyncQueue.enqueueAndForget(async () => {
@@ -426,7 +397,6 @@ export function firestoreClientGetDocumentFromLocalCache(
426397
firestoreClient: FirestoreClient,
427398
docKey: DocumentKey
428399
): Promise<Document | null> {
429-
firestoreClient.verifyNotTerminated();
430400
const deferred = new Deferred<Document | null>();
431401
firestoreClient.asyncQueue.enqueueAndForget(async () => {
432402
const localStore = await getLocalStore(firestoreClient);
@@ -440,7 +410,6 @@ export function firestoreClientGetDocumentViaSnapshotListener(
440410
key: DocumentKey,
441411
options: GetOptions = {}
442412
): Promise<ViewSnapshot> {
443-
firestoreClient.verifyNotTerminated();
444413
const deferred = new Deferred<ViewSnapshot>();
445414
firestoreClient.asyncQueue.enqueueAndForget(async () => {
446415
const eventManager = await getEventManager(firestoreClient);
@@ -456,14 +425,13 @@ export function firestoreClientGetDocumentViaSnapshotListener(
456425
}
457426

458427
export async function getLocalStore(firestoreClient: FirestoreClient) {
459-
return (await ensureOfflineComponents(firestoreClient)).localStore;
428+
return ensureOfflineComponents(firestoreClient).then(c => c.localStore);
460429
}
461430

462431
export function firestoreClientGetDocumentsFromLocalCache(
463432
firestoreClient: FirestoreClient,
464433
query: Query
465434
): Promise<ViewSnapshot> {
466-
firestoreClient.verifyNotTerminated();
467435
const deferred = new Deferred<ViewSnapshot>();
468436
firestoreClient.asyncQueue.enqueueAndForget(async () => {
469437
const localStore = await getLocalStore(firestoreClient);
@@ -477,7 +445,6 @@ export function firestoreClientGetDocumentsViaSnapshotListener(
477445
query: Query,
478446
options: GetOptions = {}
479447
): Promise<ViewSnapshot> {
480-
firestoreClient.verifyNotTerminated();
481448
const deferred = new Deferred<ViewSnapshot>();
482449
firestoreClient.asyncQueue.enqueueAndForget(async () => {
483450
const eventManager = await getEventManager(firestoreClient);
@@ -496,7 +463,6 @@ export function firestoreClientWrite(
496463
firestoreClient: FirestoreClient,
497464
mutations: Mutation[]
498465
): Promise<void> {
499-
firestoreClient.verifyNotTerminated();
500466
const deferred = new Deferred<void>();
501467
firestoreClient.asyncQueue.enqueueAndForget(async () => {
502468
const syncEngine = await getSyncEngine(firestoreClient);
@@ -509,7 +475,6 @@ export function firestoreClientAddSnapshotsInSyncListener(
509475
firestoreClient: FirestoreClient,
510476
observer: Partial<Observer<void>>
511477
): () => void {
512-
firestoreClient.verifyNotTerminated();
513478
const wrappedObserver = new AsyncObserver(observer);
514479
firestoreClient.asyncQueue.enqueueAndForget(async () => {
515480
const eventManager = await getEventManager(firestoreClient);
@@ -524,10 +489,10 @@ export function firestoreClientAddSnapshotsInSyncListener(
524489
};
525490
}
526491

527-
export async function getDatastore(
492+
export function getDatastore(
528493
firestoreClient: FirestoreClient
529494
): Promise<Datastore> {
530-
return (await ensureOnlineComponents(firestoreClient)).datastore;
495+
return ensureOnlineComponents(firestoreClient).then(c => c.datastore);
531496
}
532497

533498
/**
@@ -549,7 +514,6 @@ export function firestoreClientTransaction<T>(
549514
firestoreClient: FirestoreClient,
550515
updateFunction: (transaction: Transaction) => Promise<T>
551516
): Promise<T> {
552-
firestoreClient.verifyNotTerminated();
553517
const deferred = new Deferred<T>();
554518
firestoreClient.asyncQueue.enqueueAndForget(async () => {
555519
const datastore = await getDatastore(firestoreClient);

packages/firestore/src/util/async_queue.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,12 @@ export class AsyncQueue {
303303
* when the promise returned by the new operation is (with its value).
304304
*/
305305
enqueue<T extends unknown>(op: () => Promise<T>): Promise<T> {
306+
if (this.isShuttingDown) {
307+
throw new FirestoreError(
308+
Code.FAILED_PRECONDITION,
309+
'The client has already been terminated.'
310+
);
311+
}
306312
this.verifyNotFailed();
307313
if (this._isShuttingDown) {
308314
// Return a Promise which never resolves.

0 commit comments

Comments
 (0)