Skip to content

Commit

Permalink
fix(storage, emulator): avoid calling useEmulator multiple times
Browse files Browse the repository at this point in the history
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)
  • Loading branch information
mikehardy committed Oct 19, 2022
1 parent 5fcfc44 commit 276630d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@
public class ReactNativeFirebaseStorageModule extends ReactNativeFirebaseModule {
private static final String TAG = "Storage";

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

ReactNativeFirebaseStorageModule(ReactApplicationContext reactContext) {
super(reactContext, TAG);
}
Expand Down Expand Up @@ -282,7 +284,10 @@ public void setMaxUploadRetryTime(String appName, double milliseconds, Promise p
public void useEmulator(String appName, String host, int port, Promise promise) {
FirebaseApp firebaseApp = FirebaseApp.getInstance(appName);
FirebaseStorage firebaseStorage = FirebaseStorage.getInstance(firebaseApp);
firebaseStorage.useEmulator(host, port);
if (emulatorConfigs.get(appName) == null) {
firebaseStorage.useEmulator(host, port);
emulatorConfigs.put(appName, "true");
}
promise.resolve(null);
}

Expand Down
16 changes: 8 additions & 8 deletions packages/storage/ios/RNFBStorage/RNFBStorageModule.m
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
// The iOS SDK has a short memory on settings, store these globally and set them in each time
static NSString *emulatorHost = nil;
static NSInteger emulatorPort = 0;
static bool useEmulatorCalled = false;
static NSMutableDictionary *emulatorConfigs;
static NSTimeInterval maxDownloadRetryTime = 600;
static NSTimeInterval maxUploadRetryTime = 600;
static NSTimeInterval maxOperationRetryTime = 120;
Expand All @@ -56,6 +56,7 @@ - (id)init {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
PENDING_TASKS = [[NSMutableDictionary alloc] init];
emulatorConfigs = [[NSMutableDictionary alloc] init];
});

return self;
Expand Down Expand Up @@ -506,12 +507,10 @@ - (void)invalidate {
: (NSInteger)port) {
emulatorHost = host;
emulatorPort = port;
if (useEmulatorCalled == true) {
return;
if (!emulatorConfigs[firebaseApp.name]) {
[[FIRStorage storageForApp:firebaseApp] useEmulatorWithHost:host port:port];
emulatorConfigs[firebaseApp.name] = @YES;
}

[[FIRStorage storageForApp:firebaseApp] useEmulatorWithHost:host port:port];
useEmulatorCalled = true;
}

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

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

0 comments on commit 276630d

Please sign in to comment.