From afde9da93d88d3124856aaaeaa58ff644bda42a1 Mon Sep 17 00:00:00 2001 From: Ben Roth Date: Wed, 7 Sep 2016 17:24:23 -0700 Subject: [PATCH] Scope RCTReloadNotification to one bridge Summary: At the moment, posting RCTReloadNotification in any circumstance causes all RCTBridge instances to reload. This change scopes the notification to the bridge for which it was intended. Closes https://github.com/facebook/react-native/pull/8762 Differential Revision: D3831914 fbshipit-source-id: ff29574f574ecd1a403057ddd0458dea38f0136e --- React/Base/RCTBatchedBridge.m | 5 +++++ React/Base/RCTBridge.h | 8 +++++++- React/Base/RCTBridge.m | 16 ++++++++-------- React/Modules/RCTDevMenu.m | 4 +--- React/Modules/RCTExceptionsManager.m | 2 +- React/Modules/RCTRedBox.m | 2 +- React/Profiler/RCTProfile.m | 3 +-- 7 files changed, 24 insertions(+), 16 deletions(-) diff --git a/React/Base/RCTBatchedBridge.m b/React/Base/RCTBatchedBridge.m index 867626233ab..0fe7c5210ff 100644 --- a/React/Base/RCTBatchedBridge.m +++ b/React/Base/RCTBatchedBridge.m @@ -621,6 +621,11 @@ RCT_NOT_IMPLEMENTED(- (instancetype)initWithBundleURL:(__unused NSURL *)bundleUR [_parentBridge reload]; } +- (void)requestReload +{ + [_parentBridge requestReload]; +} + - (Class)executorClass { return _parentBridge.executorClass ?: [RCTJSCExecutor class]; diff --git a/React/Base/RCTBridge.h b/React/Base/RCTBridge.h index 944997666ce..b87821c9b36 100644 --- a/React/Base/RCTBridge.h +++ b/React/Base/RCTBridge.h @@ -21,8 +21,9 @@ /** * This notification triggers a reload of all bridges currently running. + * Deprecated, use RCTBridge::requestReload instead. */ -RCT_EXTERN NSString *const RCTReloadNotification; +RCT_EXTERN NSString *const RCTReloadNotification DEPRECATED_ATTRIBUTE; /** * This notification fires when the bridge starts loading the JS bundle. @@ -183,6 +184,11 @@ RCT_EXTERN NSString *RCTBridgeModuleNameForClass(Class bridgeModuleClass); */ - (void)reload; +/** + * Inform the bridge, and anything subscribing to it, that it should reload. + */ +- (void)requestReload; + /** * Says whether bridge has started recieving calls from javascript. */ diff --git a/React/Base/RCTBridge.m b/React/Base/RCTBridge.m index 16086005bbd..5743229906c 100644 --- a/React/Base/RCTBridge.m +++ b/React/Base/RCTBridge.m @@ -161,21 +161,15 @@ RCT_NOT_IMPLEMENTED(- (instancetype)init) { RCTAssertMainQueue(); - [[NSNotificationCenter defaultCenter] addObserver:self - selector:@selector(reload) - name:RCTReloadNotification - object:nil]; - #if TARGET_IPHONE_SIMULATOR RCTKeyCommands *commands = [RCTKeyCommands sharedInstance]; // reload in current mode + __weak typeof(self) weakSelf = self; [commands registerKeyCommandWithInput:@"r" modifierFlags:UIKeyModifierCommand action:^(__unused UIKeyCommand *command) { - [[NSNotificationCenter defaultCenter] postNotificationName:RCTReloadNotification - object:nil - userInfo:nil]; + [weakSelf requestReload]; }]; #endif } @@ -235,6 +229,12 @@ RCT_NOT_IMPLEMENTED(- (instancetype)init) }); } +- (void)requestReload +{ + [[NSNotificationCenter defaultCenter] postNotificationName:RCTReloadNotification object:self]; + [self reload]; +} + - (void)setUp { RCT_PROFILE_BEGIN_EVENT(0, @"-[RCTBridge setUp]", nil); diff --git a/React/Modules/RCTDevMenu.m b/React/Modules/RCTDevMenu.m index 66181785b1a..b6ad245aeee 100644 --- a/React/Modules/RCTDevMenu.m +++ b/React/Modules/RCTDevMenu.m @@ -566,9 +566,7 @@ RCT_EXPORT_METHOD(show) RCT_EXPORT_METHOD(reload) { - [[NSNotificationCenter defaultCenter] postNotificationName:RCTReloadNotification - object:nil - userInfo:nil]; + [_bridge requestReload]; } - (void)setShakeToShow:(BOOL)shakeToShow diff --git a/React/Modules/RCTExceptionsManager.m b/React/Modules/RCTExceptionsManager.m index 791c163f16d..5e7b08b7198 100644 --- a/React/Modules/RCTExceptionsManager.m +++ b/React/Modules/RCTExceptionsManager.m @@ -57,7 +57,7 @@ RCT_EXPORT_METHOD(reportFatalException:(NSString *)message static NSUInteger reloadRetries = 0; if (!RCT_DEBUG && reloadRetries < _maxReloadAttempts) { reloadRetries++; - [[NSNotificationCenter defaultCenter] postNotificationName:RCTReloadNotification object:nil]; + [_bridge requestReload]; } else { NSString *description = [@"Unhandled JS Exception: " stringByAppendingString:message]; NSDictionary *errorInfo = @{ NSLocalizedDescriptionKey: description, RCTJSStackTraceKey: stack }; diff --git a/React/Modules/RCTRedBox.m b/React/Modules/RCTRedBox.m index 1a60451eb06..f5434b5ef34 100644 --- a/React/Modules/RCTRedBox.m +++ b/React/Modules/RCTRedBox.m @@ -450,7 +450,7 @@ RCT_EXPORT_METHOD(dismiss) } - (void)reloadFromRedBoxWindow:(__unused RCTRedBoxWindow *)redBoxWindow { - [[NSNotificationCenter defaultCenter] postNotificationName:RCTReloadNotification object:nil userInfo:nil]; + [_bridge requestReload]; } @end diff --git a/React/Profiler/RCTProfile.m b/React/Profiler/RCTProfile.m index 7a9edf8be82..8026d62d139 100644 --- a/React/Profiler/RCTProfile.m +++ b/React/Profiler/RCTProfile.m @@ -363,8 +363,7 @@ void RCTProfileUnhookModules(RCTBridge *bridge) + (void)reload { - [[NSNotificationCenter defaultCenter] postNotificationName:RCTReloadNotification - object:NULL]; + [RCTProfilingBridge() requestReload]; } + (void)toggle:(UIButton *)target