fix r & d not working from Metro sometimes (#47624)
Some checks are pending
Label closed PR as merged and leave a comment / comment-and-label (push) Waiting to run
Publish Bumped Packages / publish_bumped_packages (push) Waiting to run
Update node modules cache / update_node_modules_cache (push) Waiting to run

Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/47624

Changelog:
[iOS][Fixed] - fix `r` & `d` not working from Metro sometimes

While investigating these bugs, I've come across some cases where `r` (Reload) & `d` (Open Dev Menu) not working in Metro.

* T206141946 / [WP: Reconnecting dev tools does not work after restarting the app](https://fb.workplace.com/groups/rn.debugger.feedback/posts/1107620434125533)
* T206754760 / [WP: Can't launch DevTools from Metro sometimes](https://fb.workplace.com/groups/rn.debugger.feedback/posts/1112235073664069/)

This is because when we
1. Start app without Metro
1. Start Metro
1. Reload from Dev Menu (rage shake)

`RCTPackagerConnection` did not get notified about the change in bundle URL. It'd stay "listening" to the commands from the local bundle instead of Metro
.

Reviewed By: robhogan

Differential Revision: D65973309

fbshipit-source-id: a67a58b405bb78dfe56b814f2ec0bbee9e530e46
This commit is contained in:
Edmond Chui 2024-11-14 20:58:10 -08:00 committed by Facebook GitHub Bot
parent 74ed831a33
commit 9a60038a40

View File

@ -20,6 +20,7 @@
#import <React/RCTLog.h>
#import <React/RCTPackagerClient.h>
#import <React/RCTReconnectingWebSocket.h>
#import <React/RCTReloadCommand.h>
#import <React/RCTUtils.h>
#if RCT_DEV
@ -44,6 +45,7 @@ struct Registration {
NSString *_serverHostPortForSocket;
NSString *_serverSchemeForSocket;
id _bundleURLChangeObserver;
id _reloadWithPotentiallyNewURLObserver;
uint32_t _nextToken;
std::vector<Registration<RCTNotificationHandler>> _notificationRegistrations;
std::vector<Registration<RCTRequestHandler>> _requestRegistrations;
@ -78,6 +80,13 @@ struct Registration {
usingBlock:^(NSNotification *_Nonnull __unused note) {
[weakSelf bundleURLSettingsChanged];
}];
_reloadWithPotentiallyNewURLObserver =
[[NSNotificationCenter defaultCenter] addObserverForName:RCTTriggerReloadCommandNotification
object:nil
queue:[NSOperationQueue mainQueue]
usingBlock:^(NSNotification *_Nonnull __unused note) {
[weakSelf bundleURLSettingsChanged];
}];
}
return self;
}
@ -119,6 +128,8 @@ static RCTReconnectingWebSocket *socketForLocation(NSString *const serverHostPor
}
[[NSNotificationCenter defaultCenter] removeObserver:_bundleURLChangeObserver];
_bundleURLChangeObserver = nil;
[[NSNotificationCenter defaultCenter] removeObserver:_reloadWithPotentiallyNewURLObserver];
_reloadWithPotentiallyNewURLObserver = nil;
_socketConnected = NO;
[_socket stop];
_socket = nil;