Skip to content

Commit

Permalink
Make clearIndexedDbPersistence() work without enableIndexedDbPersiste…
Browse files Browse the repository at this point in the history
…nce()
  • Loading branch information
schmidt-sebastian committed Jul 9, 2020
1 parent 9b53ec8 commit ca0261e
Show file tree
Hide file tree
Showing 10 changed files with 222 additions and 137 deletions.
36 changes: 36 additions & 0 deletions integration/integration/firestore/overwrites.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* @license
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import firebase from 'firebase/app/dist/index.esm.js';

let appCount = 0;
export function newTestFirestore(projectId, nameOrApp, settings) {
if (nameOrApp === undefined) {
nameOrApp = 'test-app-' + appCount++;
}

const app =
typeof nameOrApp === 'string'
? firebase.initializeApp({ apiKey: 'fake-api-key', projectId }, nameOrApp)
: nameOrApp;

const firestore = firebase.firestore(app);
if (settings) {
firestore.settings(settings);
}
return firestore;
}
2 changes: 1 addition & 1 deletion packages/auth/src/args.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* @license
* Copyright 2017 Google Inc.
* Copyright 2017 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion packages/auth/src/auth.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* @license
* Copyright 2017 Google Inc.
* Copyright 2017 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
29 changes: 19 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 {
buildStoragePrefix,
clearPersistence
} 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
* `clearPersistenceDelegate` 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,9 @@ export function clearIndexedDbPersistence(
firestore: firestore.FirebaseFirestore
): Promise<void> {
const firestoreImpl = cast(firestore, Firestore);
return firestoreImpl._clearPersistence();
return firestoreImpl._clearPersistence((databaseId, persistenceKey) => {
return clearPersistence(buildStoragePrefix(databaseId, persistenceKey));
});
}

export function waitForPendingWrites(
Expand Down
Loading

0 comments on commit ca0261e

Please sign in to comment.