From ffe2306164ed7edfe5ab9d75b5122791037a852a Mon Sep 17 00:00:00 2001 From: Peter Argany Date: Wed, 30 Oct 2019 12:20:29 -0700 Subject: [PATCH] Deprecate bridge reload API [1/n] Summary: Testing the waters with an idea for unifying RN lifecycle with/without bridge. ### Motivation/Background RN bridge is being reloaded from [several different places](https://fburl.com/codesearch/ae3zeatt). When this happens, the bridge does a bunch of things: 1. Post `RCTBridgeWillReloadNotification` (RCTSurfacePresenter listens to this and reloads) 2. Invalidate batched bridge, which does: a. invalidate display link b. post `RCTBridgeWillInvalidateModules/RCTBridgeDidInvalidateModules` (TurboModuleManager listens to this and reloads modules) c. clear js thread state d. clear some local caches 3. Set up (reload bundle and batched bridge) In a bridgeless world, there isn't one thing which owns all these peices of infra, and can reload them. ### Plan Use `RCTReloadCommand` to handle reloads. This light class previously only handled CMD+R. The new workflow looks like this: 1. Anything which cares about reloads can register itself as a listener. (RCTBridge, RCTSurfacePresenter, TurboModuleManager...) 2. Anything that previously called `bridge reload` now calls `RCTTriggerReloadCommandListeners`. 3. Delete old notifications ### Alternate Plan Use yet another NSNotification. I like `RCTReloadCommand` better. ### Unknowns Looks like we log the reason for bridge reloading [here](https://fburl.com/diffusion/lc9jj8la), do we want to save this behaviour? Does anyone look at why bridge is being reloaded cc/Rick? If so, I can add back that functionality to `RCTReloadCommand`. It should be possible to customize the order/priority of what gets reloaded first. There may be some racy behaviour that I haven't thought about yet. Changelog: [iOS][Deprecated] Deprecate [bridge reload] API - prefer RCTReloadCommand Reviewed By: shergin Differential Revision: D17869635 fbshipit-source-id: 81f39eaa2c3ce08ea1bc6f2193684c2630d81a2d --- React/Base/RCTBridge.h | 6 +++--- React/Base/RCTBridge.m | 8 +++++++- React/Base/RCTReloadCommand.h | 13 +++++++++++-- 3 files changed, 21 insertions(+), 6 deletions(-) diff --git a/React/Base/RCTBridge.h b/React/Base/RCTBridge.h index 8c1269ca102a75..fede77b9f8c046 100644 --- a/React/Base/RCTBridge.h +++ b/React/Base/RCTBridge.h @@ -271,12 +271,12 @@ RCT_EXTERN void RCTEnableTurboModule(BOOL enabled); /** * Reload the bundle and reset executor & modules. Safe to call from any thread. */ -- (void)reload __deprecated_msg("Call reloadWithReason instead"); +- (void)reload __deprecated_msg("Use RCTReloadCommand instead"); /** * Reload the bundle and reset executor & modules. Safe to call from any thread. */ -- (void)reloadWithReason:(NSString *)reason; +- (void)reloadWithReason:(NSString *)reason __deprecated_msg("Use RCTReloadCommand instead"); /** * Handle notifications for a fast refresh. Safe to call from any thread. @@ -286,7 +286,7 @@ RCT_EXTERN void RCTEnableTurboModule(BOOL enabled); /** * Inform the bridge, and anything subscribing to it, that it should reload. */ -- (void)requestReload __deprecated_msg("Call reloadWithReason instead"); +- (void)requestReload __deprecated_msg("Use RCTReloadCommand instead"); /** * Says whether bridge has started receiving calls from javascript. diff --git a/React/Base/RCTBridge.m b/React/Base/RCTBridge.m index 1fe20d0cc01b3c..3cfb3ab82280f3 100644 --- a/React/Base/RCTBridge.m +++ b/React/Base/RCTBridge.m @@ -294,13 +294,16 @@ - (BOOL)moduleIsInitialized:(Class)moduleClass } /** - * Legacy reload, please use reloadWithReason and provide a reason for stats. + * DEPRECATED - please use RCTReloadCommand. */ - (void)reload { [self reloadWithReason:@"Unknown from bridge"]; } +/** + * DEPRECATED - please use RCTReloadCommand. + */ - (void)reloadWithReason:(NSString *)reason { #if RCT_ENABLE_INSPECTOR && !TARGET_OS_UIKITFORMAC @@ -328,6 +331,9 @@ - (void)onFastRefresh [[NSNotificationCenter defaultCenter] postNotificationName:RCTBridgeFastRefreshNotification object:self]; } +/** + * DEPRECATED - please use RCTReloadCommand. + */ - (void)requestReload { [self reloadWithReason:@"Requested from bridge"]; diff --git a/React/Base/RCTReloadCommand.h b/React/Base/RCTReloadCommand.h index aff7410097d0c5..30abcb336e0eaf 100644 --- a/React/Base/RCTReloadCommand.h +++ b/React/Base/RCTReloadCommand.h @@ -9,12 +9,21 @@ #import +/** + * A protocol which should be conformed to in order to be notified of RN reload events. These events can be + * created by CMD+R or dev menu during development, or anywhere the trigger is exposed to JS. + * The listener must also register itself using the method below. + */ @protocol RCTReloadListener - (void)didReceiveReloadCommand; @end -/** Registers a weakly-held observer of the Command+R reload key command. */ +/** + * Registers a weakly-held observer of RN reload events. + */ RCT_EXTERN void RCTRegisterReloadCommandListener(id listener); -/** Triggers a reload for all current listeners. You shouldn't need to use this directly in most cases. */ +/** + * Triggers a reload for all current listeners. + */ RCT_EXTERN void RCTTriggerReloadCommandListeners(void);