Skip to content

Commit

Permalink
Introduce RCTCallableJSModules
Browse files Browse the repository at this point in the history
Summary:
This is a Bridgeless-compatible API to allow our NativeModules to call JSModule methods.

Changelog: [iOS][Added] - Introduce RCTCallableJSModules API for NativeModules

Differential Revision: D28395448

fbshipit-source-id: b7929ba8b4cc4410361961b45efa23c76baacd24
  • Loading branch information
RSNara authored and facebook-github-bot committed May 14, 2021
1 parent c6d77a8 commit ece373d
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 0 deletions.
33 changes: 33 additions & 0 deletions React/Base/RCTBridgeModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
@class RCTModuleRegistry;
@class RCTViewRegistry;
@class RCTBundleManager;
@class RCTCallableJSModules;

/**
* The type of a block that is capable of sending a response to a bridged
Expand Down Expand Up @@ -146,6 +147,17 @@ RCT_EXTERN_C_END
*/
@property (nonatomic, weak, readwrite) RCTBundleManager *bundleManager;

/**
* A reference to an RCTCallableJSModules. Useful for modules that need to
* call into methods on JavaScript modules registered as callable with
* React Native.
*
* To implement this in your module, just add `@synthesize callableJSModules =
* _callableJSModules;`. If using Swift, add `@objc var callableJSModules:
* RCTCallableJSModules!` to your module.
*/
@property (nonatomic, weak, readwrite) RCTCallableJSModules *callableJSModules;

/**
* A reference to the RCTBridge. Useful for modules that require access
* to bridge features, such as sending events or making JS calls. This
Expand Down Expand Up @@ -443,3 +455,24 @@ typedef UIView * (^RCTBridgelessComponentViewProvider)(NSNumber *);

- (UIView *)viewForReactTag:(NSNumber *)reactTag;
@end

typedef void (^RCTBridgelessJSModuleMethodInvoker)(
NSString *moduleName,
NSString *methodName,
NSArray *args,
dispatch_block_t onComplete);

/**
* A class that allows NativeModules to call methods on JavaScript modules registered
* as callable with React Native.
*/
@interface RCTCallableJSModules : NSObject
- (void)setBridge:(RCTBridge *)bridge;
- (void)setBridgelessJSModuleMethodInvoker:(RCTBridgelessJSModuleMethodInvoker)bridgelessJSModuleMethodInvoker;

- (void)invokeModule:(NSString *)moduleName method:(NSString *)methodName withArgs:(NSArray *)args;
- (void)invokeModule:(NSString *)moduleName
method:(NSString *)methodName
withArgs:(NSArray *)args
onComplete:(dispatch_block_t)onComplete;
@end
47 changes: 47 additions & 0 deletions React/Base/RCTCallableJSModules.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

#import "RCTBridge.h"
#import "RCTBridgeModule.h"

@implementation RCTCallableJSModules {
RCTBridgelessJSModuleMethodInvoker _bridgelessJSModuleMethodInvoker;
__weak RCTBridge *_bridge;
}

- (void)setBridge:(RCTBridge *)bridge
{
_bridge = bridge;
}

- (void)setBridgelessJSModuleMethodInvoker:(RCTBridgelessJSModuleMethodInvoker)bridgelessJSModuleMethodInvoker
{
_bridgelessJSModuleMethodInvoker = bridgelessJSModuleMethodInvoker;
}

- (void)invokeModule:(NSString *)moduleName method:(NSString *)methodName withArgs:(NSArray *)args
{
[self invokeModule:moduleName method:methodName withArgs:args onComplete:NULL];
}

- (void)invokeModule:(NSString *)moduleName
method:(NSString *)methodName
withArgs:(NSArray *)args
onComplete:(dispatch_block_t)onComplete
{
RCTBridge *bridge = _bridge;
if (bridge) {
[bridge enqueueJSCall:moduleName method:methodName args:args completion:onComplete];
return;
}

if (_bridgelessJSModuleMethodInvoker) {
_bridgelessJSModuleMethodInvoker(moduleName, methodName, args, onComplete);
}
}

@end

0 comments on commit ece373d

Please sign in to comment.