From c90ad74ba3584373f75df18e36190253c3b1222d Mon Sep 17 00:00:00 2001 From: Edmond Chui Date: Thu, 14 Nov 2024 15:08:20 -0800 Subject: [PATCH] fix `r` & `d` not working from Metro sometimes Summary: 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 . Differential Revision: D65973309 --- .../React/DevSupport/RCTPackagerConnection.mm | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/packages/react-native/React/DevSupport/RCTPackagerConnection.mm b/packages/react-native/React/DevSupport/RCTPackagerConnection.mm index bd4075ca1b3294..39419d60e12036 100644 --- a/packages/react-native/React/DevSupport/RCTPackagerConnection.mm +++ b/packages/react-native/React/DevSupport/RCTPackagerConnection.mm @@ -20,6 +20,7 @@ #import #import #import +#import #import #if RCT_DEV @@ -44,6 +45,7 @@ @implementation RCTPackagerConnection { NSString *_serverHostPortForSocket; NSString *_serverSchemeForSocket; id _bundleURLChangeObserver; + id _reloadWithPotentiallyNewURLObserver; uint32_t _nextToken; std::vector> _notificationRegistrations; std::vector> _requestRegistrations; @@ -78,6 +80,13 @@ - (instancetype)init 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 @@ - (void)stop } [[NSNotificationCenter defaultCenter] removeObserver:_bundleURLChangeObserver]; _bundleURLChangeObserver = nil; + [[NSNotificationCenter defaultCenter] removeObserver:_reloadWithPotentiallyNewURLObserver]; + _reloadWithPotentiallyNewURLObserver = nil; _socketConnected = NO; [_socket stop]; _socket = nil;