introduce RCTContextContainerHandling (#37702)

Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/37702

Changelog: [Internal]

the context container is a DI object in our internal infra. the delegate method `createContextContainer` is used to pass down internal implementations that may be used in our infra, notably the `ReactNativeConfig`. this API used to be required for the app delegate to implement.

in this change, i introduce a protocol, `RCTContextContainerHandling`, where we shift the burden of creating the DI container to our internal infra where userland can now optionally add dependencies to this.

this also removes more C++ boilerplate for the bridgeless setup.

Reviewed By: sammy-SC

Differential Revision: D46245433

fbshipit-source-id: 552a9ab0b6a178bab592743863694ff886b152f8
This commit is contained in:
Phillip Pan
2023-06-05 12:31:19 -07:00
committed by Facebook GitHub Bot
parent 2861dd2b1e
commit e0a1a18d4b
6 changed files with 39 additions and 15 deletions
@@ -0,0 +1,16 @@
/*
* 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>
#import <react/utils/ContextContainer.h>
@protocol RCTContextContainerHandling <NSObject>
- (void)didCreateContextContainer:(std::shared_ptr<facebook::react::ContextContainer>)contextContainer;
@end
@@ -7,6 +7,7 @@
#import "RCTHost.h"
#import <ReactCommon/RCTContextContainerHandling.h>
#import <jsi/jsi.h>
typedef NSURL * (^RCTHostBundleURLProvider)(void);
@@ -22,5 +23,6 @@ typedef NSURL * (^RCTHostBundleURLProvider)(void);
- (void)registerSegmentWithId:(NSNumber *)segmentId path:(NSString *)path;
- (void)setBundleURLProvider:(RCTHostBundleURLProvider)bundleURLProvider;
- (void)setRuntimeDelegate:(id<RCTHostRuntimeDelegate>)runtimeDelegate;
- (void)setContextContainerHandler:(id<RCTContextContainerHandling>)contextContainerHandler;
@end
@@ -29,8 +29,6 @@ typedef std::shared_ptr<facebook::react::JSEngineInstance> (^RCTHostJSEngineProv
@protocol RCTHostDelegate <NSObject>
- (std::shared_ptr<facebook::react::ContextContainer>)createContextContainer;
- (void)host:(RCTHost *)host
didReceiveJSErrorStack:(NSArray<NSDictionary<NSString *, id> *> *)stack
message:(NSString *)message
@@ -25,9 +25,12 @@ using namespace facebook::react;
@implementation RCTHost {
RCTInstance *_instance;
__weak id<RCTHostDelegate> _hostDelegate;
__weak id<RCTTurboModuleManagerDelegate> _turboModuleManagerDelegate;
__weak id<RCTHostRuntimeDelegate> _runtimeDelegate;
__weak id<RCTContextContainerHandling> _contextContainerHandler;
NSURL *_oldDelegateBundleURL;
NSURL *_bundleURL;
RCTBundleManager *_bundleManager;
@@ -228,11 +231,6 @@ using namespace facebook::react;
#pragma mark - RCTInstanceDelegate
- (std::shared_ptr<facebook::react::ContextContainer>)createContextContainer
{
return [_hostDelegate createContextContainer];
}
- (void)instance:(RCTInstance *)instance didReceiveErrorMap:(facebook::react::MapBuffer)errorMap
{
NSString *message = [NSString stringWithCString:errorMap.getString(JSErrorHandlerKey::kErrorMessage).c_str()
@@ -262,6 +260,13 @@ using namespace facebook::react;
[_runtimeDelegate hostDidInitializeRuntime:runtime];
}
#pragma mark - RCTContextContainerHandling
- (void)didCreateContextContainer:(std::shared_ptr<facebook::react::ContextContainer>)contextContainer
{
[_contextContainerHandler didCreateContextContainer:contextContainer];
}
#pragma mark - Internal
- (void)registerSegmentWithId:(NSNumber *)segmentId path:(NSString *)path
@@ -279,6 +284,11 @@ using namespace facebook::react;
_runtimeDelegate = runtimeDelegate;
}
- (void)setContextContainerHandler:(id<RCTContextContainerHandling>)contextContainerHandler
{
_contextContainerHandler = contextContainerHandler;
}
#pragma mark - Private
- (void)_attachSurface:(RCTFabricSurface *)surface FB_OBJC_DIRECT
@@ -12,7 +12,8 @@
#import <react/bridgeless/JSEngineInstance.h>
#import <react/bridgeless/ReactInstance.h>
#import <react/renderer/mapbuffer/MapBuffer.h>
#import <react/utils/ContextContainer.h>
#import "RCTContextContainerHandling.h"
NS_ASSUME_NONNULL_BEGIN
@@ -35,9 +36,7 @@ RCT_EXTERN void RCTInstanceSetRuntimeDiagnosticFlags(NSString *_Nullable flags);
FB_RUNTIME_PROTOCOL
@protocol RCTTurboModuleManagerDelegate;
@protocol RCTInstanceDelegate <NSObject>
- (std::shared_ptr<facebook::react::ContextContainer>)createContextContainer;
@protocol RCTInstanceDelegate <RCTContextContainerHandling>
- (void)instance:(RCTInstance *)instance didReceiveErrorMap:(facebook::react::MapBuffer)errorMap;
- (void)instance:(RCTInstance *)instance didInitializeRuntime:(facebook::jsi::Runtime &)runtime;
@@ -235,10 +235,9 @@ void RCTInstanceSetRuntimeDiagnosticFlags(NSString *flags)
RCTLogSetBridgelessModuleRegistry(_bridgeModuleDecorator.moduleRegistry);
RCTLogSetBridgelessCallableJSModules(_bridgeModuleDecorator.callableJSModules);
auto contextContainer = [_delegate createContextContainer];
if (!contextContainer) {
contextContainer = std::make_shared<ContextContainer>();
}
auto contextContainer = std::make_shared<ContextContainer>();
[_delegate didCreateContextContainer:contextContainer];
contextContainer->insert(
"RCTImageLoader", facebook::react::wrapManagedObject([_turboModuleManager moduleForName:"RCTImageLoader"]));
contextContainer->insert(