Skip to content

Commit

Permalink
1/5 Refactor CxxBridge: Introduce RCTBridgeModuleDecorator to attach @…
Browse files Browse the repository at this point in the history
…synthesize ivars to RCTTurboModules, in Bridge mode

Summary:
Changelog: [iOS][Internal] Refactor CxxBridge: Introduce RCTBridgeModuleDecorator to attach synthesize ivars to RCTTurboModules, in Bridge mode

This doesn't change any logic. RCTBridgeModuleDecorator was created to consolidate several nearly identical implementations of the `attachInteropAPIsToModule` method to one place.

Most importantly, it allows us to attach interop APIs in RCTBridgeModuleDecorator to RCTViewManagers in diff 4/4, using  `attachInteropAPIsToModule`. Before this stack, these four synthesize ivars in RCTViewManagers are nil in Bridgeless mode, and point to instances in Bridge mode.

# Context
These are used in RCTBridgeModules to access APIs for view managers. These APIs are necessary and compatible with Bridgeless mode.

*  synthesize viewRegistry_DEPRECATED
*  synthesize bundleManager
*  synthesize callableJSModules
*  synthesize moduleRegistry

Reviewed By: RSNara

Differential Revision: D34437802

fbshipit-source-id: b773d511cf877d4896436fabf4893c978e5f8dd9
  • Loading branch information
p-sun authored and facebook-github-bot committed Feb 25, 2022
1 parent dc50308 commit 94b1b8a
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 49 deletions.
1 change: 1 addition & 0 deletions BUCK
Expand Up @@ -245,6 +245,7 @@ REACT_PUBLIC_HEADERS = {
"React/RCTBridgeDelegate.h": RCTBASE_PATH + "RCTBridgeDelegate.h",
"React/RCTBridgeMethod.h": RCTBASE_PATH + "RCTBridgeMethod.h",
"React/RCTBridgeModule.h": RCTBASE_PATH + "RCTBridgeModule.h",
"React/RCTBridgeModuleDecorator.h": RCTBASE_PATH + "RCTBridgeModuleDecorator.h",
"React/RCTBundleURLProvider.h": RCTBASE_PATH + "RCTBundleURLProvider.h",
"React/RCTComponent.h": RCTVIEWS_PATH + "RCTComponent.h",
"React/RCTComponentData.h": RCTVIEWS_PATH + "RCTComponentData.h",
Expand Down
35 changes: 35 additions & 0 deletions React/Base/RCTBridgeModuleDecorator.h
@@ -0,0 +1,35 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

#import "RCTBridgeModule.h"

@class RCTBundleManager;
@class RCTCallableJSModules;
@class RCTModuleRegistry;
@class RCTViewRegistry;

/**
RCTBridgeModuleDecorator contains instances that can be intialized with @synthesize
in RCTBridgeModules. For the Fabric interop layer.
In Bridgeless, @synthesize ivars are passed from RCTBridgeModuleDecorator.
In Bridge, @synthesize ivars are passed from RCTModuleData.
*/
@interface RCTBridgeModuleDecorator : NSObject
@property (nonatomic, strong, readonly) RCTViewRegistry *viewRegistry_DEPRECATED;
@property (nonatomic, strong, readonly) RCTModuleRegistry *moduleRegistry;
@property (nonatomic, strong, readonly) RCTBundleManager *bundleManager;
@property (nonatomic, strong, readonly) RCTCallableJSModules *callableJSModules;

- (instancetype)initWithViewRegistry:(RCTViewRegistry *)viewRegistry
moduleRegistry:(RCTModuleRegistry *)moduleRegistry
bundleManager:(RCTBundleManager *)bundleManager
callableJSModules:(RCTCallableJSModules *)callableJSModules;

- (void)attachInteropAPIsToModule:(id<RCTBridgeModule>)bridgeModule;

@end
73 changes: 73 additions & 0 deletions React/Base/RCTBridgeModuleDecorator.m
@@ -0,0 +1,73 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

#include "RCTBridgeModuleDecorator.h"

@implementation RCTBridgeModuleDecorator

- (instancetype)initWithViewRegistry:(RCTViewRegistry *)viewRegistry
moduleRegistry:(RCTModuleRegistry *)moduleRegistry
bundleManager:(RCTBundleManager *)bundleManager
callableJSModules:(RCTCallableJSModules *)callableJSModules
{
if (self = [super init]) {
_viewRegistry_DEPRECATED = viewRegistry;
_moduleRegistry = moduleRegistry;
_bundleManager = bundleManager;
_callableJSModules = callableJSModules;
}
return self;
}

- (void)attachInteropAPIsToModule:(id<RCTBridgeModule>)bridgeModule
{
/**
* Attach the RCTViewRegistry to this TurboModule, which allows this TurboModule
* To query a React component's UIView, given its reactTag.
*
* Usage: In the TurboModule @implementation, include:
* `@synthesize viewRegistry_DEPRECATED = _viewRegistry_DEPRECATED`
*/
if ([bridgeModule respondsToSelector:@selector(setViewRegistry_DEPRECATED:)]) {
bridgeModule.viewRegistry_DEPRECATED = _viewRegistry_DEPRECATED;
}

/**
* Attach the RCTBundleManager to this TurboModule, which allows this TurboModule to
* read from/write to the app's bundle URL.
*
* Usage: In the TurboModule @implementation, include:
* `@synthesize bundleManager = _bundleManager`
*/
if ([bridgeModule respondsToSelector:@selector(setBundleManager:)]) {
bridgeModule.bundleManager = _bundleManager;
}

/**
* Attach the RCTCallableJSModules to this TurboModule, which allows this TurboModule
* to call JS Module methods.
*
* Usage: In the TurboModule @implementation, include:
* `@synthesize callableJSModules = _callableJSModules`
*/
if ([bridgeModule respondsToSelector:@selector(setCallableJSModules:)]) {
bridgeModule.callableJSModules = _callableJSModules;
}

/**
* Attach the RCTModuleRegistry to this TurboModule, which allows this TurboModule
* to require other TurboModules/NativeModules.
*
* Usage: In the TurboModule @implementation, include:
* `@synthesize moduleRegistry = _moduleRegistry`
*/
if ([bridgeModule respondsToSelector:@selector(setModuleRegistry:)]) {
bridgeModule.moduleRegistry = _moduleRegistry;
}
}

@end
53 changes: 7 additions & 46 deletions React/CxxBridge/RCTCxxBridge.mm
Expand Up @@ -13,6 +13,7 @@
#import <React/RCTBridge.h>
#import <React/RCTBridgeMethod.h>
#import <React/RCTBridgeModule.h>
#import <React/RCTBridgeModuleDecorator.h>
#import <React/RCTConstants.h>
#import <React/RCTConvert.h>
#import <React/RCTCxxBridgeDelegate.h>
Expand Down Expand Up @@ -251,52 +252,12 @@ - (void)setRCTTurboModuleRegistry:(id<RCTTurboModuleRegistry>)turboModuleRegistr

- (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 TurboModule @implementation, include:
* `@synthesize viewRegistry_DEPRECATED = _viewRegistry_DEPRECATED`
*/
if ([bridgeModule respondsToSelector:@selector(setViewRegistry_DEPRECATED:)]) {
bridgeModule.viewRegistry_DEPRECATED = _viewRegistry_DEPRECATED;
}

/**
* Attach the RCTBundleManager to this TurboModule, which allows this TurboModule to
* read from/write to the app's bundle URL.
*
* Usage: In the TurboModule @implementation, include:
* `@synthesize bundleManager = _bundleManager`
*/
if ([bridgeModule respondsToSelector:@selector(setBundleManager:)]) {
bridgeModule.bundleManager = _bundleManager;
}

/**
* Attach the RCTCallableJSModules to this TurboModule, which allows this TurboModule
* to call JS Module methods.
*
* Usage: In the TurboModule @implementation, include:
* `@synthesize callableJSModules = _callableJSModules`
*/

if ([bridgeModule respondsToSelector:@selector(setCallableJSModules:)]) {
bridgeModule.callableJSModules = _callableJSModules;
}

/**
* Attach the RCTModuleRegistry to this TurboModule, which allows this TurboModule
* to require other TurboModules/NativeModules.
*
* Usage: In the TurboModule @implementation, include:
* `@synthesize moduleRegistry = _moduleRegistry`
*/

if ([bridgeModule respondsToSelector:@selector(setModuleRegistry:)]) {
bridgeModule.moduleRegistry = _objCModuleRegistry;
}
RCTBridgeModuleDecorator *bridgeModuleDecorator =
[[RCTBridgeModuleDecorator alloc] initWithViewRegistry:_viewRegistry_DEPRECATED
moduleRegistry:_objCModuleRegistry
bundleManager:_bundleManager
callableJSModules:_callableJSModules];
[bridgeModuleDecorator attachInteropAPIsToModule:(id<RCTBridgeModule>)module];
}

- (std::shared_ptr<MessageQueueThread>)jsMessageThread
Expand Down
6 changes: 3 additions & 3 deletions packages/rn-tester/Podfile.lock
Expand Up @@ -884,7 +884,7 @@ SPEC CHECKSUMS:
CocoaAsyncSocket: 065fd1e645c7abab64f7a6a2007a48038fdc6a99
DoubleConversion: 831926d9b8bf8166fd87886c4abab286c2422662
FBLazyVector: b81a2b70c72d8b0aefb652cea22c11e9ffd02949
FBReactNativeSpec: fdfe4008ff58c7c6e3ffd5ad938b9dfa2cc7e7a2
FBReactNativeSpec: 35cca37e6328d64b4e38c4adb1a28db74b60aeac
Flipper: 26fc4b7382499f1281eb8cb921e5c3ad6de91fe0
Flipper-Boost-iOSX: fd1e2b8cbef7e662a122412d7ac5f5bea715403c
Flipper-DoubleConversion: 57ffbe81ef95306cc9e69c4aa3aeeeeb58a6a28c
Expand Down Expand Up @@ -926,10 +926,10 @@ SPEC CHECKSUMS:
React-RCTTest: 12bbd7fc2e72bd9920dc7286c5b8ef96639582b6
React-RCTText: e9146b2c0550a83d1335bfe2553760070a2d75c7
React-RCTVibration: 50be9c390f2da76045ef0dfdefa18b9cf9f35cfa
React-rncore: 9d392094961ad6270bd39d5ea0237cccdb1cc081
React-rncore: d292b6c2196dcd555622a538e1b206aa2e0ad8a2
React-runtimeexecutor: 4b0c6eb341c7d3ceb5e2385cb0fdb9bf701024f3
ReactCommon: 7a2714d1128f965392b6f99a8b390e3aa38c9569
ScreenshotManager: 646257771aab2075223caf1316e4b089001d3cb9
ScreenshotManager: e8da2e62b3c08a87cece436776ce91555ecec77c
SocketRocket: fccef3f9c5cedea1353a9ef6ada904fde10d6608
Yoga: c0d06f5380d34e939f55420669a60fe08b79bd75
YogaKit: f782866e155069a2cca2517aafea43200b01fd5a
Expand Down

0 comments on commit 94b1b8a

Please sign in to comment.