Pull RCTViewRegistry attachment out of TurboModuleManager

Summary:
## Rationale
The TurboModuleManager should only be concerned with modules. The bridge and RCTInstance integrate the TurboModuleManager with the rest of React Native. Therefore, abstractions used by TurboModules that reach into the rest of React Native should be attached by the bridge or by RCTInstance.

## Changes
In this diff, we pull RCTViewRegistry attachment out of the TurboModuleManager. In bridge mode, it'll be attached to TurboModule by the bridge. In bridgeless mode, it'll be attached to TurboModules by the RCTInstance.

Changelog: [Internal]

Reviewed By: PeteTheHeat

Differential Revision: D28086320

fbshipit-source-id: 9d99835bdbb66bb6a41fbd0d8a3970cefae16b81
This commit is contained in:
Ramanpreet Nara
2021-04-30 16:40:16 -07:00
committed by Facebook GitHub Bot
parent 8eceee744e
commit e5080e6171
6 changed files with 36 additions and 31 deletions
+7
View File
@@ -231,6 +231,13 @@ RCT_EXTERN void RCTEnableTurboModuleBlockGuard(BOOL enabled);
*/
- (void)setRCTTurboModuleRegistry:(id<RCTTurboModuleRegistry>)turboModuleRegistry;
/**
* This hook is called by the TurboModule infra with every TurboModule that's created.
* It allows the bridge to attach properties to TurboModules that give TurboModules
* access to Bridge APIs.
*/
- (void)attachBridgeAPIsToTurboModule:(id<RCTTurboModule>)module;
/**
* Convenience method for retrieving all modules conforming to a given protocol.
* Modules will be synchronously instantiated if they haven't already been,
+5
View File
@@ -230,6 +230,11 @@ RCT_NOT_IMPLEMENTED(-(instancetype)init)
[self.batchedBridge setRCTTurboModuleRegistry:turboModuleRegistry];
}
- (void)attachBridgeAPIsToTurboModule:(id<RCTTurboModule>)module
{
[self.batchedBridge attachBridgeAPIsToTurboModule:module];
}
- (void)didReceiveReloadCommand
{
#if RCT_ENABLE_INSPECTOR
+1 -1
View File
@@ -133,7 +133,7 @@ RCT_EXTERN_C_END
* viewRegistry_DEPRECATED = _viewRegistry_DEPRECATED;`. If using Swift, add
* `@objc var viewRegistry_DEPRECATED: RCTViewRegistry!` to your module.
*/
@property (nonatomic, weak, readonly) RCTViewRegistry *viewRegistry_DEPRECATED;
@property (nonatomic, weak, readwrite) RCTViewRegistry *viewRegistry_DEPRECATED;
/**
* A reference to the RCTBridge. Useful for modules that require access
+15
View File
@@ -249,6 +249,21 @@ struct RCTInstanceCallback : public InstanceCallback {
[_objCModuleRegistry setTurboModuleRegistry:_turboModuleRegistry];
}
- (void)attachBridgeAPIsToTurboModule:(id<RCTTurboModule>)module
{
id<RCTBridgeModule> bridgeModule = (id<RCTBridgeModule>)module;
/**
* Attach the RCTViewRegistry to this TurboModule, which allows this TurboModule
* To query a React component's UIView, given its reactTag.
*
* Usage: In the NativeModule @implementation, include:
* `@synthesize viewRegistry_DEPRECATED = _viewRegistry_DEPRECATED`
*/
if ([bridgeModule respondsToSelector:@selector(setViewRegistry_DEPRECATED:)]) {
bridgeModule.viewRegistry_DEPRECATED = _viewRegistry_DEPRECATED;
}
}
- (std::shared_ptr<MessageQueueThread>)jsMessageThread
{
return _jsMessageThread;
@@ -45,7 +45,6 @@
jsInvoker:(std::shared_ptr<facebook::react::CallInvoker>)jsInvoker;
- (void)installJSBindingWithRuntimeExecutor:(facebook::react::RuntimeExecutor)runtimeExecutor;
- (void)setBridgelessComponentViewProvider:(RCTBridgelessComponentViewProvider)bridgelessComponentViewProvider;
- (void)invalidate;
@@ -174,7 +174,6 @@ static Class getFallbackClassFromName(const char *name)
std::atomic<bool> _invalidating;
RCTModuleRegistry *_moduleRegistry;
RCTViewRegistry *_viewRegistry_DEPRECATED;
}
- (instancetype)initWithBridge:(RCTBridge *)bridge
@@ -190,9 +189,6 @@ static Class getFallbackClassFromName(const char *name)
[_moduleRegistry setBridge:bridge];
[_moduleRegistry setTurboModuleRegistry:self];
_viewRegistry_DEPRECATED = [RCTViewRegistry new];
[_viewRegistry_DEPRECATED setBridge:bridge];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(bridgeWillInvalidateModules:)
name:RCTBridgeWillInvalidateModulesNotification
@@ -205,11 +201,6 @@ static Class getFallbackClassFromName(const char *name)
return self;
}
- (void)setBridgelessComponentViewProvider:(RCTBridgelessComponentViewProvider)viewProvider
{
[_viewRegistry_DEPRECATED setBridgelessComponentViewProvider:viewProvider];
}
- (void)notifyAboutTurboModuleSetup:(const char *)name
{
NSString *moduleName = [[NSString alloc] initWithUTF8String:name];
@@ -563,25 +554,6 @@ static Class getFallbackClassFromName(const char *name)
}
}
/**
* Attach the RCTViewRegistry to this TurboModule, which allows this TurboModule
* To query a React component's UIView, given its reactTag.
*
* Usage: In the NativeModule @implementation, include:
* `@synthesize viewRegistry_DEPRECATED = _viewRegistry_DEPRECATED`
*/
if ([module respondsToSelector:@selector(viewRegistry_DEPRECATED)] && _viewRegistry_DEPRECATED) {
@try {
[(id)module setValue:_viewRegistry_DEPRECATED forKey:@"viewRegistry_DEPRECATED"];
} @catch (NSException *exception) {
RCTLogError(
@"%@ has no setter or ivar for its module registry, which is not "
"permitted. You must either @synthesize the viewRegistry_DEPRECATED property, "
"or provide your own setter method.",
RCTBridgeModuleNameForClass([module class]));
}
}
/**
* Some modules need their own queues, but don't provide any, so we need to create it for them.
* These modules typically have the following:
@@ -626,6 +598,13 @@ static Class getFallbackClassFromName(const char *name)
}
}
/**
* Decorate TurboModules with bridgeless-compatible APIs that call into the bridge.
*/
if (_bridge) {
[_bridge attachBridgeAPIsToTurboModule:module];
}
/**
* Attach method queue to id<RCTTurboModule> object.
* This is necessary because the id<RCTTurboModule> object can be eagerly created/initialized before the method
@@ -645,7 +624,7 @@ static Class getFallbackClassFromName(const char *name)
RCTModuleData *data = [[RCTModuleData alloc] initWithModuleInstance:(id<RCTBridgeModule>)module
bridge:_bridge
moduleRegistry:_moduleRegistry
viewRegistry_DEPRECATED:_viewRegistry_DEPRECATED];
viewRegistry_DEPRECATED:nil];
[_bridge registerModuleForFrameUpdates:(id<RCTBridgeModule>)module withModuleData:data];
}