[firestore] Add support for enable / disable network
This commit is contained in:
parent
51721a950f
commit
dd940b953b
|
@ -18,6 +18,7 @@ import com.google.android.gms.tasks.OnFailureListener;
|
|||
import com.google.android.gms.tasks.OnSuccessListener;
|
||||
import com.google.android.gms.tasks.Task;
|
||||
import com.google.firebase.FirebaseApp;
|
||||
import com.google.firebase.firestore.FirebaseFirestoreSettings;
|
||||
import com.google.firebase.firestore.Transaction;
|
||||
import com.google.firebase.firestore.DocumentReference;
|
||||
import com.google.firebase.firestore.FieldValue;
|
||||
|
@ -47,14 +48,42 @@ public class RNFirebaseFirestore extends ReactContextBaseJavaModule {
|
|||
* REACT NATIVE METHODS
|
||||
*/
|
||||
|
||||
/**
|
||||
* @param enabled
|
||||
*/
|
||||
@ReactMethod
|
||||
public void disableNetwork(String appName, final Promise promise) {
|
||||
getFirestoreForApp(appName).disableNetwork().addOnCompleteListener(new OnCompleteListener<Void>() {
|
||||
@Override
|
||||
public void onComplete(@NonNull Task<Void> task) {
|
||||
if (task.isSuccessful()) {
|
||||
Log.d(TAG, "disableNetwork:onComplete:success");
|
||||
promise.resolve(null);
|
||||
} else {
|
||||
Log.e(TAG, "disableNetwork:onComplete:failure", task.getException());
|
||||
RNFirebaseFirestore.promiseRejectException(promise, (FirebaseFirestoreException)task.getException());
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@ReactMethod
|
||||
public void enableLogging(Boolean enabled) {
|
||||
FirebaseFirestore.setLoggingEnabled(enabled);
|
||||
}
|
||||
|
||||
@ReactMethod
|
||||
public void enableNetwork(String appName, final Promise promise) {
|
||||
getFirestoreForApp(appName).enableNetwork().addOnCompleteListener(new OnCompleteListener<Void>() {
|
||||
@Override
|
||||
public void onComplete(@NonNull Task<Void> task) {
|
||||
if (task.isSuccessful()) {
|
||||
Log.d(TAG, "enableNetwork:onComplete:success");
|
||||
promise.resolve(null);
|
||||
} else {
|
||||
Log.e(TAG, "enableNetwork:onComplete:failure", task.getException());
|
||||
RNFirebaseFirestore.promiseRejectException(promise, (FirebaseFirestoreException)task.getException());
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@ReactMethod
|
||||
public void collectionGet(String appName, String path, ReadableArray filters,
|
||||
|
|
|
@ -185,11 +185,36 @@ RCT_EXPORT_METHOD(transactionBegin:(NSString *)appDisplayName
|
|||
* TRANSACTIONS END
|
||||
*/
|
||||
|
||||
RCT_EXPORT_METHOD(disableNetwork:(NSString *)appDisplayName
|
||||
resolver:(RCTPromiseResolveBlock) resolve
|
||||
rejecter:(RCTPromiseRejectBlock) reject) {
|
||||
FIRFirestore *firestore = [RNFirebaseFirestore getFirestoreForApp:appDisplayName];
|
||||
[firestore disableNetworkWithCompletion:^(NSError * _Nullable error) {
|
||||
if (error) {
|
||||
[RNFirebaseFirestore promiseRejectException:reject error:error];
|
||||
} else {
|
||||
resolve(nil);
|
||||
}
|
||||
}];
|
||||
}
|
||||
|
||||
RCT_EXPORT_METHOD(enableLogging:(BOOL)enabled) {
|
||||
[FIRFirestore enableLogging:enabled];
|
||||
}
|
||||
|
||||
RCT_EXPORT_METHOD(enableNetwork:(NSString *)appDisplayName
|
||||
resolver:(RCTPromiseResolveBlock) resolve
|
||||
rejecter:(RCTPromiseRejectBlock) reject) {
|
||||
FIRFirestore *firestore = [RNFirebaseFirestore getFirestoreForApp:appDisplayName];
|
||||
[firestore enableNetworkWithCompletion:^(NSError * _Nullable error) {
|
||||
if (error) {
|
||||
[RNFirebaseFirestore promiseRejectException:reject error:error];
|
||||
} else {
|
||||
resolve(nil);
|
||||
}
|
||||
}];
|
||||
}
|
||||
|
||||
RCT_EXPORT_METHOD(collectionGet:(NSString *)appDisplayName
|
||||
path:(NSString *)path
|
||||
filters:(NSArray *)filters
|
||||
|
|
|
@ -1149,7 +1149,9 @@ declare module "react-native-firebase" {
|
|||
readonly app: App;
|
||||
batch(): WriteBatch;
|
||||
collection(collectionPath: string): CollectionReference;
|
||||
disableNetwork(): Promise<void>
|
||||
doc(documentPath: string): DocumentReference;
|
||||
enableNetwork(): Promise<void>
|
||||
|
||||
/** NOT SUPPORTED YET */
|
||||
// enablePersistence(): Promise<void>;
|
||||
|
|
|
@ -16,6 +16,7 @@ import WriteBatch from './WriteBatch';
|
|||
import TransactionHandler from './TransactionHandler';
|
||||
import Transaction from './Transaction';
|
||||
import INTERNALS from '../../utils/internals';
|
||||
import { getNativeModule } from '../../utils/native';
|
||||
|
||||
import type DocumentSnapshot from './DocumentSnapshot';
|
||||
import type App from '../core/app';
|
||||
|
@ -110,6 +111,10 @@ export default class Firestore extends ModuleBase {
|
|||
return new CollectionReference(this, path);
|
||||
}
|
||||
|
||||
disableNetwork(): void {
|
||||
return getNativeModule(this).disableNetwork();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a DocumentReference instance that refers to the document at the specified path.
|
||||
*
|
||||
|
@ -125,6 +130,10 @@ export default class Firestore extends ModuleBase {
|
|||
return new DocumentReference(this, path);
|
||||
}
|
||||
|
||||
enableNetwork(): Promise<void> {
|
||||
return getNativeModule(this).enableNetwork();
|
||||
}
|
||||
|
||||
/**
|
||||
* Executes the given updateFunction and then attempts to commit the
|
||||
* changes applied within the transaction. If any document read within
|
||||
|
@ -156,24 +165,6 @@ export default class Firestore extends ModuleBase {
|
|||
);
|
||||
}
|
||||
|
||||
enableNetwork(): void {
|
||||
throw new Error(
|
||||
INTERNALS.STRINGS.ERROR_UNSUPPORTED_MODULE_METHOD(
|
||||
'firestore',
|
||||
'enableNetwork'
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
disableNetwork(): void {
|
||||
throw new Error(
|
||||
INTERNALS.STRINGS.ERROR_UNSUPPORTED_MODULE_METHOD(
|
||||
'firestore',
|
||||
'disableNetwork'
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* -------------
|
||||
* MISC
|
||||
|
|
|
@ -126,23 +126,10 @@ function firestoreTests({ before, describe, it, context, firebase }) {
|
|||
});
|
||||
});
|
||||
|
||||
context('disableNetwork()', () => {
|
||||
it('should throw an unsupported error', () => {
|
||||
(() => {
|
||||
firebase.native.firestore().disableNetwork();
|
||||
}).should.throw(
|
||||
'firebase.firestore().disableNetwork() is unsupported by the native Firebase SDKs.'
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
context('enableNetwork()', () => {
|
||||
it('should throw an unsupported error', () => {
|
||||
(() => {
|
||||
firebase.native.firestore().enableNetwork();
|
||||
}).should.throw(
|
||||
'firebase.firestore().enableNetwork() is unsupported by the native Firebase SDKs.'
|
||||
);
|
||||
context('disable/enableNetwork()', () => {
|
||||
it('should work without error', async () => {
|
||||
await firebase.native.firestore().disableNetwork();
|
||||
await firebase.native.firestore().enableNetwork();
|
||||
});
|
||||
});
|
||||
|
||||
|
|
Loading…
Reference in New Issue