2018-04-10 23:37:06 +00:00
|
|
|
/**
|
2018-09-11 22:27:47 +00:00
|
|
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
2018-04-10 23:37:06 +00:00
|
|
|
*
|
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#import "RCTSurfacePresenter.h"
|
|
|
|
|
2018-10-29 20:02:27 +00:00
|
|
|
#import <objc/runtime.h>
|
2018-09-26 17:02:06 +00:00
|
|
|
#import <mutex>
|
2018-10-29 20:02:27 +00:00
|
|
|
#import <jsi/jsi.h>
|
|
|
|
#import <cxxreact/MessageQueueThread.h>
|
2018-09-26 17:02:06 +00:00
|
|
|
|
2018-04-10 23:37:06 +00:00
|
|
|
#import <React/RCTAssert.h>
|
|
|
|
#import <React/RCTBridge+Private.h>
|
2018-06-15 03:39:44 +00:00
|
|
|
#import <React/RCTComponentViewRegistry.h>
|
|
|
|
#import <React/RCTFabricSurface.h>
|
2018-09-24 19:54:26 +00:00
|
|
|
#import <React/RCTImageLoader.h>
|
2018-06-15 03:39:44 +00:00
|
|
|
#import <React/RCTMountingManager.h>
|
2018-04-10 23:37:06 +00:00
|
|
|
#import <React/RCTMountingManagerDelegate.h>
|
2018-06-15 03:39:44 +00:00
|
|
|
#import <React/RCTScheduler.h>
|
2018-04-10 23:37:06 +00:00
|
|
|
#import <React/RCTSurfaceRegistry.h>
|
|
|
|
#import <React/RCTSurfaceView.h>
|
|
|
|
#import <React/RCTSurfaceView+Internal.h>
|
2018-06-22 14:28:29 +00:00
|
|
|
#import <React/RCTUtils.h>
|
2018-11-10 22:19:08 +00:00
|
|
|
#import <react/core/LayoutContext.h>
|
|
|
|
#import <react/core/LayoutConstraints.h>
|
2018-11-26 06:15:00 +00:00
|
|
|
#import <react/components/root/RootShadowNode.h>
|
2018-11-10 22:19:08 +00:00
|
|
|
#import <react/imagemanager/ImageManager.h>
|
|
|
|
#import <react/uimanager/ContextContainer.h>
|
2018-05-09 05:56:27 +00:00
|
|
|
|
2018-09-24 19:54:26 +00:00
|
|
|
#import "MainRunLoopEventBeat.h"
|
2018-11-06 18:58:49 +00:00
|
|
|
#import "RuntimeEventBeat.h"
|
2018-05-09 05:56:27 +00:00
|
|
|
#import "RCTConversions.h"
|
|
|
|
|
2018-04-10 23:37:06 +00:00
|
|
|
using namespace facebook::react;
|
|
|
|
|
2018-09-24 19:54:26 +00:00
|
|
|
@interface RCTBridge ()
|
|
|
|
- (std::shared_ptr<facebook::react::MessageQueueThread>)jsMessageThread;
|
|
|
|
@end
|
|
|
|
|
2018-04-10 23:37:06 +00:00
|
|
|
@interface RCTSurfacePresenter () <RCTSchedulerDelegate, RCTMountingManagerDelegate>
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation RCTSurfacePresenter {
|
2018-09-26 17:02:06 +00:00
|
|
|
std::mutex _schedulerMutex;
|
2018-12-22 01:57:32 +00:00
|
|
|
std::mutex _contextContainerMutex;
|
2018-09-26 17:02:06 +00:00
|
|
|
RCTScheduler *_Nullable _scheduler; // Thread-safe. Mutation of the instance variable is protected by `_schedulerMutex`.
|
|
|
|
RCTMountingManager *_mountingManager; // Thread-safe.
|
|
|
|
RCTSurfaceRegistry *_surfaceRegistry; // Thread-safe.
|
|
|
|
RCTBridge *_bridge; // Unsafe. We are moving away from Bridge.
|
2018-04-10 23:37:06 +00:00
|
|
|
RCTBridge *_batchedBridge;
|
2018-12-05 23:00:34 +00:00
|
|
|
std::shared_ptr<const ReactNativeConfig> _reactNativeConfig;
|
2018-04-10 23:37:06 +00:00
|
|
|
}
|
|
|
|
|
2018-12-05 23:00:34 +00:00
|
|
|
- (instancetype)initWithBridge:(RCTBridge *)bridge config:(std::shared_ptr<const ReactNativeConfig>)config
|
2018-04-10 23:37:06 +00:00
|
|
|
{
|
|
|
|
if (self = [super init]) {
|
|
|
|
_bridge = bridge;
|
|
|
|
_batchedBridge = [_bridge batchedBridge] ?: _bridge;
|
|
|
|
|
|
|
|
_surfaceRegistry = [[RCTSurfaceRegistry alloc] init];
|
2018-09-26 17:01:42 +00:00
|
|
|
|
2018-04-10 23:37:06 +00:00
|
|
|
_mountingManager = [[RCTMountingManager alloc] init];
|
|
|
|
_mountingManager.delegate = self;
|
2018-06-15 17:56:40 +00:00
|
|
|
|
2018-12-05 23:00:34 +00:00
|
|
|
if (config != nullptr) {
|
|
|
|
_reactNativeConfig = config;
|
|
|
|
} else {
|
|
|
|
_reactNativeConfig = std::make_shared<const EmptyReactNativeConfig>();
|
|
|
|
}
|
|
|
|
|
2018-06-15 17:56:40 +00:00
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:self
|
|
|
|
selector:@selector(handleBridgeWillReloadNotification:)
|
|
|
|
name:RCTBridgeWillReloadNotification
|
|
|
|
object:_bridge];
|
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:self
|
|
|
|
selector:@selector(handleJavaScriptDidLoadNotification:)
|
|
|
|
name:RCTJavaScriptDidLoadNotification
|
|
|
|
object:_bridge];
|
2018-04-10 23:37:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2018-06-20 01:38:47 +00:00
|
|
|
- (void)dealloc
|
|
|
|
{
|
|
|
|
[[NSNotificationCenter defaultCenter] removeObserver:self];
|
|
|
|
}
|
|
|
|
|
2018-11-26 06:15:00 +00:00
|
|
|
- (RCTComponentViewFactory *)componentViewFactory
|
|
|
|
{
|
|
|
|
return _mountingManager.componentViewRegistry.componentViewFactory;
|
|
|
|
}
|
|
|
|
|
2018-04-10 23:37:06 +00:00
|
|
|
#pragma mark - Internal Surface-dedicated Interface
|
|
|
|
|
|
|
|
- (void)registerSurface:(RCTFabricSurface *)surface
|
|
|
|
{
|
|
|
|
[_surfaceRegistry registerSurface:surface];
|
2018-12-22 01:57:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)startSurface:(RCTFabricSurface *)surface
|
|
|
|
{
|
2018-09-26 17:02:06 +00:00
|
|
|
[self _startSurface:surface];
|
2018-04-10 23:37:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)unregisterSurface:(RCTFabricSurface *)surface
|
|
|
|
{
|
2018-09-26 17:02:06 +00:00
|
|
|
[self _stopSurface:surface];
|
2018-04-10 23:37:06 +00:00
|
|
|
[_surfaceRegistry unregisterSurface:surface];
|
|
|
|
}
|
|
|
|
|
2018-09-26 17:01:42 +00:00
|
|
|
- (void)setProps:(NSDictionary *)props
|
|
|
|
surface:(RCTFabricSurface *)surface
|
|
|
|
{
|
|
|
|
// This implementation is suboptimal indeed but still better than nothing for now.
|
2018-09-26 17:02:06 +00:00
|
|
|
[self _stopSurface:surface];
|
|
|
|
[self _startSurface:surface];
|
2018-09-26 17:01:42 +00:00
|
|
|
}
|
|
|
|
|
2018-06-15 03:39:44 +00:00
|
|
|
- (RCTFabricSurface *)surfaceForRootTag:(ReactTag)rootTag
|
|
|
|
{
|
|
|
|
return [_surfaceRegistry surfaceForRootTag:rootTag];
|
|
|
|
}
|
|
|
|
|
2018-05-09 05:56:27 +00:00
|
|
|
- (CGSize)sizeThatFitsMinimumSize:(CGSize)minimumSize
|
|
|
|
maximumSize:(CGSize)maximumSize
|
|
|
|
surface:(RCTFabricSurface *)surface
|
|
|
|
{
|
2018-10-09 23:25:08 +00:00
|
|
|
LayoutContext layoutContext = {
|
|
|
|
.pointScaleFactor = RCTScreenScale()
|
|
|
|
};
|
|
|
|
|
|
|
|
LayoutConstraints layoutConstraints = {
|
|
|
|
.minimumSize = RCTSizeFromCGSize(minimumSize),
|
|
|
|
.maximumSize = RCTSizeFromCGSize(maximumSize)
|
|
|
|
};
|
2018-05-09 05:56:27 +00:00
|
|
|
|
2018-09-26 17:02:06 +00:00
|
|
|
return [self._scheduler measureSurfaceWithLayoutConstraints:layoutConstraints
|
|
|
|
layoutContext:layoutContext
|
|
|
|
surfaceId:surface.rootTag];
|
2018-05-09 05:56:27 +00:00
|
|
|
}
|
|
|
|
|
2018-10-09 23:25:03 +00:00
|
|
|
- (void)setMinimumSize:(CGSize)minimumSize
|
2018-05-09 05:56:27 +00:00
|
|
|
maximumSize:(CGSize)maximumSize
|
|
|
|
surface:(RCTFabricSurface *)surface
|
|
|
|
{
|
2018-10-09 23:25:08 +00:00
|
|
|
LayoutContext layoutContext = {
|
|
|
|
.pointScaleFactor = RCTScreenScale()
|
|
|
|
};
|
|
|
|
|
|
|
|
LayoutConstraints layoutConstraints = {
|
|
|
|
.minimumSize = RCTSizeFromCGSize(minimumSize),
|
|
|
|
.maximumSize = RCTSizeFromCGSize(maximumSize)
|
|
|
|
};
|
2018-05-09 05:56:27 +00:00
|
|
|
|
2018-10-09 23:25:03 +00:00
|
|
|
[self._scheduler constraintSurfaceLayoutWithLayoutConstraints:layoutConstraints
|
|
|
|
layoutContext:layoutContext
|
|
|
|
surfaceId:surface.rootTag];
|
2018-09-26 17:02:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark - Private
|
|
|
|
|
|
|
|
- (RCTScheduler *)_scheduler
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock(_schedulerMutex);
|
|
|
|
|
|
|
|
if (_scheduler) {
|
|
|
|
return _scheduler;
|
|
|
|
}
|
|
|
|
|
2018-12-22 01:57:32 +00:00
|
|
|
_scheduler = [[RCTScheduler alloc] initWithContextContainer:self.contextContainer];
|
|
|
|
_scheduler.delegate = self;
|
2018-12-22 01:57:32 +00:00
|
|
|
|
2018-12-22 01:57:32 +00:00
|
|
|
return _scheduler;
|
|
|
|
}
|
|
|
|
|
|
|
|
@synthesize contextContainer = _contextContainer;
|
|
|
|
|
|
|
|
- (SharedContextContainer)contextContainer
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock(_contextContainerMutex);
|
2018-12-22 01:57:32 +00:00
|
|
|
|
2018-12-22 01:57:32 +00:00
|
|
|
if (_contextContainer) {
|
|
|
|
return _contextContainer;
|
|
|
|
}
|
|
|
|
|
|
|
|
_contextContainer = std::make_shared<ContextContainer>();
|
2018-09-26 17:02:06 +00:00
|
|
|
|
2018-12-22 01:57:32 +00:00
|
|
|
_contextContainer->registerInstance(_reactNativeConfig, "ReactNativeConfig");
|
2018-12-05 23:00:34 +00:00
|
|
|
|
2018-09-26 17:02:06 +00:00
|
|
|
auto messageQueueThread = _batchedBridge.jsMessageThread;
|
2018-10-29 20:02:27 +00:00
|
|
|
auto runtime = (facebook::jsi::Runtime *)((RCTCxxBridge *)_batchedBridge).runtime;
|
|
|
|
|
|
|
|
RuntimeExecutor runtimeExecutor =
|
|
|
|
[runtime, messageQueueThread](std::function<void(facebook::jsi::Runtime &runtime)> &&callback) {
|
|
|
|
messageQueueThread->runOnQueue([runtime, callback = std::move(callback)]() {
|
|
|
|
callback(*runtime);
|
|
|
|
});
|
|
|
|
};
|
2018-09-26 17:02:06 +00:00
|
|
|
|
2018-11-06 18:58:49 +00:00
|
|
|
EventBeatFactory synchronousBeatFactory = [runtimeExecutor]() {
|
|
|
|
return std::make_unique<MainRunLoopEventBeat>(runtimeExecutor);
|
2018-09-26 17:02:06 +00:00
|
|
|
};
|
|
|
|
|
2018-11-06 18:58:49 +00:00
|
|
|
EventBeatFactory asynchronousBeatFactory = [runtimeExecutor]() {
|
|
|
|
return std::make_unique<RuntimeEventBeat>(runtimeExecutor);
|
2018-09-26 17:02:06 +00:00
|
|
|
};
|
|
|
|
|
2018-12-22 01:57:32 +00:00
|
|
|
_contextContainer->registerInstance<EventBeatFactory>(synchronousBeatFactory, "synchronous");
|
|
|
|
_contextContainer->registerInstance<EventBeatFactory>(asynchronousBeatFactory, "asynchronous");
|
2018-09-26 17:02:06 +00:00
|
|
|
|
2018-12-22 01:57:32 +00:00
|
|
|
_contextContainer->registerInstance(runtimeExecutor, "runtime-executor");
|
2018-09-26 17:02:06 +00:00
|
|
|
|
2018-12-22 01:57:32 +00:00
|
|
|
_contextContainer->registerInstance(std::make_shared<ImageManager>((__bridge void *)[_bridge imageLoader]), "ImageManager");
|
2018-12-22 01:57:32 +00:00
|
|
|
return _contextContainer;
|
2018-05-09 05:56:27 +00:00
|
|
|
}
|
|
|
|
|
2018-09-26 17:02:06 +00:00
|
|
|
- (void)_startSurface:(RCTFabricSurface *)surface
|
2018-04-10 23:37:06 +00:00
|
|
|
{
|
2018-11-26 06:15:00 +00:00
|
|
|
[_mountingManager.componentViewRegistry dequeueComponentViewWithComponentHandle:RootShadowNode::Handle()
|
|
|
|
tag:surface.rootTag];
|
2018-09-26 17:01:42 +00:00
|
|
|
|
2018-10-09 23:25:08 +00:00
|
|
|
LayoutContext layoutContext = {
|
|
|
|
.pointScaleFactor = RCTScreenScale()
|
|
|
|
};
|
|
|
|
|
|
|
|
LayoutConstraints layoutConstraints = {
|
|
|
|
.minimumSize = RCTSizeFromCGSize(surface.minimumSize),
|
|
|
|
.maximumSize = RCTSizeFromCGSize(surface.maximumSize)
|
|
|
|
};
|
|
|
|
|
2018-09-26 17:02:06 +00:00
|
|
|
[self._scheduler startSurfaceWithSurfaceId:surface.rootTag
|
|
|
|
moduleName:surface.moduleName
|
2018-10-09 23:25:08 +00:00
|
|
|
initailProps:surface.properties
|
|
|
|
layoutConstraints:layoutConstraints
|
|
|
|
layoutContext:layoutContext];
|
2018-04-10 23:37:06 +00:00
|
|
|
}
|
|
|
|
|
2018-09-26 17:02:06 +00:00
|
|
|
- (void)_stopSurface:(RCTFabricSurface *)surface
|
2018-04-10 23:37:06 +00:00
|
|
|
{
|
2018-09-26 17:02:06 +00:00
|
|
|
[self._scheduler stopSurfaceWithSurfaceId:surface.rootTag];
|
2018-09-26 17:01:42 +00:00
|
|
|
|
2018-11-26 06:15:00 +00:00
|
|
|
UIView<RCTComponentViewProtocol> *rootView =
|
|
|
|
[_mountingManager.componentViewRegistry componentViewByTag:surface.rootTag];
|
|
|
|
[_mountingManager.componentViewRegistry enqueueComponentViewWithComponentHandle:RootShadowNode::Handle()
|
|
|
|
tag:surface.rootTag
|
|
|
|
componentView:rootView];
|
2018-09-26 17:01:42 +00:00
|
|
|
|
|
|
|
[surface _unsetStage:(RCTSurfaceStagePrepared | RCTSurfaceStageMounted)];
|
2018-04-10 23:37:06 +00:00
|
|
|
}
|
|
|
|
|
2018-09-26 17:02:06 +00:00
|
|
|
- (void)_startAllSurfaces
|
2018-09-26 17:01:42 +00:00
|
|
|
{
|
|
|
|
for (RCTFabricSurface *surface in _surfaceRegistry.enumerator) {
|
2018-09-26 17:02:06 +00:00
|
|
|
[self _startSurface:surface];
|
2018-09-26 17:01:42 +00:00
|
|
|
}
|
|
|
|
}
|
2018-04-10 23:37:06 +00:00
|
|
|
|
2018-09-26 17:02:06 +00:00
|
|
|
- (void)_stopAllSurfaces
|
2018-04-10 23:37:06 +00:00
|
|
|
{
|
2018-09-26 17:01:42 +00:00
|
|
|
for (RCTFabricSurface *surface in _surfaceRegistry.enumerator) {
|
2018-09-26 17:02:06 +00:00
|
|
|
[self _stopSurface:surface];
|
2018-09-26 17:01:42 +00:00
|
|
|
}
|
2018-04-10 23:37:06 +00:00
|
|
|
}
|
|
|
|
|
2018-09-26 17:01:42 +00:00
|
|
|
#pragma mark - RCTSchedulerDelegate
|
|
|
|
|
|
|
|
- (void)schedulerDidFinishTransaction:(facebook::react::ShadowViewMutationList)mutations
|
|
|
|
rootTag:(ReactTag)rootTag
|
2018-04-10 23:37:06 +00:00
|
|
|
{
|
|
|
|
RCTFabricSurface *surface = [_surfaceRegistry surfaceForRootTag:rootTag];
|
|
|
|
|
2018-09-26 17:01:42 +00:00
|
|
|
[surface _setStage:RCTSurfaceStagePrepared];
|
|
|
|
|
|
|
|
[_mountingManager performTransactionWithMutations:mutations
|
|
|
|
rootTag:rootTag];
|
|
|
|
}
|
|
|
|
|
2018-11-26 06:15:00 +00:00
|
|
|
- (void)schedulerOptimisticallyCreateComponentViewWithComponentHandle:(ComponentHandle)componentHandle
|
2018-09-26 17:01:42 +00:00
|
|
|
{
|
2018-11-26 06:15:00 +00:00
|
|
|
[_mountingManager optimisticallyCreateComponentViewWithComponentHandle:componentHandle];
|
2018-09-26 17:01:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark - RCTMountingManagerDelegate
|
|
|
|
|
|
|
|
- (void)mountingManager:(RCTMountingManager *)mountingManager willMountComponentsWithRootTag:(ReactTag)rootTag
|
|
|
|
{
|
|
|
|
RCTAssertMainQueue();
|
|
|
|
|
|
|
|
// Does nothing.
|
|
|
|
}
|
2018-04-10 23:37:06 +00:00
|
|
|
|
2018-09-26 17:01:42 +00:00
|
|
|
- (void)mountingManager:(RCTMountingManager *)mountingManager didMountComponentsWithRootTag:(ReactTag)rootTag
|
|
|
|
{
|
|
|
|
RCTAssertMainQueue();
|
2018-04-10 23:37:06 +00:00
|
|
|
|
2018-09-26 17:01:42 +00:00
|
|
|
RCTFabricSurface *surface = [_surfaceRegistry surfaceForRootTag:rootTag];
|
|
|
|
RCTSurfaceStage stage = surface.stage;
|
|
|
|
if (stage & RCTSurfaceStagePrepared) {
|
|
|
|
// We have to progress the stage only if the preparing phase is done.
|
|
|
|
if ([surface _setStage:RCTSurfaceStageMounted]) {
|
|
|
|
UIView *rootComponentView = [_mountingManager.componentViewRegistry componentViewByTag:rootTag];
|
|
|
|
surface.view.rootView = (RCTSurfaceRootView *)rootComponentView;
|
|
|
|
}
|
|
|
|
}
|
2018-04-10 23:37:06 +00:00
|
|
|
}
|
|
|
|
|
2018-06-15 17:56:40 +00:00
|
|
|
#pragma mark - Bridge events
|
|
|
|
|
|
|
|
- (void)handleBridgeWillReloadNotification:(NSNotification *)notification
|
|
|
|
{
|
2018-09-26 17:02:06 +00:00
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock(_schedulerMutex);
|
|
|
|
if (!_scheduler) {
|
|
|
|
// Seems we are already in the realoding process.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[self _stopAllSurfaces];
|
|
|
|
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock(_schedulerMutex);
|
|
|
|
_scheduler = nil;
|
2018-12-22 01:57:32 +00:00
|
|
|
_contextContainer = nil;
|
2018-09-26 17:02:06 +00:00
|
|
|
}
|
2018-06-15 17:56:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)handleJavaScriptDidLoadNotification:(NSNotification *)notification
|
|
|
|
{
|
|
|
|
RCTBridge *bridge = notification.userInfo[@"bridge"];
|
|
|
|
if (bridge != _batchedBridge) {
|
|
|
|
_batchedBridge = bridge;
|
2018-09-26 17:01:42 +00:00
|
|
|
|
2018-09-26 17:02:06 +00:00
|
|
|
[self _startAllSurfaces];
|
2018-06-15 17:56:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-10 23:37:06 +00:00
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation RCTSurfacePresenter (Deprecated)
|
|
|
|
|
2018-08-01 22:23:06 +00:00
|
|
|
- (RCTBridge *)bridge_DO_NOT_USE
|
|
|
|
{
|
|
|
|
return _bridge;
|
|
|
|
}
|
|
|
|
|
2018-04-10 23:37:06 +00:00
|
|
|
@end
|
2018-10-29 20:02:27 +00:00
|
|
|
|
|
|
|
@implementation RCTBridge (Deprecated)
|
|
|
|
|
|
|
|
- (void)setSurfacePresenter:(RCTSurfacePresenter *)surfacePresenter
|
|
|
|
{
|
|
|
|
objc_setAssociatedObject(self, @selector(surfacePresenter), surfacePresenter, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
|
|
|
|
}
|
|
|
|
|
|
|
|
- (RCTSurfacePresenter *)surfacePresenter
|
|
|
|
{
|
|
|
|
return objc_getAssociatedObject(self, @selector(surfacePresenter));
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|