Skip to content

Commit

Permalink
fix(firestore, emulator): avoid double calls to useEmulator
Browse files Browse the repository at this point in the history
covers case where javascript hot-reloads and loses state then calls again
by tracking state of calls natively

Fixes #5723
  • Loading branch information
mikehardy committed Oct 19, 2022
1 parent 276630d commit 4e0d188
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,14 @@
import io.invertase.firebase.common.UniversalFirebaseModule;
import io.invertase.firebase.common.UniversalFirebasePreferences;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

public class UniversalFirebaseFirestoreModule extends UniversalFirebaseModule {

private static HashMap<String, String> emulatorConfigs = new HashMap<>();

UniversalFirebaseFirestoreModule(Context context, String serviceName) {
super(context, serviceName);
}
Expand All @@ -49,7 +52,10 @@ Task<Void> useEmulator(String appName, String host, int port) {
return Tasks.call(
getExecutor(),
() -> {
getFirestoreForApp(appName).useEmulator(host, port);
if (emulatorConfigs.get(appName) == null) {
emulatorConfigs.put(appName, "true");
getFirestoreForApp(appName).useEmulator(host, port);
}
return null;
});
}
Expand Down
22 changes: 15 additions & 7 deletions packages/firestore/ios/RNFBFirestore/RNFBFirestoreModule.m
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
#import "RNFBFirestoreCommon.h"
#import "RNFBPreferences.h"

NSMutableDictionary *emulatorConfigs;

@implementation RNFBFirestoreModule
#pragma mark -
#pragma mark Module Setup
Expand Down Expand Up @@ -142,13 +144,19 @@ + (BOOL)requiresMainQueueSetup {
: (FIRApp *)firebaseApp
: (nonnull NSString *)host
: (NSInteger)port) {
FIRFirestore *firestore = [RNFBFirestoreCommon getFirestoreForApp:firebaseApp];
[firestore useEmulatorWithHost:host port:port];

// It is not sufficient to just use emulator. You have toggle SSL off too.
FIRFirestoreSettings *settings = firestore.settings;
settings.sslEnabled = FALSE;
firestore.settings = settings;
if (emulatorConfigs == nil) {
emulatorConfigs = [[NSMutableDictionary alloc] init];
}
if (!emulatorConfigs[firebaseApp.name]) {
FIRFirestore *firestore = [RNFBFirestoreCommon getFirestoreForApp:firebaseApp];
[firestore useEmulatorWithHost:host port:port];
emulatorConfigs[firebaseApp.name] = @YES;

// It is not sufficient to just use emulator. You have toggle SSL off too.
FIRFirestoreSettings *settings = firestore.settings;
settings.sslEnabled = FALSE;
firestore.settings = settings;
}
}

RCT_EXPORT_METHOD(waitForPendingWrites
Expand Down

0 comments on commit 4e0d188

Please sign in to comment.