Skip to content

Commit 7a15e7e

Browse files
author
Brian Chen
authored
Expose clearPersistence() publicly (#1717)
Addresses (#449).
1 parent 4b126b7 commit 7a15e7e

File tree

6 files changed

+60
-17
lines changed

6 files changed

+60
-17
lines changed

packages/firebase/index.d.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6086,7 +6086,8 @@ declare namespace firebase.firestore {
60866086
/**
60876087
* Attempts to enable persistent storage, if possible.
60886088
*
6089-
* Must be called before any other methods (other than settings()).
6089+
* Must be called before any other methods (other than settings() and
6090+
* clearPersistence()).
60906091
*
60916092
* If this fails, enablePersistence() will reject the promise it returns.
60926093
* Note that even after this failure, the firestore instance will remain
@@ -6178,6 +6179,28 @@ declare namespace firebase.firestore {
61786179
*/
61796180
app: firebase.app.App;
61806181

6182+
/**
6183+
* Clears the persistent storage. This includes pending writes and cached
6184+
* documents.
6185+
*
6186+
* Must be called while the firestore instance is not started (after the app is
6187+
* shutdown or when the app is first initialized). On startup, this method
6188+
* must be called before other methods (other than settings()). If the
6189+
* firestore instance is still running, the promise will be rejected with
6190+
* the error code of `failed-precondition`.
6191+
*
6192+
* Note: clearPersistence() is primarily intended to help write reliable
6193+
* tests that use Firestore. It uses the most efficient mechanism possible
6194+
* for dropping existing data but does not attempt to securely overwrite or
6195+
* otherwise make cached data unrecoverable. For applications that are
6196+
* sensitive to the disclosure of cache data in between user sessions we
6197+
* strongly recommend not to enable persistence in the first place.
6198+
*
6199+
* @return A promise that is resolved once the persistent storage has been
6200+
* cleared. Otherwise, the promise is rejected with an error.
6201+
*/
6202+
clearPersistence(): Promise<void>;
6203+
61816204
/**
61826205
* Re-enables use of the network for this Firestore instance after a prior
61836206
* call to {@link firebase.firestore.Firestore.disableNetwork

packages/firestore-types/index.d.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,8 @@ export class FirebaseFirestore {
145145
/**
146146
* Attempts to enable persistent storage, if possible.
147147
*
148-
* Must be called before any other methods (other than settings()).
148+
* Must be called before any other methods (other than settings() and
149+
* clearPersistence()).
149150
*
150151
* If this fails, enablePersistence() will reject the promise it returns.
151152
* Note that even after this failure, the firestore instance will remain
@@ -235,6 +236,28 @@ export class FirebaseFirestore {
235236
*/
236237
app: any;
237238

239+
/**
240+
* Clears the persistent storage. This includes pending writes and cached
241+
* documents.
242+
*
243+
* Must be called while the firestore instance is not started (after the app is
244+
* shutdown or when the app is first initialized). On startup, this method
245+
* must be called before other methods (other than settings()). If the
246+
* firestore instance is still running, the promise will be rejected with
247+
* the error code of `failed-precondition`.
248+
*
249+
* Note: clearPersistence() is primarily intended to help write reliable
250+
* tests that use Firestore. It uses the most efficient mechanism possible
251+
* for dropping existing data but does not attempt to securely overwrite or
252+
* otherwise make cached data unrecoverable. For applications that are
253+
* sensitive to the disclosure of cache data in between user sessions we
254+
* strongly recommend not to enable persistence in the first place.
255+
*
256+
* @return A promise that is resolved once the persistent storage has been
257+
* cleared. Otherwise, the promise is rejected with an error.
258+
*/
259+
clearPersistence(): Promise<void>;
260+
238261
/**
239262
* Re-enables use of the network for this Firestore instance after a prior
240263
* call to disableNetwork().

packages/firestore/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11

22
# Unreleased
3+
- [feature] Added `clearPersistence()`, which clears the persistent storage
4+
including pending writes and cached documents. This is intended to help
5+
write reliable tests (#449).
6+
7+
# 1.3.3
38
- [changed] Firestore now recovers more quickly after network connectivity
49
changes (airplane mode, Wi-Fi availability, etc.).
510

packages/firestore/src/api/database.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,7 @@ export class Firestore implements firestore.FirebaseFirestore, FirebaseService {
424424
);
425425
}
426426

427-
_clearPersistence(): Promise<void> {
427+
clearPersistence(): Promise<void> {
428428
const persistenceKey = IndexedDbPersistence.buildStoragePrefix(
429429
this.makeDatabaseInfo()
430430
);
@@ -437,7 +437,7 @@ export class Firestore implements firestore.FirebaseFirestore, FirebaseService {
437437
) {
438438
throw new FirestoreError(
439439
Code.FAILED_PRECONDITION,
440-
'Persistence cannot be cleared while this firestore instance is running.'
440+
'Persistence cannot be cleared after this Firestore instance is initialized.'
441441
);
442442
}
443443
await IndexedDbPersistence.clearPersistence(persistenceKey);

packages/firestore/test/integration/api/database.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ import { EventsAccumulator } from '../util/events_accumulator';
3030
import firebase from '../util/firebase_export';
3131
import {
3232
apiDescribe,
33-
clearPersistence,
3433
withTestCollection,
3534
withTestDb,
3635
withTestDbs,
@@ -954,13 +953,14 @@ apiDescribe('Database', persistence => {
954953
'can clear persistence if the client has not been initialized',
955954
async () => {
956955
await withTestDoc(persistence, async docRef => {
956+
const firestore = docRef.firestore;
957957
await docRef.set({ foo: 'bar' });
958958
const app = docRef.firestore.app;
959959
const name = app.name;
960960
const options = app.options;
961961

962962
await app.delete();
963-
await clearPersistence(docRef.firestore);
963+
await firestore.clearPersistence();
964964
const app2 = firebase.initializeApp(options, name);
965965
const firestore2 = firebase.firestore!(app2);
966966
await firestore2.enablePersistence();
@@ -984,7 +984,7 @@ apiDescribe('Database', persistence => {
984984
const firestore = docRef.firestore;
985985
await firestore.app.delete();
986986
await expect(
987-
clearPersistence(firestore)
987+
firestore.clearPersistence()
988988
).to.eventually.be.rejectedWith('Failed to delete the database.');
989989
} finally {
990990
SimpleDb.delete = oldDelete;
@@ -996,8 +996,8 @@ apiDescribe('Database', persistence => {
996996
it('can not clear persistence if the client has been initialized', async () => {
997997
await withTestDoc(persistence, async docRef => {
998998
const firestore = docRef.firestore;
999-
await expect(clearPersistence(firestore)).to.eventually.be.rejectedWith(
1000-
'Persistence cannot be cleared while this firestore instance is running.'
999+
await expect(firestore.clearPersistence()).to.eventually.be.rejectedWith(
1000+
'Persistence cannot be cleared after this Firestore instance is initialized.'
10011001
);
10021002
});
10031003
});

packages/firestore/test/integration/util/helpers.ts

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -106,14 +106,6 @@ function apiDescribeInternal(
106106
}
107107
}
108108

109-
// TODO(b/131094514): Remove after clearPersistence() is updated in index.d.ts.
110-
export async function clearPersistence(
111-
firestore: firestore.FirebaseFirestore
112-
): Promise<void> {
113-
// tslint:disable-next-line:no-any
114-
await (firestore as any)._clearPersistence();
115-
}
116-
117109
/** Converts the documents in a QuerySnapshot to an array with the data of each document. */
118110
export function toDataArray(
119111
docSet: firestore.QuerySnapshot

0 commit comments

Comments
 (0)