diff --git a/React/Fabric/RCTSurfacePresenter.h b/React/Fabric/RCTSurfacePresenter.h index edcfb6321ab..0e01623b752 100644 --- a/React/Fabric/RCTSurfacePresenter.h +++ b/React/Fabric/RCTSurfacePresenter.h @@ -10,6 +10,7 @@ #import #import +#import #import #import @@ -30,6 +31,7 @@ NS_ASSUME_NONNULL_BEGIN config:(std::shared_ptr)config; @property (nonatomic, readonly) RCTComponentViewFactory *componentViewFactory; +@property (nonatomic, readonly) facebook::react::SharedContextContainer contextContainer; @end diff --git a/React/Fabric/RCTSurfacePresenter.mm b/React/Fabric/RCTSurfacePresenter.mm index d4424745e58..d7f56e3fb6a 100644 --- a/React/Fabric/RCTSurfacePresenter.mm +++ b/React/Fabric/RCTSurfacePresenter.mm @@ -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(); + _scheduler = [[RCTScheduler alloc] initWithContextContainer:self.contextContainer]; + _scheduler.delegate = self; + + return _scheduler; +} - contextContainer->registerInstance(_reactNativeConfig); +@synthesize contextContainer = _contextContainer; + +- (SharedContextContainer)contextContainer +{ + std::lock_guard lock(_contextContainerMutex); + + if (_contextContainer) { + return _contextContainer; + } + + _contextContainer = std::make_shared(); + + _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(runtimeExecutor); }; - contextContainer->registerInstance(synchronousBeatFactory, "synchronous"); - contextContainer->registerInstance(asynchronousBeatFactory, "asynchronous"); + _contextContainer->registerInstance(synchronousBeatFactory, "synchronous"); + _contextContainer->registerInstance(asynchronousBeatFactory, "asynchronous"); - contextContainer->registerInstance(runtimeExecutor, "runtime-executor"); + _contextContainer->registerInstance(runtimeExecutor, "runtime-executor"); - contextContainer->registerInstance(std::make_shared((__bridge void *)[_bridge imageLoader])); - - _scheduler = [[RCTScheduler alloc] initWithContextContainer:contextContainer]; - _scheduler.delegate = self; - - return _scheduler; + _contextContainer->registerInstance(std::make_shared((__bridge void *)[_bridge imageLoader])); + return _contextContainer; } - (void)_startSurface:(RCTFabricSurface *)surface @@ -308,6 +321,7 @@ using namespace facebook::react; { std::lock_guard lock(_schedulerMutex); _scheduler = nil; + _contextContainer = nil; } } diff --git a/ReactCommon/fabric/uimanager/Scheduler.cpp b/ReactCommon/fabric/uimanager/Scheduler.cpp index 80040655d2e..76422d1ea3c 100644 --- a/ReactCommon/fabric/uimanager/Scheduler.cpp +++ b/ReactCommon/fabric/uimanager/Scheduler.cpp @@ -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("asynchronous"); const auto synchronousEventBeatFactory = diff --git a/ReactCommon/fabric/uimanager/Scheduler.h b/ReactCommon/fabric/uimanager/Scheduler.h index ff45fa9aee4..7402b32c26a 100644 --- a/ReactCommon/fabric/uimanager/Scheduler.h +++ b/ReactCommon/fabric/uimanager/Scheduler.h @@ -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_; std::shared_ptr reactNativeConfig_;