From 0ccc98d8945d04c1a25c3917b9b69effbb9b98ec Mon Sep 17 00:00:00 2001 From: Phillip Pan Date: Thu, 11 May 2023 15:13:03 -0700 Subject: [PATCH] move callFunctionOnJSModule to public api (#37399) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/37399 Changelog: [Internal] in this stack, i remove the `ReactInstanceForwarding` protocol. it is super lean and is only used by two classes, and the polymorphic behavior never comes into play because we never have a pointer to `id` in our codebase. being able to call into JS from native is tablestakes behavior in userland, so i'm moving that to the public API. i also renamed it to be more clear that it's calling from native into JS, not the other way around. Reviewed By: cipolleschi Differential Revision: D45760090 fbshipit-source-id: 8ac99723796cb65891076e8e7d69aeefe3e94213 --- .../bridgeless/platform/ios/Core/RCTHost.h | 6 ++++++ .../bridgeless/platform/ios/Core/RCTHost.mm | 12 ++++++------ .../bridgeless/platform/ios/Core/RCTInstance.h | 8 ++------ .../platform/ios/Core/RCTInstance.mm | 18 +++++++++--------- 4 files changed, 23 insertions(+), 21 deletions(-) diff --git a/packages/react-native/ReactCommon/react/bridgeless/platform/ios/Core/RCTHost.h b/packages/react-native/ReactCommon/react/bridgeless/platform/ios/Core/RCTHost.h index a5f6bc2a3e1..b6a5d384e3e 100644 --- a/packages/react-native/ReactCommon/react/bridgeless/platform/ios/Core/RCTHost.h +++ b/packages/react-native/ReactCommon/react/bridgeless/platform/ios/Core/RCTHost.h @@ -76,6 +76,12 @@ typedef std::shared_ptr (^RCTHostJSEngineProv - (RCTSurfacePresenter *)getSurfacePresenter FB_OBJC_DIRECT; +/** + * Calls a method on a JS module that has been registered with `registerCallableModule`. Used to invoke a JS function + * from platform code. + */ +- (void)callFunctionOnJSModule:(NSString *)moduleName method:(NSString *)method args:(NSArray *)args; + @end NS_ASSUME_NONNULL_END diff --git a/packages/react-native/ReactCommon/react/bridgeless/platform/ios/Core/RCTHost.mm b/packages/react-native/ReactCommon/react/bridgeless/platform/ios/Core/RCTHost.mm index 6bd55a6b47e..d5a5b1a48ca 100644 --- a/packages/react-native/ReactCommon/react/bridgeless/platform/ios/Core/RCTHost.mm +++ b/packages/react-native/ReactCommon/react/bridgeless/platform/ios/Core/RCTHost.mm @@ -140,7 +140,7 @@ NSString *const RCTHostDidReloadNotification = @"RCTHostDidReloadNotification"; return self; } -#pragma mark - Public API +#pragma mark - Public - (void)preload { @@ -199,6 +199,11 @@ NSString *const RCTHostDidReloadNotification = @"RCTHostDidReloadNotification"; return [_instance surfacePresenter]; } +- (void)callFunctionOnJSModule:(NSString *)moduleName method:(NSString *)method args:(NSArray *)args +{ + [_instance callFunctionOnJSModule:moduleName method:method args:args]; +} + #pragma mark - RCTReloadListener - (void)didReceiveReloadCommand @@ -237,11 +242,6 @@ NSString *const RCTHostDidReloadNotification = @"RCTHostDidReloadNotification"; // early in startup, but could add some intelligent guards here. #pragma mark - ReactInstanceForwarding -- (void)callFunctionOnModule:(NSString *)moduleName method:(NSString *)method args:(NSArray *)args -{ - [_instance callFunctionOnModule:moduleName method:method args:args]; -} - - (void)registerSegmentWithId:(NSNumber *)segmentId path:(NSString *)path { [_instance registerSegmentWithId:segmentId path:path]; diff --git a/packages/react-native/ReactCommon/react/bridgeless/platform/ios/Core/RCTInstance.h b/packages/react-native/ReactCommon/react/bridgeless/platform/ios/Core/RCTInstance.h index 4ee1868741c..17f94015292 100644 --- a/packages/react-native/ReactCommon/react/bridgeless/platform/ios/Core/RCTInstance.h +++ b/packages/react-native/ReactCommon/react/bridgeless/platform/ios/Core/RCTInstance.h @@ -45,12 +45,6 @@ FB_RUNTIME_PROTOCOL */ @protocol ReactInstanceForwarding -/** - * Calls a method on a JS module that has been registered with `registerCallableModule`. Used to invoke a JS function - * from platform code. - */ -- (void)callFunctionOnModule:(NSString *)moduleName method:(NSString *)method args:(NSArray *)args; - /** * Registers a new JS segment. */ @@ -77,6 +71,8 @@ typedef void (^_Null_unspecified RCTInstanceInitialBundleLoadCompletionBlock)(); jsErrorHandlingFunc:(facebook::react::JsErrorHandler::JsErrorHandlingFunc)jsErrorHandlingFunc FB_OBJC_DIRECT; +- (void)callFunctionOnJSModule:(NSString *)moduleName method:(NSString *)method args:(NSArray *)args; + - (void)invalidate; @property (nonatomic, readonly, strong) RCTSurfacePresenter *surfacePresenter; diff --git a/packages/react-native/ReactCommon/react/bridgeless/platform/ios/Core/RCTInstance.mm b/packages/react-native/ReactCommon/react/bridgeless/platform/ios/Core/RCTInstance.mm index 0a8bd552169..32a6f68b594 100644 --- a/packages/react-native/ReactCommon/react/bridgeless/platform/ios/Core/RCTInstance.mm +++ b/packages/react-native/ReactCommon/react/bridgeless/platform/ios/Core/RCTInstance.mm @@ -115,7 +115,7 @@ void RCTInstanceSetRuntimeDiagnosticFlags(NSString *flags) setBridgelessJSModuleMethodInvoker:^( NSString *moduleName, NSString *methodName, NSArray *args, dispatch_block_t onComplete) { // TODO: Make RCTInstance call onComplete - [weakInstance callFunctionOnModule:moduleName method:methodName args:args]; + [weakInstance callFunctionOnJSModule:moduleName method:methodName args:args]; }]; } @@ -129,6 +129,14 @@ void RCTInstanceSetRuntimeDiagnosticFlags(NSString *flags) return self; } +- (void)callFunctionOnJSModule:(NSString *)moduleName method:(NSString *)method args:(NSArray *)args +{ + if (_valid) { + _reactInstance->callFunctionOnModule( + [moduleName UTF8String], [method UTF8String], convertIdToFollyDynamic(args ?: @[])); + } +} + - (void)invalidate { std::lock_guard lock(_invalidationMutex); @@ -191,14 +199,6 @@ void RCTInstanceSetRuntimeDiagnosticFlags(NSString *flags) #pragma mark - ReactInstanceForwarding -- (void)callFunctionOnModule:(NSString *)moduleName method:(NSString *)method args:(NSArray *)args -{ - if (_valid) { - _reactInstance->callFunctionOnModule( - [moduleName UTF8String], [method UTF8String], convertIdToFollyDynamic(args ?: @[])); - } -} - - (void)registerSegmentWithId:(NSNumber *)segmentId path:(NSString *)path { if (_valid) {