Skip to content

Remove exception throwing from clearPersistence() #1793

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 9 additions & 11 deletions packages/firestore/src/api/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -309,16 +309,13 @@ export class Firestore implements firestore.FirebaseFirestore, FirebaseService {
// are already set to synchronize on the async queue.
private _firestoreClient: FirestoreClient | undefined;

private clientRunning: boolean;

// Public for use in tests.
// TODO(mikelehen): Use modularized initialization instead.
readonly _queue = new AsyncQueue();

_dataConverter: UserDataConverter;

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

_clearPersistence(): Promise<void> {
if (this.clientRunning) {
throw new FirestoreError(
Code.FAILED_PRECONDITION,
'Persistence cannot be cleared while the client is running'
);
}
const persistenceKey = IndexedDbPersistence.buildStoragePrefix(
this.makeDatabaseInfo()
);
const deferred = new Deferred<void>();
this._queue.enqueueAndForget(async () => {
try {
if (
this._firestoreClient !== undefined &&
!this._firestoreClient.clientShutdown
) {
throw new FirestoreError(
Code.FAILED_PRECONDITION,
'Persistence cannot be cleared while this firestore instance is running.'
);
}
await IndexedDbPersistence.clearPersistence(persistenceKey);
deferred.resolve();
} catch (e) {
Expand Down Expand Up @@ -478,7 +478,6 @@ export class Firestore implements firestore.FirebaseFirestore, FirebaseService {

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

this.clientRunning = true;
const databaseInfo = this.makeDatabaseInfo();

const preConverter = (value: unknown) => {
Expand Down Expand Up @@ -546,7 +545,6 @@ export class Firestore implements firestore.FirebaseFirestore, FirebaseService {
// throws an exception.
this.ensureClientConfigured();
await this._firestoreClient!.shutdown();
this.clientRunning = false;
}
};

Expand Down
12 changes: 8 additions & 4 deletions packages/firestore/src/core/firestore_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ export class FirestoreClient {
private lruScheduler?: LruScheduler;

private readonly clientId = AutoId.newId();
private isShutdown = false;
private _clientShutdown = false;

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

shutdown(): Promise<void> {
return this.asyncQueue.enqueue(async () => {
if (!this.isShutdown) {
if (!this._clientShutdown) {
// PORTING NOTE: LocalStore does not need an explicit shutdown on web.
if (this.lruScheduler) {
this.lruScheduler.stop();
Expand All @@ -509,7 +509,7 @@ export class FirestoreClient {
// RemoteStore as it will prevent the RemoteStore from retrieving
// auth tokens.
this.credentials.removeChangeListener();
this.isShutdown = true;
this._clientShutdown = true;
}
});
}
Expand Down Expand Up @@ -590,6 +590,10 @@ export class FirestoreClient {
return this.databaseInfo.databaseId;
}

get clientShutdown(): boolean {
return this._clientShutdown;
}

transaction<T>(
updateFunction: (transaction: Transaction) => Promise<T>
): Promise<T> {
Expand Down
2 changes: 1 addition & 1 deletion packages/firestore/test/integration/api/database.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -997,7 +997,7 @@ apiDescribe('Database', persistence => {
await withTestDoc(persistence, async docRef => {
const firestore = docRef.firestore;
await expect(clearPersistence(firestore)).to.eventually.be.rejectedWith(
'Persistence cannot be cleared while the client is running'
'Persistence cannot be cleared while this firestore instance is running.'
);
});
});
Expand Down