[firestore] Add support for enable / disable network

This commit is contained in:
Chris Bianca 2018-03-27 17:31:25 +01:00
parent 51721a950f
commit dd940b953b
5 changed files with 72 additions and 38 deletions

View File

@ -18,6 +18,7 @@ import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener; import com.google.android.gms.tasks.OnSuccessListener;
import com.google.android.gms.tasks.Task; import com.google.android.gms.tasks.Task;
import com.google.firebase.FirebaseApp; import com.google.firebase.FirebaseApp;
import com.google.firebase.firestore.FirebaseFirestoreSettings;
import com.google.firebase.firestore.Transaction; import com.google.firebase.firestore.Transaction;
import com.google.firebase.firestore.DocumentReference; import com.google.firebase.firestore.DocumentReference;
import com.google.firebase.firestore.FieldValue; import com.google.firebase.firestore.FieldValue;
@ -47,14 +48,42 @@ public class RNFirebaseFirestore extends ReactContextBaseJavaModule {
* REACT NATIVE METHODS * REACT NATIVE METHODS
*/ */
/** @ReactMethod
* @param enabled 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 @ReactMethod
public void enableLogging(Boolean enabled) { public void enableLogging(Boolean enabled) {
FirebaseFirestore.setLoggingEnabled(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 @ReactMethod
public void collectionGet(String appName, String path, ReadableArray filters, public void collectionGet(String appName, String path, ReadableArray filters,

View File

@ -185,11 +185,36 @@ RCT_EXPORT_METHOD(transactionBegin:(NSString *)appDisplayName
* TRANSACTIONS END * 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) { RCT_EXPORT_METHOD(enableLogging:(BOOL)enabled) {
[FIRFirestore enableLogging: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 RCT_EXPORT_METHOD(collectionGet:(NSString *)appDisplayName
path:(NSString *)path path:(NSString *)path
filters:(NSArray *)filters filters:(NSArray *)filters

2
lib/index.d.ts vendored
View File

@ -1149,7 +1149,9 @@ declare module "react-native-firebase" {
readonly app: App; readonly app: App;
batch(): WriteBatch; batch(): WriteBatch;
collection(collectionPath: string): CollectionReference; collection(collectionPath: string): CollectionReference;
disableNetwork(): Promise<void>
doc(documentPath: string): DocumentReference; doc(documentPath: string): DocumentReference;
enableNetwork(): Promise<void>
/** NOT SUPPORTED YET */ /** NOT SUPPORTED YET */
// enablePersistence(): Promise<void>; // enablePersistence(): Promise<void>;

View File

@ -16,6 +16,7 @@ import WriteBatch from './WriteBatch';
import TransactionHandler from './TransactionHandler'; import TransactionHandler from './TransactionHandler';
import Transaction from './Transaction'; import Transaction from './Transaction';
import INTERNALS from '../../utils/internals'; import INTERNALS from '../../utils/internals';
import { getNativeModule } from '../../utils/native';
import type DocumentSnapshot from './DocumentSnapshot'; import type DocumentSnapshot from './DocumentSnapshot';
import type App from '../core/app'; import type App from '../core/app';
@ -110,6 +111,10 @@ export default class Firestore extends ModuleBase {
return new CollectionReference(this, path); return new CollectionReference(this, path);
} }
disableNetwork(): void {
return getNativeModule(this).disableNetwork();
}
/** /**
* Gets a DocumentReference instance that refers to the document at the specified path. * 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); return new DocumentReference(this, path);
} }
enableNetwork(): Promise<void> {
return getNativeModule(this).enableNetwork();
}
/** /**
* Executes the given updateFunction and then attempts to commit the * Executes the given updateFunction and then attempts to commit the
* changes applied within the transaction. If any document read within * 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 * MISC

View File

@ -126,23 +126,10 @@ function firestoreTests({ before, describe, it, context, firebase }) {
}); });
}); });
context('disableNetwork()', () => { context('disable/enableNetwork()', () => {
it('should throw an unsupported error', () => { it('should work without error', async () => {
(() => { await firebase.native.firestore().disableNetwork();
firebase.native.firestore().disableNetwork(); await firebase.native.firestore().enableNetwork();
}).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.'
);
}); });
}); });