Skip to content

Commit 276630d

Browse files
committed
fix(storage, emulator): avoid calling useEmulator multiple times
In case of javascript hot reload, javascript will attempt to call useEmulator again, and the native layer will complain about the second call. This avoids the second call by tracking useEmulator call state at the native layer Fixes #5860 (again, but this time per-app, and for android as well)
1 parent 5fcfc44 commit 276630d

File tree

2 files changed

+14
-9
lines changed

2 files changed

+14
-9
lines changed

packages/storage/android/src/main/java/io/invertase/firebase/storage/ReactNativeFirebaseStorageModule.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@
4444
public class ReactNativeFirebaseStorageModule extends ReactNativeFirebaseModule {
4545
private static final String TAG = "Storage";
4646

47+
private static HashMap<String, String> emulatorConfigs = new HashMap<>();
48+
4749
ReactNativeFirebaseStorageModule(ReactApplicationContext reactContext) {
4850
super(reactContext, TAG);
4951
}
@@ -282,7 +284,10 @@ public void setMaxUploadRetryTime(String appName, double milliseconds, Promise p
282284
public void useEmulator(String appName, String host, int port, Promise promise) {
283285
FirebaseApp firebaseApp = FirebaseApp.getInstance(appName);
284286
FirebaseStorage firebaseStorage = FirebaseStorage.getInstance(firebaseApp);
285-
firebaseStorage.useEmulator(host, port);
287+
if (emulatorConfigs.get(appName) == null) {
288+
firebaseStorage.useEmulator(host, port);
289+
emulatorConfigs.put(appName, "true");
290+
}
286291
promise.resolve(null);
287292
}
288293

packages/storage/ios/RNFBStorage/RNFBStorageModule.m

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
// The iOS SDK has a short memory on settings, store these globally and set them in each time
3636
static NSString *emulatorHost = nil;
3737
static NSInteger emulatorPort = 0;
38-
static bool useEmulatorCalled = false;
38+
static NSMutableDictionary *emulatorConfigs;
3939
static NSTimeInterval maxDownloadRetryTime = 600;
4040
static NSTimeInterval maxUploadRetryTime = 600;
4141
static NSTimeInterval maxOperationRetryTime = 120;
@@ -56,6 +56,7 @@ - (id)init {
5656
static dispatch_once_t onceToken;
5757
dispatch_once(&onceToken, ^{
5858
PENDING_TASKS = [[NSMutableDictionary alloc] init];
59+
emulatorConfigs = [[NSMutableDictionary alloc] init];
5960
});
6061

6162
return self;
@@ -506,12 +507,10 @@ - (void)invalidate {
506507
: (NSInteger)port) {
507508
emulatorHost = host;
508509
emulatorPort = port;
509-
if (useEmulatorCalled == true) {
510-
return;
510+
if (!emulatorConfigs[firebaseApp.name]) {
511+
[[FIRStorage storageForApp:firebaseApp] useEmulatorWithHost:host port:port];
512+
emulatorConfigs[firebaseApp.name] = @YES;
511513
}
512-
513-
[[FIRStorage storageForApp:firebaseApp] useEmulatorWithHost:host port:port];
514-
useEmulatorCalled = true;
515514
}
516515

517516
/**
@@ -669,10 +668,11 @@ - (FIRStorageReference *)getReferenceFromUrl:(NSString *)url app:(FIRApp *)fireb
669668
storage = [FIRStorage storageForApp:firebaseApp URL:bucket];
670669

671670
NSLog(@"Setting emulator - host %@ port %ld", emulatorHost, (long)emulatorPort);
672-
if (![emulatorHost isEqual:[NSNull null]] && emulatorHost != nil && useEmulatorCalled == false) {
671+
if (![emulatorHost isEqual:[NSNull null]] && emulatorHost != nil &&
672+
!emulatorConfigs[firebaseApp.name]) {
673673
@try {
674674
[storage useEmulatorWithHost:emulatorHost port:emulatorPort];
675-
useEmulatorCalled = true;
675+
emulatorConfigs[firebaseApp.name] = @YES;
676676
} @catch (NSException *e) {
677677
NSLog(@"WARNING: Unable to set the Firebase Storage emulator settings. These must be set "
678678
@"before any usages of Firebase Storage. If you see this log after a hot "

0 commit comments

Comments
 (0)