2019-10-16 20:03:47 +03:00
|
|
|
/*
|
2018-09-12 01:27:47 +03:00
|
|
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
2018-04-11 02:37:06 +03:00
|
|
|
*
|
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
|
|
|
*/
|
|
|
|
|
2019-08-08 17:13:22 +03:00
|
|
|
#import "RCTSurfacePresenter.h"
|
2018-04-11 02:37:06 +03:00
|
|
|
|
2018-09-26 20:02:06 +03:00
|
|
|
#import <mutex>
|
|
|
|
|
2018-04-11 02:37:06 +03:00
|
|
|
#import <React/RCTAssert.h>
|
2019-08-08 17:13:22 +03:00
|
|
|
#import <React/RCTComponentViewFactory.h>
|
|
|
|
#import <React/RCTComponentViewRegistry.h>
|
2020-08-12 22:11:04 +03:00
|
|
|
#import <React/RCTConstants.h>
|
2019-08-08 17:13:22 +03:00
|
|
|
#import <React/RCTFabricSurface.h>
|
2019-02-25 23:22:24 +03:00
|
|
|
#import <React/RCTFollyConvert.h>
|
2020-03-18 00:05:23 +03:00
|
|
|
#import <React/RCTI18nUtil.h>
|
2019-08-08 17:13:22 +03:00
|
|
|
#import <React/RCTMountingManager.h>
|
|
|
|
#import <React/RCTMountingManagerDelegate.h>
|
|
|
|
#import <React/RCTScheduler.h>
|
|
|
|
#import <React/RCTSurfaceRegistry.h>
|
2018-04-11 02:37:06 +03:00
|
|
|
#import <React/RCTSurfaceView+Internal.h>
|
2019-02-25 23:22:24 +03:00
|
|
|
#import <React/RCTSurfaceView.h>
|
2018-06-22 17:28:29 +03:00
|
|
|
#import <React/RCTUtils.h>
|
2019-04-30 07:17:40 +03:00
|
|
|
|
2020-04-11 22:12:58 +03:00
|
|
|
#import <react/config/ReactNativeConfig.h>
|
2020-07-31 23:28:05 +03:00
|
|
|
#import <react/renderer/componentregistry/ComponentDescriptorFactory.h>
|
|
|
|
#import <react/renderer/components/root/RootShadowNode.h>
|
|
|
|
#import <react/renderer/core/LayoutConstraints.h>
|
|
|
|
#import <react/renderer/core/LayoutContext.h>
|
|
|
|
#import <react/renderer/scheduler/AsynchronousEventBeat.h>
|
|
|
|
#import <react/renderer/scheduler/SchedulerToolbox.h>
|
|
|
|
#import <react/renderer/scheduler/SynchronousEventBeat.h>
|
2019-04-13 01:10:40 +03:00
|
|
|
#import <react/utils/ContextContainer.h>
|
2019-04-30 07:17:40 +03:00
|
|
|
#import <react/utils/ManagedObjectWrapper.h>
|
2018-05-09 08:56:27 +03:00
|
|
|
|
2020-05-11 07:29:32 +03:00
|
|
|
#import "PlatformRunLoopObserver.h"
|
2019-08-08 17:13:22 +03:00
|
|
|
#import "RCTConversions.h"
|
2018-05-09 08:56:27 +03:00
|
|
|
|
2018-04-11 02:37:06 +03:00
|
|
|
using namespace facebook::react;
|
|
|
|
|
2021-02-18 23:37:05 +03:00
|
|
|
static inline LayoutConstraints RCTGetLayoutConstraintsForSize(CGSize minimumSize, CGSize maximumSize)
|
|
|
|
{
|
|
|
|
return {
|
|
|
|
.minimumSize = RCTSizeFromCGSize(minimumSize),
|
|
|
|
.maximumSize = RCTSizeFromCGSize(maximumSize),
|
|
|
|
.layoutDirection = RCTLayoutDirection([[RCTI18nUtil sharedInstance] isRTL]),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline LayoutContext RCTGetLayoutContext(CGPoint viewportOffset)
|
|
|
|
{
|
|
|
|
return {
|
|
|
|
.pointScaleFactor = RCTScreenScale(),
|
|
|
|
.swapLeftAndRightInRTL =
|
|
|
|
[[RCTI18nUtil sharedInstance] isRTL] && [[RCTI18nUtil sharedInstance] doLeftAndRightSwapInRTL],
|
|
|
|
.fontSizeMultiplier = RCTFontSizeMultiplier(),
|
|
|
|
.viewportOffset = RCTPointFromCGPoint(viewportOffset)};
|
|
|
|
}
|
|
|
|
|
2020-07-25 09:50:50 +03:00
|
|
|
static dispatch_queue_t RCTGetBackgroundQueue()
|
|
|
|
{
|
|
|
|
static dispatch_queue_t queue;
|
|
|
|
static dispatch_once_t onceToken;
|
|
|
|
dispatch_once(&onceToken, ^{
|
|
|
|
dispatch_queue_attr_t attr =
|
|
|
|
dispatch_queue_attr_make_with_qos_class(DISPATCH_QUEUE_SERIAL, QOS_CLASS_USER_INTERACTIVE, 0);
|
|
|
|
queue = dispatch_queue_create("com.facebook.react.background", attr);
|
|
|
|
});
|
|
|
|
return queue;
|
|
|
|
}
|
|
|
|
|
|
|
|
static BackgroundExecutor RCTGetBackgroundExecutor()
|
|
|
|
{
|
|
|
|
return [](std::function<void()> &&callback) {
|
|
|
|
if (RCTIsMainQueue()) {
|
|
|
|
callback();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto copyableCallback = callback;
|
|
|
|
dispatch_async(RCTGetBackgroundQueue(), ^{
|
|
|
|
copyableCallback();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-08-08 17:13:22 +03:00
|
|
|
@interface RCTSurfacePresenter () <RCTSchedulerDelegate, RCTMountingManagerDelegate>
|
2018-04-11 02:37:06 +03:00
|
|
|
@end
|
|
|
|
|
2019-08-08 17:13:22 +03:00
|
|
|
@implementation RCTSurfacePresenter {
|
|
|
|
RCTMountingManager *_mountingManager; // Thread-safe.
|
|
|
|
RCTSurfaceRegistry *_surfaceRegistry; // Thread-safe.
|
2019-12-30 22:13:07 +03:00
|
|
|
|
2020-03-26 20:09:55 +03:00
|
|
|
std::mutex _schedulerAccessMutex;
|
|
|
|
std::mutex _schedulerLifeCycleMutex;
|
|
|
|
RCTScheduler *_Nullable _scheduler; // Thread-safe. Pointer is protected by `_schedulerAccessMutex`.
|
|
|
|
ContextContainer::Shared _contextContainer; // Protected by `_schedulerLifeCycleMutex`.
|
|
|
|
RuntimeExecutor _runtimeExecutor; // Protected by `_schedulerLifeCycleMutex`.
|
2019-12-30 22:13:07 +03:00
|
|
|
|
2019-03-26 19:52:27 +03:00
|
|
|
better::shared_mutex _observerListMutex;
|
2019-08-08 17:13:22 +03:00
|
|
|
NSMutableArray<id<RCTSurfacePresenterObserver>> *_observers;
|
2018-04-11 02:37:06 +03:00
|
|
|
}
|
|
|
|
|
2019-10-25 03:43:08 +03:00
|
|
|
- (instancetype)initWithContextContainer:(ContextContainer::Shared)contextContainer
|
|
|
|
runtimeExecutor:(RuntimeExecutor)runtimeExecutor
|
2018-04-11 02:37:06 +03:00
|
|
|
{
|
|
|
|
if (self = [super init]) {
|
2019-10-25 03:43:08 +03:00
|
|
|
assert(contextContainer && "RuntimeExecutor must be not null.");
|
|
|
|
|
2019-08-15 21:27:29 +03:00
|
|
|
_runtimeExecutor = runtimeExecutor;
|
2019-10-25 03:43:08 +03:00
|
|
|
_contextContainer = contextContainer;
|
2018-04-11 02:37:06 +03:00
|
|
|
|
2019-08-08 17:13:22 +03:00
|
|
|
_surfaceRegistry = [[RCTSurfaceRegistry alloc] init];
|
|
|
|
_mountingManager = [[RCTMountingManager alloc] init];
|
2018-04-11 02:37:06 +03:00
|
|
|
_mountingManager.delegate = self;
|
2018-06-15 20:56:40 +03:00
|
|
|
|
2019-03-20 08:11:32 +03:00
|
|
|
_observers = [NSMutableArray array];
|
2018-12-06 02:00:34 +03:00
|
|
|
|
2019-10-25 03:43:08 +03:00
|
|
|
_scheduler = [self _createScheduler];
|
2021-02-18 23:37:05 +03:00
|
|
|
|
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:self
|
|
|
|
selector:@selector(_handleContentSizeCategoryDidChangeNotification:)
|
|
|
|
name:UIContentSizeCategoryDidChangeNotification
|
|
|
|
object:nil];
|
2018-04-11 02:37:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2020-03-26 20:09:55 +03:00
|
|
|
- (RCTScheduler *_Nullable)_scheduler
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock(_schedulerAccessMutex);
|
|
|
|
return _scheduler;
|
|
|
|
}
|
|
|
|
|
2019-10-25 03:43:08 +03:00
|
|
|
- (ContextContainer::Shared)contextContainer
|
2018-11-26 09:15:00 +03:00
|
|
|
{
|
2020-03-26 20:09:55 +03:00
|
|
|
std::lock_guard<std::mutex> lock(_schedulerLifeCycleMutex);
|
2019-10-25 03:43:08 +03:00
|
|
|
return _contextContainer;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setContextContainer:(ContextContainer::Shared)contextContainer
|
|
|
|
{
|
2020-03-26 20:09:55 +03:00
|
|
|
std::lock_guard<std::mutex> lock(_schedulerLifeCycleMutex);
|
2019-10-25 03:43:08 +03:00
|
|
|
_contextContainer = contextContainer;
|
|
|
|
}
|
|
|
|
|
2019-12-30 22:13:07 +03:00
|
|
|
- (RuntimeExecutor)runtimeExecutor
|
2019-10-25 03:43:08 +03:00
|
|
|
{
|
2020-03-26 20:09:55 +03:00
|
|
|
std::lock_guard<std::mutex> lock(_schedulerLifeCycleMutex);
|
2019-12-30 22:13:07 +03:00
|
|
|
return _runtimeExecutor;
|
2019-10-25 03:43:08 +03:00
|
|
|
}
|
|
|
|
|
2019-12-30 22:13:07 +03:00
|
|
|
- (void)setRuntimeExecutor:(RuntimeExecutor)runtimeExecutor
|
2019-10-25 03:43:08 +03:00
|
|
|
{
|
2020-03-26 20:09:55 +03:00
|
|
|
std::lock_guard<std::mutex> lock(_schedulerLifeCycleMutex);
|
2019-12-30 22:13:07 +03:00
|
|
|
_runtimeExecutor = runtimeExecutor;
|
2018-11-26 09:15:00 +03:00
|
|
|
}
|
|
|
|
|
2018-04-11 02:37:06 +03:00
|
|
|
#pragma mark - Internal Surface-dedicated Interface
|
|
|
|
|
2019-08-08 17:13:22 +03:00
|
|
|
- (void)registerSurface:(RCTFabricSurface *)surface
|
2018-04-11 02:37:06 +03:00
|
|
|
{
|
2021-02-09 19:08:09 +03:00
|
|
|
RCTScheduler *scheduler = [self _scheduler];
|
2021-02-18 23:37:05 +03:00
|
|
|
[_surfaceRegistry registerSurface:surface];
|
2020-03-26 20:09:55 +03:00
|
|
|
if (scheduler) {
|
2021-02-18 23:37:05 +03:00
|
|
|
[self _startSurface:surface scheduler:scheduler];
|
2019-10-25 03:43:08 +03:00
|
|
|
}
|
2018-04-11 02:37:06 +03:00
|
|
|
}
|
|
|
|
|
2019-08-08 17:13:22 +03:00
|
|
|
- (void)unregisterSurface:(RCTFabricSurface *)surface
|
2018-04-11 02:37:06 +03:00
|
|
|
{
|
2020-03-26 20:09:55 +03:00
|
|
|
RCTScheduler *scheduler = [self _scheduler];
|
|
|
|
if (scheduler) {
|
2021-02-18 23:37:05 +03:00
|
|
|
[self _stopSurface:surface scheduler:scheduler];
|
2019-10-25 03:43:08 +03:00
|
|
|
}
|
2018-04-11 02:37:06 +03:00
|
|
|
[_surfaceRegistry unregisterSurface:surface];
|
|
|
|
}
|
|
|
|
|
2021-02-18 23:37:05 +03:00
|
|
|
- (void)setProps:(NSDictionary *)props surface:(RCTFabricSurface *)surface
|
|
|
|
{
|
|
|
|
RCTScheduler *scheduler = [self _scheduler];
|
|
|
|
if (scheduler) {
|
|
|
|
[self _stopSurface:surface scheduler:scheduler];
|
|
|
|
[self _startSurface:surface scheduler:scheduler];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-08 17:13:22 +03:00
|
|
|
- (RCTFabricSurface *)surfaceForRootTag:(ReactTag)rootTag
|
2018-06-15 06:39:44 +03:00
|
|
|
{
|
|
|
|
return [_surfaceRegistry surfaceForRootTag:rootTag];
|
|
|
|
}
|
|
|
|
|
2021-02-18 23:37:05 +03:00
|
|
|
- (CGSize)sizeThatFitsMinimumSize:(CGSize)minimumSize
|
|
|
|
maximumSize:(CGSize)maximumSize
|
|
|
|
surface:(RCTFabricSurface *)surface
|
|
|
|
{
|
|
|
|
RCTScheduler *scheduler = [self _scheduler];
|
|
|
|
if (!scheduler) {
|
|
|
|
return minimumSize;
|
|
|
|
}
|
|
|
|
LayoutContext layoutContext = RCTGetLayoutContext(surface.viewportOffset);
|
|
|
|
LayoutConstraints layoutConstraints = RCTGetLayoutConstraintsForSize(minimumSize, maximumSize);
|
|
|
|
return [scheduler measureSurfaceWithLayoutConstraints:layoutConstraints
|
|
|
|
layoutContext:layoutContext
|
|
|
|
surfaceId:surface.rootTag];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setMinimumSize:(CGSize)minimumSize maximumSize:(CGSize)maximumSize surface:(RCTFabricSurface *)surface
|
|
|
|
{
|
|
|
|
RCTScheduler *scheduler = [self _scheduler];
|
|
|
|
if (!scheduler) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
LayoutContext layoutContext = RCTGetLayoutContext(surface.viewportOffset);
|
|
|
|
LayoutConstraints layoutConstraints = RCTGetLayoutConstraintsForSize(minimumSize, maximumSize);
|
|
|
|
[scheduler constraintSurfaceLayoutWithLayoutConstraints:layoutConstraints
|
|
|
|
layoutContext:layoutContext
|
|
|
|
surfaceId:surface.rootTag];
|
|
|
|
}
|
|
|
|
|
2020-06-30 01:24:35 +03:00
|
|
|
- (UIView *)findComponentViewWithTag_DO_NOT_USE_DEPRECATED:(NSInteger)tag
|
|
|
|
{
|
|
|
|
UIView<RCTComponentViewProtocol> *componentView =
|
|
|
|
[_mountingManager.componentViewRegistry findComponentViewWithTag:tag];
|
|
|
|
return componentView;
|
|
|
|
}
|
|
|
|
|
2019-02-25 23:22:24 +03:00
|
|
|
- (BOOL)synchronouslyUpdateViewOnUIThread:(NSNumber *)reactTag props:(NSDictionary *)props
|
|
|
|
{
|
2020-03-26 20:09:55 +03:00
|
|
|
RCTScheduler *scheduler = [self _scheduler];
|
|
|
|
if (!scheduler) {
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
2019-02-25 23:22:24 +03:00
|
|
|
ReactTag tag = [reactTag integerValue];
|
2019-11-01 19:35:40 +03:00
|
|
|
UIView<RCTComponentViewProtocol> *componentView =
|
|
|
|
[_mountingManager.componentViewRegistry findComponentViewWithTag:tag];
|
2019-02-25 23:22:24 +03:00
|
|
|
if (componentView == nil) {
|
|
|
|
return NO; // This view probably isn't managed by Fabric
|
|
|
|
}
|
2019-04-18 08:39:40 +03:00
|
|
|
ComponentHandle handle = [[componentView class] componentDescriptorProvider].handle;
|
2020-03-26 20:09:55 +03:00
|
|
|
auto *componentDescriptor = [scheduler findComponentDescriptorByHandle_DO_NOT_USE_THIS_IS_BROKEN:handle];
|
2019-12-07 04:56:37 +03:00
|
|
|
|
|
|
|
if (!componentDescriptor) {
|
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
|
|
|
[_mountingManager synchronouslyUpdateViewOnUIThread:tag changedProps:props componentDescriptor:*componentDescriptor];
|
2019-02-25 23:22:24 +03:00
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
2021-02-18 23:37:05 +03:00
|
|
|
- (BOOL)synchronouslyWaitSurface:(RCTFabricSurface *)surface timeout:(NSTimeInterval)timeout
|
2020-01-30 21:09:06 +03:00
|
|
|
{
|
2021-02-18 23:37:05 +03:00
|
|
|
RCTScheduler *scheduler = [self _scheduler];
|
|
|
|
if (!scheduler) {
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto mountingCoordinator = [scheduler mountingCoordinatorWithSurfaceId:surface.rootTag];
|
|
|
|
|
|
|
|
if (!mountingCoordinator->waitForTransaction(std::chrono::duration<NSTimeInterval>(timeout))) {
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
|
|
|
[_mountingManager scheduleTransaction:mountingCoordinator];
|
|
|
|
|
|
|
|
return YES;
|
2020-01-30 21:09:06 +03:00
|
|
|
}
|
|
|
|
|
2019-10-25 03:43:08 +03:00
|
|
|
- (BOOL)suspend
|
|
|
|
{
|
2020-03-26 20:09:55 +03:00
|
|
|
std::lock_guard<std::mutex> lock(_schedulerLifeCycleMutex);
|
2019-10-25 03:43:08 +03:00
|
|
|
|
2020-03-26 20:09:55 +03:00
|
|
|
RCTScheduler *scheduler;
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> accessLock(_schedulerAccessMutex);
|
|
|
|
|
|
|
|
if (!_scheduler) {
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
scheduler = _scheduler;
|
|
|
|
_scheduler = nil;
|
2019-10-25 03:43:08 +03:00
|
|
|
}
|
|
|
|
|
2020-03-26 20:09:55 +03:00
|
|
|
[self _stopAllSurfacesWithScheduler:scheduler];
|
2019-10-25 03:43:08 +03:00
|
|
|
|
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)resume
|
|
|
|
{
|
2020-03-26 20:09:55 +03:00
|
|
|
std::lock_guard<std::mutex> lock(_schedulerLifeCycleMutex);
|
2019-10-25 03:43:08 +03:00
|
|
|
|
2020-03-26 20:09:55 +03:00
|
|
|
RCTScheduler *scheduler;
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> accessLock(_schedulerAccessMutex);
|
|
|
|
|
|
|
|
if (_scheduler) {
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
scheduler = [self _createScheduler];
|
2019-10-25 03:43:08 +03:00
|
|
|
}
|
|
|
|
|
2020-03-26 20:09:55 +03:00
|
|
|
[self _startAllSurfacesWithScheduler:scheduler];
|
|
|
|
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> accessLock(_schedulerAccessMutex);
|
|
|
|
_scheduler = scheduler;
|
|
|
|
}
|
2019-10-25 03:43:08 +03:00
|
|
|
|
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
2018-09-26 20:02:06 +03:00
|
|
|
#pragma mark - Private
|
|
|
|
|
2019-10-25 03:43:08 +03:00
|
|
|
- (RCTScheduler *)_createScheduler
|
2019-09-11 07:30:53 +03:00
|
|
|
{
|
2020-05-11 07:29:32 +03:00
|
|
|
auto reactNativeConfig = _contextContainer->at<std::shared_ptr<ReactNativeConfig const>>("ReactNativeConfig");
|
|
|
|
|
2020-07-02 04:23:06 +03:00
|
|
|
if (reactNativeConfig && reactNativeConfig->getBool("react_fabric:scrollview_on_demand_mounting_ios")) {
|
2020-08-12 22:11:04 +03:00
|
|
|
RCTExperimentSetOnDemandViewMounting(YES);
|
2020-07-02 04:23:06 +03:00
|
|
|
}
|
|
|
|
|
2020-09-16 08:27:36 +03:00
|
|
|
if (reactNativeConfig && reactNativeConfig->getBool("react_fabric:optimized_hit_testing_ios")) {
|
|
|
|
RCTExperimentSetOptimizedHitTesting(YES);
|
|
|
|
}
|
|
|
|
|
2020-10-17 03:54:26 +03:00
|
|
|
if (reactNativeConfig && reactNativeConfig->getBool("react_fabric:preemptive_view_allocation_disabled_ios")) {
|
|
|
|
RCTExperimentSetPreemptiveViewAllocationDisabled(YES);
|
|
|
|
}
|
|
|
|
|
2021-01-20 21:05:06 +03:00
|
|
|
if (reactNativeConfig && reactNativeConfig->getBool("react_fabric:release_resources_when_backgrounded_ios")) {
|
|
|
|
RCTExperimentSetReleaseResourcesWhenBackgrounded(YES);
|
|
|
|
}
|
|
|
|
|
2019-12-19 18:33:36 +03:00
|
|
|
auto componentRegistryFactory =
|
|
|
|
[factory = wrapManagedObject(_mountingManager.componentViewRegistry.componentViewFactory)](
|
|
|
|
EventDispatcher::Weak const &eventDispatcher, ContextContainer::Shared const &contextContainer) {
|
|
|
|
return [(RCTComponentViewFactory *)unwrapManagedObject(factory)
|
|
|
|
createComponentDescriptorRegistryWithParameters:{eventDispatcher, contextContainer}];
|
|
|
|
};
|
2019-04-18 08:39:41 +03:00
|
|
|
|
2019-10-25 03:43:08 +03:00
|
|
|
auto runtimeExecutor = _runtimeExecutor;
|
2019-09-11 07:30:53 +03:00
|
|
|
|
2019-06-07 22:00:01 +03:00
|
|
|
auto toolbox = SchedulerToolbox{};
|
2019-10-25 03:43:08 +03:00
|
|
|
toolbox.contextContainer = _contextContainer;
|
2019-06-07 22:00:01 +03:00
|
|
|
toolbox.componentRegistryFactory = componentRegistryFactory;
|
|
|
|
toolbox.runtimeExecutor = runtimeExecutor;
|
2020-05-11 07:29:32 +03:00
|
|
|
toolbox.mainRunLoopObserverFactory = [](RunLoopObserver::Activity activities,
|
|
|
|
RunLoopObserver::WeakOwner const &owner) {
|
|
|
|
return std::make_unique<MainRunLoopObserver>(activities, owner);
|
2019-06-07 22:00:01 +03:00
|
|
|
};
|
|
|
|
|
2020-07-25 09:50:50 +03:00
|
|
|
if (reactNativeConfig && reactNativeConfig->getBool("react_fabric:enable_background_executor_ios")) {
|
|
|
|
toolbox.backgroundExecutor = RCTGetBackgroundExecutor();
|
|
|
|
}
|
|
|
|
|
2020-09-11 18:21:26 +03:00
|
|
|
toolbox.synchronousEventBeatFactory = [runtimeExecutor](EventBeat::SharedOwnerBox const &ownerBox) {
|
|
|
|
auto runLoopObserver =
|
|
|
|
std::make_unique<MainRunLoopObserver const>(RunLoopObserver::Activity::BeforeWaiting, ownerBox->owner);
|
|
|
|
return std::make_unique<SynchronousEventBeat>(std::move(runLoopObserver), runtimeExecutor);
|
|
|
|
};
|
|
|
|
|
|
|
|
toolbox.asynchronousEventBeatFactory = [runtimeExecutor](EventBeat::SharedOwnerBox const &ownerBox) {
|
|
|
|
auto runLoopObserver =
|
|
|
|
std::make_unique<MainRunLoopObserver const>(RunLoopObserver::Activity::BeforeWaiting, ownerBox->owner);
|
|
|
|
return std::make_unique<AsynchronousEventBeat>(std::move(runLoopObserver), runtimeExecutor);
|
|
|
|
};
|
2019-06-07 22:00:01 +03:00
|
|
|
|
2019-10-25 03:43:08 +03:00
|
|
|
RCTScheduler *scheduler = [[RCTScheduler alloc] initWithToolbox:toolbox];
|
|
|
|
scheduler.delegate = self;
|
2018-12-22 04:57:32 +03:00
|
|
|
|
2019-10-25 03:43:08 +03:00
|
|
|
return scheduler;
|
2019-09-11 07:30:53 +03:00
|
|
|
}
|
2018-12-22 04:57:32 +03:00
|
|
|
|
2021-02-18 23:37:05 +03:00
|
|
|
- (void)_startSurface:(RCTFabricSurface *)surface scheduler:(RCTScheduler *)scheduler
|
|
|
|
{
|
|
|
|
RCTMountingManager *mountingManager = _mountingManager;
|
|
|
|
RCTExecuteOnMainQueue(^{
|
|
|
|
[mountingManager attachSurfaceToView:surface.view surfaceId:surface.rootTag];
|
|
|
|
});
|
|
|
|
|
|
|
|
LayoutContext layoutContext = RCTGetLayoutContext(surface.viewportOffset);
|
|
|
|
|
|
|
|
LayoutConstraints layoutConstraints = RCTGetLayoutConstraintsForSize(surface.minimumSize, surface.maximumSize);
|
|
|
|
|
|
|
|
[scheduler startSurfaceWithSurfaceId:surface.rootTag
|
|
|
|
moduleName:surface.moduleName
|
|
|
|
initialProps:surface.properties
|
|
|
|
layoutConstraints:layoutConstraints
|
|
|
|
layoutContext:layoutContext];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)_stopSurface:(RCTFabricSurface *)surface scheduler:(RCTScheduler *)scheduler
|
|
|
|
{
|
|
|
|
[scheduler stopSurfaceWithSurfaceId:surface.rootTag];
|
|
|
|
|
|
|
|
RCTMountingManager *mountingManager = _mountingManager;
|
|
|
|
RCTExecuteOnMainQueue(^{
|
|
|
|
[mountingManager detachSurfaceFromView:surface.view surfaceId:surface.rootTag];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-03-26 20:09:55 +03:00
|
|
|
- (void)_startAllSurfacesWithScheduler:(RCTScheduler *)scheduler
|
2018-09-26 20:01:42 +03:00
|
|
|
{
|
2019-08-08 17:13:22 +03:00
|
|
|
[_surfaceRegistry enumerateWithBlock:^(NSEnumerator<RCTFabricSurface *> *enumerator) {
|
|
|
|
for (RCTFabricSurface *surface in enumerator) {
|
2021-02-18 23:37:05 +03:00
|
|
|
[self _startSurface:surface scheduler:scheduler];
|
2019-03-22 09:34:14 +03:00
|
|
|
}
|
|
|
|
}];
|
2018-09-26 20:01:42 +03:00
|
|
|
}
|
2018-04-11 02:37:06 +03:00
|
|
|
|
2020-03-26 20:09:55 +03:00
|
|
|
- (void)_stopAllSurfacesWithScheduler:(RCTScheduler *)scheduler
|
2018-04-11 02:37:06 +03:00
|
|
|
{
|
2019-08-08 17:13:22 +03:00
|
|
|
[_surfaceRegistry enumerateWithBlock:^(NSEnumerator<RCTFabricSurface *> *enumerator) {
|
|
|
|
for (RCTFabricSurface *surface in enumerator) {
|
2021-02-18 23:37:05 +03:00
|
|
|
[self _stopSurface:surface scheduler:scheduler];
|
|
|
|
}
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)_handleContentSizeCategoryDidChangeNotification:(NSNotification *)notification
|
|
|
|
{
|
|
|
|
RCTScheduler *scheduler = [self _scheduler];
|
|
|
|
|
|
|
|
[_surfaceRegistry enumerateWithBlock:^(NSEnumerator<RCTFabricSurface *> *enumerator) {
|
|
|
|
for (RCTFabricSurface *surface in enumerator) {
|
|
|
|
LayoutContext layoutContext = RCTGetLayoutContext(surface.viewportOffset);
|
|
|
|
|
|
|
|
LayoutConstraints layoutConstraints = RCTGetLayoutConstraintsForSize(surface.minimumSize, surface.maximumSize);
|
|
|
|
|
|
|
|
[scheduler constraintSurfaceLayoutWithLayoutConstraints:layoutConstraints
|
|
|
|
layoutContext:layoutContext
|
|
|
|
surfaceId:surface.rootTag];
|
2020-08-05 01:09:44 +03:00
|
|
|
}
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
|
2019-08-08 17:13:22 +03:00
|
|
|
#pragma mark - RCTSchedulerDelegate
|
2018-09-26 20:01:42 +03:00
|
|
|
|
2019-10-25 03:43:08 +03:00
|
|
|
- (void)schedulerDidFinishTransaction:(MountingCoordinator::Shared const &)mountingCoordinator
|
2018-04-11 02:37:06 +03:00
|
|
|
{
|
2019-05-04 01:05:26 +03:00
|
|
|
[_mountingManager scheduleTransaction:mountingCoordinator];
|
2018-09-26 20:01:42 +03:00
|
|
|
}
|
|
|
|
|
2019-10-25 03:43:08 +03:00
|
|
|
- (void)schedulerDidDispatchCommand:(ShadowView const &)shadowView
|
2019-07-19 21:45:40 +03:00
|
|
|
commandName:(std::string const &)commandName
|
|
|
|
args:(folly::dynamic const)args
|
|
|
|
{
|
|
|
|
ReactTag tag = shadowView.tag;
|
|
|
|
NSString *commandStr = [[NSString alloc] initWithUTF8String:commandName.c_str()];
|
|
|
|
NSArray *argsArray = convertFollyDynamicToId(args);
|
|
|
|
|
|
|
|
[self->_mountingManager dispatchCommand:tag commandName:commandStr args:argsArray];
|
|
|
|
}
|
|
|
|
|
2021-01-09 05:08:30 +03:00
|
|
|
- (void)schedulerDidSendAccessibilityEvent:(const facebook::react::ShadowView &)shadowView
|
|
|
|
eventType:(const std::string &)eventType
|
|
|
|
{
|
|
|
|
ReactTag tag = shadowView.tag;
|
|
|
|
NSString *eventTypeStr = [[NSString alloc] initWithUTF8String:eventType.c_str()];
|
|
|
|
|
|
|
|
[self->_mountingManager sendAccessibilityEvent:tag eventType:eventTypeStr];
|
|
|
|
}
|
|
|
|
|
2021-02-05 00:13:01 +03:00
|
|
|
- (void)schedulerDidSetIsJSResponder:(BOOL)isJSResponder forShadowView:(facebook::react::ShadowView const &)shadowView;
|
|
|
|
{
|
|
|
|
[self->_mountingManager setIsJSResponder:isJSResponder forShadowView:shadowView];
|
|
|
|
}
|
|
|
|
|
2019-08-08 17:13:22 +03:00
|
|
|
- (void)addObserver:(id<RCTSurfacePresenterObserver>)observer
|
2019-03-08 04:34:41 +03:00
|
|
|
{
|
2019-03-26 19:52:27 +03:00
|
|
|
std::unique_lock<better::shared_mutex> lock(_observerListMutex);
|
2019-03-08 04:34:41 +03:00
|
|
|
[self->_observers addObject:observer];
|
|
|
|
}
|
|
|
|
|
2019-08-08 17:13:22 +03:00
|
|
|
- (void)removeObserver:(id<RCTSurfacePresenterObserver>)observer
|
2019-03-08 04:34:41 +03:00
|
|
|
{
|
2019-03-26 19:52:27 +03:00
|
|
|
std::unique_lock<better::shared_mutex> lock(_observerListMutex);
|
2019-03-08 04:34:41 +03:00
|
|
|
[self->_observers removeObject:observer];
|
|
|
|
}
|
|
|
|
|
2019-08-08 17:13:22 +03:00
|
|
|
#pragma mark - RCTMountingManagerDelegate
|
2018-09-26 20:01:42 +03:00
|
|
|
|
2019-08-08 17:13:22 +03:00
|
|
|
- (void)mountingManager:(RCTMountingManager *)mountingManager willMountComponentsWithRootTag:(ReactTag)rootTag
|
2018-09-26 20:01:42 +03:00
|
|
|
{
|
|
|
|
RCTAssertMainQueue();
|
|
|
|
|
2019-03-26 19:52:27 +03:00
|
|
|
std::shared_lock<better::shared_mutex> lock(_observerListMutex);
|
2019-08-08 17:13:22 +03:00
|
|
|
for (id<RCTSurfacePresenterObserver> observer in _observers) {
|
2019-03-08 04:34:41 +03:00
|
|
|
if ([observer respondsToSelector:@selector(willMountComponentsWithRootTag:)]) {
|
|
|
|
[observer willMountComponentsWithRootTag:rootTag];
|
|
|
|
}
|
|
|
|
}
|
2018-09-26 20:01:42 +03:00
|
|
|
}
|
2018-04-11 02:37:06 +03:00
|
|
|
|
2019-08-08 17:13:22 +03:00
|
|
|
- (void)mountingManager:(RCTMountingManager *)mountingManager didMountComponentsWithRootTag:(ReactTag)rootTag
|
2018-09-26 20:01:42 +03:00
|
|
|
{
|
|
|
|
RCTAssertMainQueue();
|
2018-04-11 02:37:06 +03:00
|
|
|
|
2019-03-26 19:52:27 +03:00
|
|
|
std::shared_lock<better::shared_mutex> lock(_observerListMutex);
|
2019-08-08 17:13:22 +03:00
|
|
|
for (id<RCTSurfacePresenterObserver> observer in _observers) {
|
2019-03-08 04:34:41 +03:00
|
|
|
if ([observer respondsToSelector:@selector(didMountComponentsWithRootTag:)]) {
|
|
|
|
[observer didMountComponentsWithRootTag:rootTag];
|
|
|
|
}
|
|
|
|
}
|
2018-04-11 02:37:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|