Skip to content

Commit

Permalink
add forward compatibility for bridgeless CallInvoker API (#44376)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #44376

Changelog: [Internal]

In order to make migration a little bit cleaner, I thought it would be nice to implement forward compatibility for RCTCallInvokerModule. This way, the consumer doesn't have to have branching logic when they try to retrieve the callInvoker in their code, and can remove a callsite to the bridge.

Reviewed By: RSNara

Differential Revision: D56807993

fbshipit-source-id: 6c9aa74db15e04b8ab632d230b3e525363a4d1ca
  • Loading branch information
philIip authored and facebook-github-bot committed May 3, 2024
1 parent 1d2221a commit 05a4232
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* LICENSE file in the root directory of this source tree.
*/

#include "RCTBridgeModuleDecorator.h"
#import "RCTBridgeModuleDecorator.h"

@implementation RCTBridgeModuleDecorator

Expand Down
18 changes: 10 additions & 8 deletions packages/react-native/React/Base/RCTModuleData.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,21 @@
@protocol RCTBridgeMethod;
@protocol RCTBridgeModule;
@class RCTBridge;
@class RCTModuleData;
@class RCTModuleRegistry;
@class RCTViewRegistry;
@class RCTBundleManager;
@class RCTCallableJSModules;
@class RCTCallInvoker;

typedef id<RCTBridgeModule> (^RCTBridgeModuleProvider)(void);

@protocol RCTModuleDataCallInvokerProvider <NSObject>

- (RCTCallInvoker *)callInvokerForModuleData:(RCTModuleData *)moduleData;

@end

@interface RCTModuleData : NSObject <RCTInvalidating>

- (instancetype)initWithModuleClass:(Class)moduleClass
Expand All @@ -28,14 +36,6 @@ typedef id<RCTBridgeModule> (^RCTBridgeModuleProvider)(void);
bundleManager:(RCTBundleManager *)bundleManager
callableJSModules:(RCTCallableJSModules *)callableJSModules;

- (instancetype)initWithModuleClass:(Class)moduleClass
moduleProvider:(RCTBridgeModuleProvider)moduleProvider
bridge:(RCTBridge *)bridge
moduleRegistry:(RCTModuleRegistry *)moduleRegistry
viewRegistry_DEPRECATED:(RCTViewRegistry *)viewRegistry_DEPRECATED
bundleManager:(RCTBundleManager *)bundleManager
callableJSModules:(RCTCallableJSModules *)callableJSModules NS_DESIGNATED_INITIALIZER;

- (instancetype)initWithModuleInstance:(id<RCTBridgeModule>)instance
bridge:(RCTBridge *)bridge
moduleRegistry:(RCTModuleRegistry *)moduleRegistry
Expand Down Expand Up @@ -110,4 +110,6 @@ typedef id<RCTBridgeModule> (^RCTBridgeModuleProvider)(void);
*/
@property (nonatomic, assign, readonly) BOOL implementsPartialBatchDidFlush;

@property (nonatomic, weak, readwrite) id<RCTModuleDataCallInvokerProvider> callInvokerProvider;

@end
38 changes: 22 additions & 16 deletions packages/react-native/React/Base/RCTModuleData.mm
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#import "RCTBridge+Private.h"
#import "RCTBridge.h"
#import "RCTBridgeModuleDecorator.h"
#import "RCTCallInvokerModule.h"
#import "RCTConstants.h"
#import "RCTInitializing.h"
#import "RCTLog.h"
Expand Down Expand Up @@ -87,24 +88,24 @@ - (instancetype)initWithModuleClass:(Class)moduleClass
bundleManager:(RCTBundleManager *)bundleManager
callableJSModules:(RCTCallableJSModules *)callableJSModules
{
return [self initWithModuleClass:moduleClass
moduleProvider:^id<RCTBridgeModule> {
return [moduleClass new];
}
bridge:bridge
moduleRegistry:moduleRegistry
viewRegistry_DEPRECATED:viewRegistry_DEPRECATED
bundleManager:bundleManager
callableJSModules:callableJSModules];
return [self _initWithModuleClass:moduleClass
moduleProvider:^id<RCTBridgeModule> {
return [moduleClass new];
}
bridge:bridge
moduleRegistry:moduleRegistry
viewRegistry_DEPRECATED:viewRegistry_DEPRECATED
bundleManager:bundleManager
callableJSModules:callableJSModules];
}

- (instancetype)initWithModuleClass:(Class)moduleClass
moduleProvider:(RCTBridgeModuleProvider)moduleProvider
bridge:(RCTBridge *)bridge
moduleRegistry:(RCTModuleRegistry *)moduleRegistry
viewRegistry_DEPRECATED:(RCTViewRegistry *)viewRegistry_DEPRECATED
bundleManager:(RCTBundleManager *)bundleManager
callableJSModules:(RCTCallableJSModules *)callableJSModules
- (instancetype)_initWithModuleClass:(Class)moduleClass
moduleProvider:(RCTBridgeModuleProvider)moduleProvider
bridge:(RCTBridge *)bridge
moduleRegistry:(RCTModuleRegistry *)moduleRegistry
viewRegistry_DEPRECATED:(RCTViewRegistry *)viewRegistry_DEPRECATED
bundleManager:(RCTBundleManager *)bundleManager
callableJSModules:(RCTCallableJSModules *)callableJSModules
{
if (self = [super init]) {
_bridge = bridge;
Expand Down Expand Up @@ -201,6 +202,11 @@ - (void)setUpInstanceAndBridge:(int32_t)requestId
bundleManager:_bundleManager
callableJSModules:_callableJSModules];
[moduleDecorator attachInteropAPIsToModule:_instance];

// This is a more performant alternative for conformsToProtocol:@protocol(RCTCallInvokerModule)
if ([_instance respondsToSelector:@selector(setCallInvoker:)]) {
[(id<RCTCallInvokerModule>)_instance setCallInvoker:[self.callInvokerProvider callInvokerForModuleData:self]];
}
}

[self setUpMethodQueue];
Expand Down
11 changes: 10 additions & 1 deletion packages/react-native/React/CxxBridge/RCTCxxBridge.mm
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#import <React/RCTBridgeMethod.h>
#import <React/RCTBridgeModule.h>
#import <React/RCTBridgeModuleDecorator.h>
#import <React/RCTCallInvoker.h>
#import <React/RCTConstants.h>
#import <React/RCTConvert.h>
#import <React/RCTCxxBridgeDelegate.h>
Expand Down Expand Up @@ -168,7 +169,7 @@ static void registerPerformanceLoggerHooks(RCTPerformanceLogger *performanceLogg
};
}

@interface RCTCxxBridge ()
@interface RCTCxxBridge () <RCTModuleDataCallInvokerProvider>

@property (nonatomic, weak, readonly) RCTBridge *parentBridge;
@property (nonatomic, assign, readonly) BOOL moduleSetupComplete;
Expand Down Expand Up @@ -767,6 +768,7 @@ - (void)updateModuleWithInstance:(id<RCTBridgeModule>)instance
viewRegistry_DEPRECATED:_viewRegistry_DEPRECATED
bundleManager:_bundleManager
callableJSModules:_callableJSModules];
moduleData.callInvokerProvider = self;
BridgeNativeModulePerfLogger::moduleDataCreateEnd([moduleName UTF8String], moduleDataId);

_moduleDataByName[moduleName] = moduleData;
Expand Down Expand Up @@ -1595,4 +1597,11 @@ - (void)invokeAsync:(CallFunc &&)func
return _reactInstance ? _reactInstance->getDecoratedNativeMethodCallInvoker(nativeInvoker) : nullptr;
}

#pragma mark - RCTModuleDataCallInvokerProvider

- (RCTCallInvoker *)callInvokerForModuleData:(RCTModuleData *)moduleData
{
return [[RCTCallInvoker alloc] initWithCallInvoker:self.jsCallInvoker];
}

@end

0 comments on commit 05a4232

Please sign in to comment.