Skip to content
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

Make clearIndexedDbPersistence() work without enableIndexedDbPersistence() #3373

Merged
merged 5 commits into from
Jul 9, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
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 @@
---
---
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These changes seem unrelated. Revert?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed. I actually tried to remove them earlier, but the pre commit hook brought them back in. I had to disable the precommit hook for this commit.

*
* 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
* `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,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));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code reads as if it's circular unless you pay careful attention. If clearPersistence were named indexedDbClearPersistence it would be clearer that the higher level API was delegating to the lower-level implementation.

I realize this is getting at that type-operation naming discussion we started earlier so feel free to defer if you must, but I think it's really hurting the ability to understand what's going on here while reading the code directly. Sure you can go look at the imports but that breaks flow. Reading code is hard enough and we should strive to make it easy to understand.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I stumbled across this earlier too, but didn't act upon my confusion. I think a minimum bar for code quality should be that at the very least the person writing it shouldn't be confused...

Renamed to indexedDbClearPersistence and indexedDbStoragePrefix.

});
}

export function waitForPendingWrites(
Expand Down
Loading