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"
|
|
|
|
|
|
|
|
#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>
|
|
|
|
#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-05-09 05:56:27 +00:00
|
|
|
#import <fabric/core/LayoutContext.h>
|
|
|
|
#import <fabric/core/LayoutConstraints.h>
|
|
|
|
|
|
|
|
#import "RCTConversions.h"
|
|
|
|
|
2018-04-10 23:37:06 +00:00
|
|
|
using namespace facebook::react;
|
|
|
|
|
|
|
|
@interface RCTSurfacePresenter () <RCTSchedulerDelegate, RCTMountingManagerDelegate>
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation RCTSurfacePresenter {
|
|
|
|
RCTScheduler *_scheduler;
|
|
|
|
RCTMountingManager *_mountingManager;
|
|
|
|
RCTBridge *_bridge;
|
|
|
|
RCTBridge *_batchedBridge;
|
|
|
|
RCTSurfaceRegistry *_surfaceRegistry;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (instancetype)initWithBridge:(RCTBridge *)bridge
|
|
|
|
{
|
|
|
|
if (self = [super init]) {
|
|
|
|
_bridge = bridge;
|
|
|
|
_batchedBridge = [_bridge batchedBridge] ?: _bridge;
|
|
|
|
|
|
|
|
_scheduler = [[RCTScheduler alloc] init];
|
|
|
|
_scheduler.delegate = self;
|
|
|
|
|
|
|
|
_surfaceRegistry = [[RCTSurfaceRegistry alloc] init];
|
|
|
|
_mountingManager = [[RCTMountingManager alloc] init];
|
|
|
|
_mountingManager.delegate = self;
|
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-06-30 02:57:43 +00:00
|
|
|
#pragma mark - RCTSchedulerDelegate
|
|
|
|
|
2018-09-04 05:53:18 +00:00
|
|
|
- (void)schedulerDidFinishTransaction:(facebook::react::ShadowViewMutationList)mutations
|
2018-06-30 02:57:43 +00:00
|
|
|
rootTag:(ReactTag)rootTag
|
2018-04-10 23:37:06 +00:00
|
|
|
{
|
2018-09-04 05:53:18 +00:00
|
|
|
[_mountingManager performTransactionWithMutations:mutations
|
|
|
|
rootTag:rootTag];
|
2018-04-10 23:37:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)schedulerDidRequestPreliminaryViewAllocationWithComponentName:(NSString *)componentName
|
|
|
|
{
|
2018-06-30 02:57:43 +00:00
|
|
|
[_mountingManager preliminaryCreateComponentViewWithName:componentName];
|
2018-04-10 23:37:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark - Internal Surface-dedicated Interface
|
|
|
|
|
|
|
|
- (void)registerSurface:(RCTFabricSurface *)surface
|
|
|
|
{
|
|
|
|
[_surfaceRegistry registerSurface:surface];
|
2018-06-22 14:28:27 +00:00
|
|
|
[_scheduler registerRootTag:surface.rootTag];
|
2018-04-10 23:37:06 +00:00
|
|
|
[self runSurface:surface];
|
|
|
|
|
2018-09-04 05:53:18 +00:00
|
|
|
// FIXME: mutation MUST produce instruction for root node.
|
2018-06-22 14:28:27 +00:00
|
|
|
[_mountingManager.componentViewRegistry dequeueComponentViewWithName:@"Root" tag:surface.rootTag];
|
2018-04-10 23:37:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)unregisterSurface:(RCTFabricSurface *)surface
|
|
|
|
{
|
|
|
|
[self stopSurface:surface];
|
2018-06-22 14:28:27 +00:00
|
|
|
[_scheduler unregisterRootTag:surface.rootTag];
|
2018-04-10 23:37:06 +00:00
|
|
|
[_surfaceRegistry unregisterSurface:surface];
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
{
|
|
|
|
LayoutContext layoutContext;
|
2018-06-22 14:28:29 +00:00
|
|
|
layoutContext.pointScaleFactor = RCTScreenScale();
|
2018-05-09 05:56:27 +00:00
|
|
|
LayoutConstraints layoutConstraints = {};
|
|
|
|
layoutConstraints.minimumSize = RCTSizeFromCGSize(minimumSize);
|
|
|
|
layoutConstraints.maximumSize = RCTSizeFromCGSize(maximumSize);
|
|
|
|
|
|
|
|
return [_scheduler measureWithLayoutConstraints:layoutConstraints
|
|
|
|
layoutContext:layoutContext
|
|
|
|
rootTag:surface.rootTag];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setMinimumSize:(CGSize)minimumSize
|
|
|
|
maximumSize:(CGSize)maximumSize
|
|
|
|
surface:(RCTFabricSurface *)surface
|
|
|
|
{
|
|
|
|
LayoutContext layoutContext;
|
2018-06-22 14:28:29 +00:00
|
|
|
layoutContext.pointScaleFactor = RCTScreenScale();
|
2018-05-09 05:56:27 +00:00
|
|
|
LayoutConstraints layoutConstraints = {};
|
|
|
|
layoutConstraints.minimumSize = RCTSizeFromCGSize(minimumSize);
|
|
|
|
layoutConstraints.maximumSize = RCTSizeFromCGSize(maximumSize);
|
|
|
|
|
|
|
|
[_scheduler constraintLayoutWithLayoutConstraints:layoutConstraints
|
|
|
|
layoutContext:layoutContext
|
|
|
|
rootTag:surface.rootTag];
|
|
|
|
}
|
|
|
|
|
2018-04-10 23:37:06 +00:00
|
|
|
- (void)runSurface:(RCTFabricSurface *)surface
|
|
|
|
{
|
|
|
|
NSDictionary *applicationParameters = @{
|
2018-06-22 14:28:27 +00:00
|
|
|
@"rootTag": @(surface.rootTag),
|
2018-04-10 23:37:06 +00:00
|
|
|
@"initialProps": surface.properties,
|
|
|
|
};
|
|
|
|
|
|
|
|
[_batchedBridge enqueueJSCall:@"AppRegistry" method:@"runApplication" args:@[surface.moduleName, applicationParameters] completion:NULL];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)stopSurface:(RCTFabricSurface *)surface
|
|
|
|
{
|
2018-09-08 06:38:53 +00:00
|
|
|
[_batchedBridge enqueueJSCall:@"ReactFabric" method:@"unmountComponentAtNode" args:@[@(surface.rootTag)] completion:NULL];
|
2018-04-10 23:37:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark - RCTMountingManagerDelegate
|
|
|
|
|
|
|
|
- (void)mountingManager:(RCTMountingManager *)mountingManager willMountComponentsWithRootTag:(ReactTag)rootTag
|
|
|
|
{
|
|
|
|
RCTIsMainQueue();
|
|
|
|
// TODO: Propagate state change to Surface.
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)mountingManager:(RCTMountingManager *)mountingManager didMountComponentsWithRootTag:(ReactTag)rootTag
|
|
|
|
{
|
|
|
|
RCTIsMainQueue();
|
|
|
|
RCTFabricSurface *surface = [_surfaceRegistry surfaceForRootTag:rootTag];
|
|
|
|
|
|
|
|
// FIXME: Implement proper state propagation mechanism.
|
|
|
|
[surface _setStage:RCTSurfaceStageSurfaceDidInitialRendering];
|
|
|
|
[surface _setStage:RCTSurfaceStageSurfaceDidInitialLayout];
|
|
|
|
[surface _setStage:RCTSurfaceStageSurfaceDidInitialMounting];
|
|
|
|
|
|
|
|
UIView *rootComponentView = [_mountingManager.componentViewRegistry componentViewByTag:rootTag];
|
|
|
|
|
|
|
|
surface.view.rootView = (RCTSurfaceRootView *)rootComponentView;
|
|
|
|
}
|
|
|
|
|
2018-06-15 17:56:40 +00:00
|
|
|
#pragma mark - Bridge events
|
|
|
|
|
|
|
|
- (void)handleBridgeWillReloadNotification:(NSNotification *)notification
|
|
|
|
{
|
|
|
|
// TODO: Define a lifecycle contract for the pieces involved here including the scheduler, mounting manager, and
|
|
|
|
// the surface registry. For now simply recreate the scheduler on reload.
|
|
|
|
// The goal is to deallocate the Scheduler and its underlying references before the JS runtime is destroyed.
|
|
|
|
_scheduler = [[RCTScheduler alloc] init];
|
|
|
|
_scheduler.delegate = self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)handleJavaScriptDidLoadNotification:(NSNotification *)notification
|
|
|
|
{
|
|
|
|
RCTBridge *bridge = notification.userInfo[@"bridge"];
|
|
|
|
if (bridge != _batchedBridge) {
|
|
|
|
_batchedBridge = bridge;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-10 23:37:06 +00:00
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation RCTSurfacePresenter (Deprecated)
|
|
|
|
|
|
|
|
- (std::shared_ptr<FabricUIManager>)uiManager_DO_NOT_USE
|
|
|
|
{
|
|
|
|
return _scheduler.uiManager_DO_NOT_USE;
|
|
|
|
}
|
|
|
|
|
2018-08-01 22:23:06 +00:00
|
|
|
- (RCTBridge *)bridge_DO_NOT_USE
|
|
|
|
{
|
|
|
|
return _bridge;
|
|
|
|
}
|
|
|
|
|
2018-04-10 23:37:06 +00:00
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation RCTBridge (RCTSurfacePresenter)
|
|
|
|
|
|
|
|
- (RCTSurfacePresenter *)surfacePresenter
|
|
|
|
{
|
|
|
|
return [self jsBoundExtraModuleForClass:[RCTSurfacePresenter class]];
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|