Skip to content

add clearPersistence() to index.d.ts #1717

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 15 commits into from
Jun 5, 2019
17 changes: 16 additions & 1 deletion packages/firebase/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5988,7 +5988,8 @@ declare namespace firebase.firestore {
/**
* Attempts to enable persistent storage, if possible.
*
* Must be called before any other methods (other than settings()).
* Must be called before any other methods (other than settings() and
* clearPersistence()).
*
* If this fails, enablePersistence() will reject the promise it returns.
* Note that even after this failure, the firestore instance will remain
Expand Down Expand Up @@ -6060,6 +6061,20 @@ declare namespace firebase.firestore {
*/
app: firebase.app.App;

/**
* Clears the persistent storage.
*
* Must be called while the client is not started (after the app is
* shutdown or when the app is first initialized). On startup, this method
* must be called before other methods (other than settings()). If the
* client is still running, an exception with a code of `failed-precondition`
* will be thrown.
*
* @return A promise that is resolved once the persistent storage has been
* cleared. Otherwise, the promise is rejected with an error.
*/
clearPersistence(): Promise<void>;

/**
* Re-enables use of the network for this Firestore instance after a prior
* call to {@link firebase.firestore.Firestore.disableNetwork
Expand Down
17 changes: 16 additions & 1 deletion packages/firestore-types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,8 @@ export class FirebaseFirestore {
/**
* Attempts to enable persistent storage, if possible.
*
* Must be called before any other methods (other than settings()).
* Must be called before any other methods (other than settings() and
* clearPersistence()).
*
* If this fails, enablePersistence() will reject the promise it returns.
* Note that even after this failure, the firestore instance will remain
Expand Down Expand Up @@ -216,6 +217,20 @@ export class FirebaseFirestore {
*/
app: any;

/**
* Clears the persistent storage.
*
* Must be called while the client is not started (after the app is
* shutdown or when the app is first initialized). On startup, this method
* must be called before other methods (other than settings()). If the
* client is still running, an exception with a code of `failed-precondition`
* will be thrown.
*
* @return A promise that is resolved once the persistent storage has been
* cleared. Otherwise, the promise is rejected with an error.
*/
clearPersistence(): Promise<void>;

/**
* Re-enables use of the network for this Firestore instance after a prior
* call to disableNetwork().
Expand Down
2 changes: 1 addition & 1 deletion packages/firestore/src/api/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ export class Firestore implements firestore.FirebaseFirestore, FirebaseService {
);
}

_clearPersistence(): Promise<void> {
clearPersistence(): Promise<void> {
if (this.clientRunning) {
throw new FirestoreError(
Code.FAILED_PRECONDITION,
Expand Down
6 changes: 3 additions & 3 deletions packages/firestore/test/integration/api/database.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import { EventsAccumulator } from '../util/events_accumulator';
import firebase from '../util/firebase_export';
import {
apiDescribe,
clearPersistence,
withTestCollection,
withTestDb,
withTestDbs,
Expand Down Expand Up @@ -953,13 +952,14 @@ apiDescribe('Database', persistence => {
'can clear persistence if the client has not been initialized',
async () => {
await withTestDoc(persistence, async docRef => {
const firestore = docRef.firestore;
await docRef.set({ foo: 'bar' });
const app = docRef.firestore.app;
const name = app.name;
const options = app.options;

await app.delete();
await clearPersistence(docRef.firestore);
await firestore.clearPersistence();
const app2 = firebase.initializeApp(options, name);
const firestore2 = firebase.firestore!(app2);
await firestore2.enablePersistence();
Expand All @@ -974,7 +974,7 @@ apiDescribe('Database', persistence => {
it('can not clear persistence if the client has been initialized', async () => {
await withTestDoc(persistence, async docRef => {
const firestore = docRef.firestore;
await expect(clearPersistence(firestore)).to.eventually.be.rejectedWith(
await expect(() => firestore.clearPersistence()).to.throw(
'Persistence cannot be cleared while the client is running'
);
});
Expand Down
8 changes: 0 additions & 8 deletions packages/firestore/test/integration/util/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,6 @@ function apiDescribeInternal(
}
}

// TODO(b/131094514): Remove after clearPersistence() is updated in index.d.ts.
export async function clearPersistence(
firestore: firestore.FirebaseFirestore
): Promise<void> {
// tslint:disable-next-line:no-any
await (firestore as any)._clearPersistence();
}

/** Converts the documents in a QuerySnapshot to an array with the data of each document. */
export function toDataArray(
docSet: firestore.QuerySnapshot
Expand Down