Skip to content

Commit

Permalink
expose contextContainer as application API
Browse files Browse the repository at this point in the history
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
  • Loading branch information
sahrens authored and facebook-github-bot committed Dec 22, 2018
1 parent fa31e44 commit 089dba3
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 14 deletions.
2 changes: 2 additions & 0 deletions React/Fabric/RCTSurfacePresenter.h
Original file line number Diff line number Diff line change
Expand Up @@ -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>

Expand All @@ -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

Expand Down
36 changes: 25 additions & 11 deletions React/Fabric/RCTSurfacePresenter.mm
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ @interface RCTSurfacePresenter () <RCTSchedulerDelegate, RCTMountingManagerDeleg

@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.
Expand Down Expand Up @@ -166,9 +167,25 @@ - (RCTScheduler *)_scheduler
return _scheduler;
}

auto contextContainer = std::make_shared<ContextContainer>();
_scheduler = [[RCTScheduler alloc] initWithContextContainer:self.contextContainer];
_scheduler.delegate = self;

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);
_contextContainer->registerInstance(_reactNativeConfig);

auto messageQueueThread = _batchedBridge.jsMessageThread;
auto runtime = (facebook::jsi::Runtime *)((RCTCxxBridge *)_batchedBridge).runtime;
Expand All @@ -188,17 +205,13 @@ - (RCTScheduler *)_scheduler
return std::make_unique<RuntimeEventBeat>(runtimeExecutor);
};

contextContainer->registerInstance<EventBeatFactory>(synchronousBeatFactory, "synchronous");
contextContainer->registerInstance<EventBeatFactory>(asynchronousBeatFactory, "asynchronous");

contextContainer->registerInstance(runtimeExecutor, "runtime-executor");
_contextContainer->registerInstance<EventBeatFactory>(synchronousBeatFactory, "synchronous");
_contextContainer->registerInstance<EventBeatFactory>(asynchronousBeatFactory, "asynchronous");

contextContainer->registerInstance(std::make_shared<ImageManager>((__bridge void *)[_bridge imageLoader]));
_contextContainer->registerInstance(runtimeExecutor, "runtime-executor");

_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
Expand Down Expand Up @@ -308,6 +321,7 @@ - (void)handleBridgeWillReloadNotification:(NSNotification *)notification
{
std::lock_guard<std::mutex> lock(_schedulerMutex);
_scheduler = nil;
_contextContainer = nil;
}
}

Expand Down
3 changes: 1 addition & 2 deletions ReactCommon/fabric/uimanager/Scheduler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down
1 change: 0 additions & 1 deletion ReactCommon/fabric/uimanager/Scheduler.h
Original file line number Diff line number Diff line change
Expand Up @@ -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_;
Expand Down

0 comments on commit 089dba3

Please sign in to comment.