expose contextContainer as application API

Summary: We need a way for different apps to inject dependencies or additional functionality into Fabric - ReactNativeConfig might be a special case, but I think this could clean up it's integration nicely, and I'm using this for a uitemplate cache system so we can use CompactDisk or other storage systems for caching depending on the app.

Reviewed By: mdvacca

Differential Revision: D13407287

fbshipit-source-id: 45481908434e6235850aa4d2d6b2bfb936a23be7
This commit is contained in:
Spencer Ahrens 2018-12-21 17:57:32 -08:00 committed by Facebook Github Bot
parent fa31e44e13
commit 089dba3842
4 changed files with 28 additions and 14 deletions

View File

@ -10,6 +10,7 @@
#import <React/RCTBridge.h>
#import <React/RCTComponentViewFactory.h>
#import <react/uimanager/ContextContainer.h>
#import <React/RCTPrimitives.h>
#import <react/config/ReactNativeConfig.h>
@ -30,6 +31,7 @@ NS_ASSUME_NONNULL_BEGIN
config:(std::shared_ptr<const facebook::react::ReactNativeConfig>)config;
@property (nonatomic, readonly) RCTComponentViewFactory *componentViewFactory;
@property (nonatomic, readonly) facebook::react::SharedContextContainer contextContainer;
@end

View File

@ -45,6 +45,7 @@ using namespace facebook::react;
@implementation RCTSurfacePresenter {
std::mutex _schedulerMutex;
std::mutex _contextContainerMutex;
RCTScheduler *_Nullable _scheduler; // Thread-safe. Mutation of the instance variable is protected by `_schedulerMutex`.
RCTMountingManager *_mountingManager; // Thread-safe.
RCTSurfaceRegistry *_surfaceRegistry; // Thread-safe.
@ -166,9 +167,25 @@ using namespace facebook::react;
return _scheduler;
}
auto contextContainer = std::make_shared<ContextContainer>();
_scheduler = [[RCTScheduler alloc] initWithContextContainer:self.contextContainer];
_scheduler.delegate = self;
contextContainer->registerInstance(_reactNativeConfig);
return _scheduler;
}
@synthesize contextContainer = _contextContainer;
- (SharedContextContainer)contextContainer
{
std::lock_guard<std::mutex> lock(_contextContainerMutex);
if (_contextContainer) {
return _contextContainer;
}
_contextContainer = std::make_shared<ContextContainer>();
_contextContainer->registerInstance(_reactNativeConfig);
auto messageQueueThread = _batchedBridge.jsMessageThread;
auto runtime = (facebook::jsi::Runtime *)((RCTCxxBridge *)_batchedBridge).runtime;
@ -188,17 +205,13 @@ using namespace facebook::react;
return std::make_unique<RuntimeEventBeat>(runtimeExecutor);
};
contextContainer->registerInstance<EventBeatFactory>(synchronousBeatFactory, "synchronous");
contextContainer->registerInstance<EventBeatFactory>(asynchronousBeatFactory, "asynchronous");
_contextContainer->registerInstance<EventBeatFactory>(synchronousBeatFactory, "synchronous");
_contextContainer->registerInstance<EventBeatFactory>(asynchronousBeatFactory, "asynchronous");
contextContainer->registerInstance(runtimeExecutor, "runtime-executor");
_contextContainer->registerInstance(runtimeExecutor, "runtime-executor");
contextContainer->registerInstance(std::make_shared<ImageManager>((__bridge void *)[_bridge imageLoader]));
_scheduler = [[RCTScheduler alloc] initWithContextContainer:contextContainer];
_scheduler.delegate = self;
return _scheduler;
_contextContainer->registerInstance(std::make_shared<ImageManager>((__bridge void *)[_bridge imageLoader]));
return _contextContainer;
}
- (void)_startSurface:(RCTFabricSurface *)surface
@ -308,6 +321,7 @@ using namespace facebook::react;
{
std::lock_guard<std::mutex> lock(_schedulerMutex);
_scheduler = nil;
_contextContainer = nil;
}
}

View File

@ -18,8 +18,7 @@
namespace facebook {
namespace react {
Scheduler::Scheduler(const SharedContextContainer &contextContainer)
: contextContainer_(contextContainer) {
Scheduler::Scheduler(const SharedContextContainer &contextContainer) {
const auto asynchronousEventBeatFactory =
contextContainer->getInstance<EventBeatFactory>("asynchronous");
const auto synchronousEventBeatFactory =

View File

@ -92,7 +92,6 @@ class Scheduler final : public UIManagerDelegate, public ShadowTreeDelegate {
SchedulerDelegate *delegate_;
SharedComponentDescriptorRegistry componentDescriptorRegistry_;
ShadowTreeRegistry shadowTreeRegistry_;
SharedContextContainer contextContainer_;
RuntimeExecutor runtimeExecutor_;
std::shared_ptr<UIManagerBinding> uiManagerBinding_;
std::shared_ptr<const ReactNativeConfig> reactNativeConfig_;