Skip to content

Commit 5f5f1c1

Browse files
author
Brian Chen
authored
Remove exception throwing from clearPersistence() (#1793)
1 parent 2b165d5 commit 5f5f1c1

File tree

3 files changed

+18
-16
lines changed

3 files changed

+18
-16
lines changed

packages/firestore/src/api/database.ts

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -309,16 +309,13 @@ export class Firestore implements firestore.FirebaseFirestore, FirebaseService {
309309
// are already set to synchronize on the async queue.
310310
private _firestoreClient: FirestoreClient | undefined;
311311

312-
private clientRunning: boolean;
313-
314312
// Public for use in tests.
315313
// TODO(mikelehen): Use modularized initialization instead.
316314
readonly _queue = new AsyncQueue();
317315

318316
_dataConverter: UserDataConverter;
319317

320318
constructor(databaseIdOrApp: FirestoreDatabase | FirebaseApp) {
321-
this.clientRunning = false;
322319
const config = new FirestoreConfig();
323320
if (typeof (databaseIdOrApp as FirebaseApp).options === 'object') {
324321
// This is very likely a Firebase app object
@@ -428,18 +425,21 @@ export class Firestore implements firestore.FirebaseFirestore, FirebaseService {
428425
}
429426

430427
_clearPersistence(): Promise<void> {
431-
if (this.clientRunning) {
432-
throw new FirestoreError(
433-
Code.FAILED_PRECONDITION,
434-
'Persistence cannot be cleared while the client is running'
435-
);
436-
}
437428
const persistenceKey = IndexedDbPersistence.buildStoragePrefix(
438429
this.makeDatabaseInfo()
439430
);
440431
const deferred = new Deferred<void>();
441432
this._queue.enqueueAndForget(async () => {
442433
try {
434+
if (
435+
this._firestoreClient !== undefined &&
436+
!this._firestoreClient.clientShutdown
437+
) {
438+
throw new FirestoreError(
439+
Code.FAILED_PRECONDITION,
440+
'Persistence cannot be cleared while this firestore instance is running.'
441+
);
442+
}
443443
await IndexedDbPersistence.clearPersistence(persistenceKey);
444444
deferred.resolve();
445445
} catch (e) {
@@ -478,7 +478,6 @@ export class Firestore implements firestore.FirebaseFirestore, FirebaseService {
478478

479479
assert(!this._firestoreClient, 'configureClient() called multiple times');
480480

481-
this.clientRunning = true;
482481
const databaseInfo = this.makeDatabaseInfo();
483482

484483
const preConverter = (value: unknown) => {
@@ -546,7 +545,6 @@ export class Firestore implements firestore.FirebaseFirestore, FirebaseService {
546545
// throws an exception.
547546
this.ensureClientConfigured();
548547
await this._firestoreClient!.shutdown();
549-
this.clientRunning = false;
550548
}
551549
};
552550

packages/firestore/src/core/firestore_client.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ export class FirestoreClient {
108108
private lruScheduler?: LruScheduler;
109109

110110
private readonly clientId = AutoId.newId();
111-
private isShutdown = false;
111+
private _clientShutdown = false;
112112

113113
// PORTING NOTE: SharedClientState is only used for multi-tab web.
114114
private sharedClientState: SharedClientState;
@@ -305,7 +305,7 @@ export class FirestoreClient {
305305
* this class cannot be called after the client is shutdown.
306306
*/
307307
private verifyNotShutdown(): void {
308-
if (this.isShutdown) {
308+
if (this._clientShutdown) {
309309
throw new FirestoreError(
310310
Code.FAILED_PRECONDITION,
311311
'The client has already been shutdown.'
@@ -496,7 +496,7 @@ export class FirestoreClient {
496496

497497
shutdown(): Promise<void> {
498498
return this.asyncQueue.enqueue(async () => {
499-
if (!this.isShutdown) {
499+
if (!this._clientShutdown) {
500500
// PORTING NOTE: LocalStore does not need an explicit shutdown on web.
501501
if (this.lruScheduler) {
502502
this.lruScheduler.stop();
@@ -509,7 +509,7 @@ export class FirestoreClient {
509509
// RemoteStore as it will prevent the RemoteStore from retrieving
510510
// auth tokens.
511511
this.credentials.removeChangeListener();
512-
this.isShutdown = true;
512+
this._clientShutdown = true;
513513
}
514514
});
515515
}
@@ -590,6 +590,10 @@ export class FirestoreClient {
590590
return this.databaseInfo.databaseId;
591591
}
592592

593+
get clientShutdown(): boolean {
594+
return this._clientShutdown;
595+
}
596+
593597
transaction<T>(
594598
updateFunction: (transaction: Transaction) => Promise<T>
595599
): Promise<T> {

packages/firestore/test/integration/api/database.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -997,7 +997,7 @@ apiDescribe('Database', persistence => {
997997
await withTestDoc(persistence, async docRef => {
998998
const firestore = docRef.firestore;
999999
await expect(clearPersistence(firestore)).to.eventually.be.rejectedWith(
1000-
'Persistence cannot be cleared while the client is running'
1000+
'Persistence cannot be cleared while this firestore instance is running.'
10011001
);
10021002
});
10031003
});

0 commit comments

Comments
 (0)