Skip to content

Commit

Permalink
introduce CallInvoker API for bridgeless modules (facebook#44378)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: facebook#44378

Changelog: [iOS][Added] introduce CallInvoker support in bridgeless native modules

I am adding this API in favor of RCTRuntimeExecutor. CallInvoker is now preferred because after facebook#43375, the CallInvoker has access to the jsi::Runtime. Since the community is using CallInvoker already for their async access use cases, CallInvoker is the preferred choice of RuntimeExecutor / RuntimeScheduler because of easier migration. Also, having a wrapper like CallInvoker will give us more flexibility in the future if we want to expand this API.

Reviewed By: RSNara

Differential Revision: D56807994

fbshipit-source-id: 5c3585356d016a50645eda3af2d3bbe00298b4e4
  • Loading branch information
philIip authored and kosmydel committed Jun 11, 2024
1 parent 9c42e87 commit 33a1b91
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 0 deletions.
27 changes: 27 additions & 0 deletions packages/react-native/React/Base/RCTCallInvoker.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* 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 <Foundation/Foundation.h>

#ifdef __cplusplus
#import <ReactCommon/CallInvoker.h>
#endif

NS_ASSUME_NONNULL_BEGIN

@interface RCTCallInvoker : NSObject

#ifdef __cplusplus
- (instancetype)initWithCallInvoker:(std::shared_ptr<facebook::react::CallInvoker>)callInvoker
NS_DESIGNATED_INITIALIZER;

- (std::shared_ptr<facebook::react::CallInvoker>)callInvoker;
#endif

@end

NS_ASSUME_NONNULL_END
28 changes: 28 additions & 0 deletions packages/react-native/React/Base/RCTCallInvoker.mm
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* 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 "RCTCallInvoker.h"

@implementation RCTCallInvoker {
std::shared_ptr<facebook::react::CallInvoker> _callInvoker;
}

- (instancetype)initWithCallInvoker:(std::shared_ptr<facebook::react::CallInvoker>)callInvoker
{
if (self = [super init]) {
_callInvoker = callInvoker;
}

return self;
}

- (std::shared_ptr<facebook::react::CallInvoker>)callInvoker
{
return _callInvoker;
}

@end
17 changes: 17 additions & 0 deletions packages/react-native/React/Base/RCTCallInvokerModule.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
* 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.
*/

@class RCTCallInvoker;

/**
* Have your module conform to this protocol to access the CallInvoker.
*/
@protocol RCTCallInvokerModule <NSObject>

@property (nonatomic, nullable, readwrite) RCTCallInvoker *callInvoker;

@end
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
#import <React/RCTBridge+Private.h>
#import <React/RCTBridgeModule.h>
#import <React/RCTBridgeProxy.h>
#import <React/RCTCallInvoker.h>
#import <React/RCTCallInvokerModule.h>
#import <React/RCTConstants.h>
#import <React/RCTCxxModule.h>
#import <React/RCTInitializing.h>
Expand Down Expand Up @@ -697,6 +699,12 @@ - (BOOL)_shouldCreateObjCModule:(Class)moduleClass
[(id<RCTRuntimeExecutorModule>)module setRuntimeExecutor:runtimeExecutor];
}

// This is a more performant alternative for conformsToProtocol:@protocol(RCTCallInvokerModule)
if ([module respondsToSelector:@selector(setCallInvoker:)]) {
RCTCallInvoker *callInvoker = [[RCTCallInvoker alloc] initWithCallInvoker:_jsInvoker];
[(id<RCTCallInvokerModule>)module setCallInvoker:callInvoker];
}

/**
* 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:
Expand Down

0 comments on commit 33a1b91

Please sign in to comment.