Skip to content

Commit

Permalink
feat(firestore): add method clearPersistence (#446)
Browse files Browse the repository at this point in the history
  • Loading branch information
robingenz authored Oct 6, 2023
1 parent 1338d5f commit 5a42c3f
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/authentication/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1665,7 +1665,7 @@ Remove all listeners for this plugin.
| Prop | Type | Description | Default | Since |
| ----------------------- | -------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------ | ----- |
| **`phoneNumber`** | <code>string</code> | The phone number to be verified in E.164 format. | | 0.1.0 |
| **`recaptchaVerifier`** | <code>unknown</code> | The reCAPTCHA verifier. Must be an instance of `firebase.auth.RecaptchaVerifier`. Only available for Web. | | 1.3.0 |
| **`recaptchaVerifier`** | <code>unknown</code> | The reCAPTCHA verifier. Must be an instance of `firebase.auth.RecaptchaVerifier`. Only available for Web. | | 5.2.0 |
| **`resendCode`** | <code>boolean</code> | Resend the verification code to the specified phone number. `signInWithPhoneNumber` must be called once before using this option. Only available for Android. | <code>false</code> | 1.3.0 |


Expand Down
16 changes: 16 additions & 0 deletions packages/firestore/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ const removeAllListeners = async () => {
* [`deleteDocument(...)`](#deletedocument)
* [`getCollection(...)`](#getcollection)
* [`getCollectionGroup(...)`](#getcollectiongroup)
* [`clearPersistence()`](#clearpersistence)
* [`enableNetwork()`](#enablenetwork)
* [`disableNetwork()`](#disablenetwork)
* [`addDocumentSnapshotListener(...)`](#adddocumentsnapshotlistener)
Expand Down Expand Up @@ -332,6 +333,21 @@ Reads the collection group referenced by the specified reference.
--------------------


### clearPersistence()

```typescript
clearPersistence() => Promise<void>
```

Clears the persistent storage. This includes pending writes and cached documents.

Must be called after the app is shutdown or when the app is first initialized.

**Since:** 5.2.0

--------------------


### enableNetwork()

```typescript
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,20 @@ public void getCollectionGroup(@NonNull GetCollectionGroupOptions options, @NonN
.addOnFailureListener(exception -> callback.error(exception));
}

public void clearPersistence(@NonNull EmptyResultCallback callback) {
this.firestoreInstance.clearPersistence()
.addOnSuccessListener(
unused -> {
callback.success();
}
)
.addOnFailureListener(
exception -> {
callback.error(exception);
}
);
}

public void enableNetwork(@NonNull EmptyResultCallback callback) {
this.firestoreInstance.enableNetwork()
.addOnSuccessListener(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,28 @@ public void error(Exception exception) {
}
}

@PluginMethod
public void clearPersistence(PluginCall call) {
try {
EmptyResultCallback callback = new EmptyResultCallback() {
@Override
public void success() {
call.resolve();
}

@Override
public void error(Exception exception) {
Logger.error(TAG, exception.getMessage(), exception);
call.reject(exception.getMessage());
}
};
implementation.clearPersistence(callback);
} catch (Exception exception) {
Logger.error(TAG, exception.getMessage(), exception);
call.reject(exception.getMessage());
}
}

@PluginMethod
public void enableNetwork(PluginCall call) {
try {
Expand Down
6 changes: 6 additions & 0 deletions packages/firestore/ios/Plugin/FirebaseFirestore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,12 @@ import FirebaseFirestore
}
}

@objc public func clearPersistence(completion: @escaping (Error?) -> Void) {
Firestore.firestore().clearPersistence { error in
completion(error)
}
}

@objc public func enableNetwork(completion: @escaping (Error?) -> Void) {
Firestore.firestore().enableNetwork { error in
completion(error)
Expand Down
11 changes: 11 additions & 0 deletions packages/firestore/ios/Plugin/FirebaseFirestorePlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,17 @@ public class FirebaseFirestorePlugin: CAPPlugin {
})
}

@objc func clearPersistence(_ call: CAPPluginCall) {
implementation?.clearPersistence(completion: { error in
if let error = error {
CAPLog.print("[", self.tag, "] ", error)
call.reject(error.localizedDescription)
return
}
call.resolve()
})
}

@objc func enableNetwork(_ call: CAPPluginCall) {
implementation?.enableNetwork(completion: { error in
if let error = error {
Expand Down
8 changes: 8 additions & 0 deletions packages/firestore/src/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ export interface FirebaseFirestorePlugin {
getCollectionGroup<T extends DocumentData = DocumentData>(
options: GetCollectionGroupOptions,
): Promise<GetCollectionGroupResult<T>>;
/**
* Clears the persistent storage. This includes pending writes and cached documents.
*
* Must be called after the app is shutdown or when the app is first initialized.
*
* @since 5.2.0
*/
clearPersistence(): Promise<void>;
/**
* Re-enables use of the network.
*
Expand Down
6 changes: 6 additions & 0 deletions packages/firestore/src/web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import type {
import {
addDoc,
and,
clearIndexedDbPersistence,
collection,
deleteDoc,
doc,
Expand Down Expand Up @@ -150,6 +151,11 @@ export class FirebaseFirestoreWeb
};
}

public async clearPersistence(): Promise<void> {
const firestore = getFirestore();
await clearIndexedDbPersistence(firestore);
}

public async enableNetwork(): Promise<void> {
throw this.unimplemented('Not implemented on web.');
}
Expand Down

0 comments on commit 5a42c3f

Please sign in to comment.