Skip to content

Commit

Permalink
Make clearIndexedDbPersistence() work without enableIndexedDbPersiste…
Browse files Browse the repository at this point in the history
…nce() (#3373)
  • Loading branch information
schmidt-sebastian committed Jul 9, 2020
1 parent 4fc68ef commit 0b14a8c
Show file tree
Hide file tree
Showing 8 changed files with 192 additions and 134 deletions.
2 changes: 2 additions & 0 deletions .changeset/nice-deers-suffer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
---
31 changes: 21 additions & 10 deletions packages/firestore/exp/src/api/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@ import { Code, FirestoreError } from '../../../src/util/error';
import { Deferred } from '../../../src/util/promise';
import { LruParams } from '../../../src/local/lru_garbage_collector';
import { CACHE_SIZE_UNLIMITED } from '../../../src/api/database';
import { DatabaseInfo } from '../../../src/core/database_info';
import { DatabaseId, DatabaseInfo } from '../../../src/core/database_info';
import {
indexedDbStoragePrefix,
indexedDbClearPersistence
} from '../../../src/local/indexeddb_persistence';

/**
* The root reference to the Firestore database and the entry point for the
Expand Down Expand Up @@ -143,7 +147,16 @@ export class Firestore extends LiteFirestore
return terminate(this);
}

_clearPersistence(): Promise<void> {
/**
* Verifies that the client is not running and clears persistence by invoking
* `delegate` on the async queue.
*
* @param delegate A function that clears the clients
* backing storage.
*/
_clearPersistence(
delegate: (databaseId: DatabaseId, persistenceKey: string) => Promise<void>
): Promise<void> {
if (this._deferredInitialization !== undefined && !this._terminated) {
throw new FirestoreError(
Code.FAILED_PRECONDITION,
Expand All @@ -152,16 +165,10 @@ export class Firestore extends LiteFirestore
);
}

const settings = this._getSettings();
const deferred = new Deferred<void>();
this._queue.enqueueAndForgetEvenAfterShutdown(async () => {
try {
const databaseInfo = this._makeDatabaseInfo(
settings.host,
settings.ssl,
settings.experimentalForceLongPolling
);
await this._componentProvider.clearPersistence(databaseInfo);
await delegate(this._databaseId, this._persistenceKey);
deferred.resolve();
} catch (e) {
deferred.reject(e);
Expand Down Expand Up @@ -247,7 +254,11 @@ export function clearIndexedDbPersistence(
firestore: firestore.FirebaseFirestore
): Promise<void> {
const firestoreImpl = cast(firestore, Firestore);
return firestoreImpl._clearPersistence();
return firestoreImpl._clearPersistence((databaseId, persistenceKey) => {
return indexedDbClearPersistence(
indexedDbStoragePrefix(databaseId, persistenceKey)
);
});
}

export function waitForPendingWrites(
Expand Down
Loading

0 comments on commit 0b14a8c

Please sign in to comment.