Move RCTSurfaceHostingComponent and RCTSurfaceBackedComponent (#38213)

Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/38213

Pull Request resolved: https://github.com/facebook/react-native/pull/38183

Changelog: [iOS][Removed] Remove RCTSurfaceHostingComponent and RCTSurfaceBackedComponent, needed only for ComponentKit integration.

Reviewed By: motiz88

Differential Revision: D47255482

fbshipit-source-id: 11db6f37a69a6f1e76ee7744a92f85c232342bd5
This commit is contained in:
Constantine Fry
2023-07-18 13:21:14 -07:00
committed by Facebook GitHub Bot
parent ff1972daba
commit b8d60a834f
12 changed files with 0 additions and 550 deletions
@@ -1,28 +0,0 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#import <ComponentKit/CKComponent.h>
#import <ComponentKit/CKCompositeComponent.h>
#import <RCTSurfaceHostingComponent/RCTSurfaceHostingComponentOptions.h>
#import <React/RCTSurfacePresenter.h>
@class RCTBridge;
/**
* ComponentKit component represents a React Native Surface created
* (and stored in the state) with given `bridge`, `moduleName`,
* and `properties`.
*/
@interface RCTSurfaceBackedComponent : CKCompositeComponent
+ (instancetype)newWithBridge:(RCTBridge *)bridge
surfacePresenter:(RCTSurfacePresenter *)surfacePresenter
moduleName:(NSString *)moduleName
properties:(NSDictionary *)properties
options:(RCTSurfaceHostingComponentOptions)options;
@end
@@ -1,76 +0,0 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#import "RCTSurfaceBackedComponent.h"
#import <UIKit/UIKit.h>
#import <ComponentKit/CKComponentSubclass.h>
#import <ComponentKit/CKOverlayLayoutComponent.h>
#import <RCTSurfaceHostingComponent/RCTSurfaceHostingComponent.h>
#import <React/RCTFabricSurface.h>
#import <React/RCTSurface.h>
#import "RCTSurfaceBackedComponentState.h"
@implementation RCTSurfaceBackedComponent
+ (id)initialState
{
return [RCTSurfaceBackedComponentState new];
}
+ (instancetype)newWithBridge:(RCTBridge *)bridge
surfacePresenter:(RCTSurfacePresenter *)surfacePresenter
moduleName:(NSString *)moduleName
properties:(NSDictionary *)properties
options:(RCTSurfaceHostingComponentOptions)options
{
CKComponentScope scope(self, moduleName);
RCTSurfaceBackedComponentState *state = scope.state();
// JavaScript entrypoints expects "fabric" key for Fabric surfaces
NSMutableDictionary *adjustedProperties = [[NSMutableDictionary alloc] initWithDictionary:properties];
adjustedProperties[@"fabric"] = surfacePresenter ? @YES : nil;
if (state.surface == nil || ![state.surface.moduleName isEqualToString:moduleName]) {
id<RCTSurfaceProtocol> surface;
if (surfacePresenter) {
surface = [[RCTFabricSurface alloc] initWithSurfacePresenter:surfacePresenter
moduleName:moduleName
initialProperties:adjustedProperties];
} else {
surface = [[RCTSurface alloc] initWithBridge:bridge moduleName:moduleName initialProperties:adjustedProperties];
}
[surface start];
state = [RCTSurfaceBackedComponentState newWithSurface:surface];
CKComponentScope::replaceState(scope, state);
} else {
if (![state.surface.properties isEqualToDictionary:adjustedProperties]) {
state.surface.properties = adjustedProperties;
}
}
RCTSurfaceHostingComponent *surfaceHostingComponent = [RCTSurfaceHostingComponent newWithSurface:state.surface
options:options];
CKComponent *component;
if (options.activityIndicatorComponentFactory == nil || RCTSurfaceStageIsRunning(state.surface.stage)) {
component = surfaceHostingComponent;
} else {
component = [[CKOverlayLayoutComponent alloc] initWithComponent:surfaceHostingComponent
overlay:options.activityIndicatorComponentFactory()
accessibilityContext:{}];
}
return [super newWithComponent:component];
}
@end
@@ -1,18 +0,0 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#import <UIKit/UIKit.h>
@protocol RCTSurfaceProtocol;
@interface RCTSurfaceBackedComponentState : NSObject
@property (atomic, readonly, strong) id<RCTSurfaceProtocol> surface;
+ (instancetype)newWithSurface:(id<RCTSurfaceProtocol>)surface;
@end
@@ -1,26 +0,0 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#import "RCTSurfaceBackedComponentState.h"
@implementation RCTSurfaceBackedComponentState
+ (instancetype)newWithSurface:(id<RCTSurfaceProtocol>)surface
{
return [[self alloc] initWithSurface:surface];
}
- (instancetype)initWithSurface:(id<RCTSurfaceProtocol>)surface
{
if (self = [super init]) {
_surface = surface;
}
return self;
}
@end
@@ -1,19 +0,0 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#import <RCTSurfaceHostingComponent/RCTSurfaceHostingComponent.h>
#import <RCTSurfaceHostingComponent/RCTSurfaceHostingComponentOptions.h>
@class RCTSurfaceHostingComponentState;
@interface RCTSurfaceHostingComponent ()
@property (nonatomic, strong, readonly) id<RCTSurfaceProtocol> surface;
@property (nonatomic, retain, readonly) RCTSurfaceHostingComponentState *state;
@property (nonatomic, assign, readonly) RCTSurfaceHostingComponentOptions options;
@end
@@ -1,19 +0,0 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#import <ComponentKit/CKComponent.h>
#import <RCTSurfaceHostingComponent/RCTSurfaceHostingComponentOptions.h>
#import <React/RCTSurfaceProtocol.h>
/**
* ComponentKit component represents given Surface instance.
*/
@interface RCTSurfaceHostingComponent : CKComponent
+ (instancetype)newWithSurface:(id<RCTSurfaceProtocol>)surface options:(RCTSurfaceHostingComponentOptions)options;
@end
@@ -1,116 +0,0 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#import "RCTSurfaceHostingComponent.h"
#import "RCTSurfaceHostingComponent+Internal.h"
#import <UIKit/UIKit.h>
#import <ComponentKit/CKComponentSubclass.h>
#import <React/RCTFabricSurface.h>
#import <React/RCTSurface.h>
#import <React/RCTSurfaceView.h>
#import "RCTSurfaceHostingComponentController.h"
#import "RCTSurfaceHostingComponentState.h"
@implementation RCTSurfaceHostingComponent
+ (Class<CKComponentControllerProtocol>)controllerClass
{
return [RCTSurfaceHostingComponentController class];
}
+ (id)initialState
{
return [RCTSurfaceHostingComponentState new];
}
+ (instancetype)newWithSurface:(id<RCTSurfaceProtocol>)surface options:(RCTSurfaceHostingComponentOptions)options
{
CKComponentScope scope(self, surface);
RCTSurfaceHostingComponentState *const state = scope.state();
RCTSurfaceHostingComponentState *const newState =
[RCTSurfaceHostingComponentState newWithStage:surface.stage intrinsicSize:surface.intrinsicSize];
if (![state isEqual:newState]) {
CKComponentScope::replaceState(scope, newState);
}
RCTSurfaceHostingComponent *const component = [super newWithView:{[UIView class]} size:{}];
if (component) {
component->_state = scope.state();
component->_surface = surface;
component->_options = options;
}
return component;
}
- (RCLayout)computeLayoutThatFits:(CKSizeRange)constrainedSize
{
// Optimistically communicating layout constraints to the `_surface`,
// just to provide layout constraints to React Native as early as possible.
// React Native *may* use this info later during applying the own state and
// related laying out in parallel with ComponentKit execution.
// This call will not interfere (or introduce any negative side effects) with
// following invocation of `sizeThatFitsMinimumSize:maximumSize:`.
// A weak point: We assume here that this particular layout will be
// mounted eventually, which is technically not guaranteed by ComponentKit.
// Therefore we also assume that the last layout calculated in a sequence
// will be mounted anyways, which is probably true for all *real* use cases.
// We plan to tackle this problem during the next big step in improving
// interop compatibilities of React Native which will enable us granularly
// control React Native mounting blocks and, as a result, implement
// truly synchronous mounting stage between React Native and ComponentKit.
[_surface setMinimumSize:constrainedSize.min maximumSize:constrainedSize.max];
// Just in case of the very first building pass, we give React Native a chance
// to prepare its internals for coming synchronous measuring.
if ([_surface isKindOfClass:[RCTSurface class]]) {
// Legacy Pre-Fabric Surface
[(RCTSurface *)_surface synchronouslyWaitForStage:RCTSurfaceStageSurfaceDidInitialLayout
timeout:_options.synchronousLayoutingTimeout];
} else if ([_surface isKindOfClass:[RCTFabricSurface class]]) {
// Fabric Surface
// Hack: Increase timeout because RCTFabricSurface stage will be RCTSurfaceStageSurfaceDidInitialLayout
// before mounting has finished, which can cause sizeThatFitsMinimumSize to return the wrong value.
// Safe hack because timeout length can be increased without making the component seem slower.
// However if timeout length is less than the time to mount a surface, the size may be incorrect.
// TODO (T115399546) Allow RCTFabricSurface synchronouslyWaitFor to wait for mounting completion stage
NSTimeInterval timeout = 20;
[(RCTFabricSurface *)_surface synchronouslyWaitFor:timeout];
}
CGSize fittingSize = CGSizeZero;
if (_surface.stage & RCTSurfaceStageSurfaceDidInitialLayout) {
fittingSize = [_surface sizeThatFitsMinimumSize:constrainedSize.min maximumSize:constrainedSize.max];
} else {
fittingSize = _options.activityIndicatorSize;
}
fittingSize = constrainedSize.clamp(fittingSize);
return {self, fittingSize};
}
- (CKComponentBoundsAnimation)boundsAnimationFromPreviousComponent:(RCTSurfaceHostingComponent *)previousComponent
{
if (_options.boundsAnimations && (previousComponent->_state.stage != _state.stage)) {
return {
.mode = CKComponentBoundsAnimationModeDefault,
.duration = 0.25,
.options = UIViewAnimationOptionCurveEaseInOut,
};
}
return {};
}
@end
@@ -1,12 +0,0 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#import <ComponentKit/CKComponentController.h>
@interface RCTSurfaceHostingComponentController : CKComponentController
@end
@@ -1,161 +0,0 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#import "RCTSurfaceHostingComponentController.h"
#import <ComponentKit/CKComponentSubclass.h>
#import <React/RCTAssert.h>
#import <React/RCTSurface.h>
#import <React/RCTSurfaceDelegate.h>
#import <React/RCTSurfaceView.h>
#import "RCTSurfaceHostingComponent+Internal.h"
#import "RCTSurfaceHostingComponent.h"
#import "RCTSurfaceHostingComponentState.h"
@interface RCTSurfaceHostingComponentController () <RCTSurfaceDelegate>
@end
@implementation RCTSurfaceHostingComponentController {
id<RCTSurfaceProtocol> _surface;
}
- (instancetype)initWithComponent:(RCTSurfaceHostingComponent *)component
{
if (self = [super initWithComponent:component]) {
[self updateSurfaceWithComponent:component];
}
return self;
}
#pragma mark - Lifecycle
- (void)didMount
{
[super didMount];
[self mountSurfaceView];
}
- (void)didRemount
{
[super didRemount];
[self mountSurfaceView];
}
- (void)didUpdateComponent
{
[super didUpdateComponent];
[self updateSurfaceWithComponent:(RCTSurfaceHostingComponent *)self.component];
}
- (void)didUnmount
{
[super didUnmount];
[self unmountSurfaceView];
}
#pragma mark - Helpers
- (void)updateSurfaceWithComponent:(RCTSurfaceHostingComponent *)component
{
// Updating `surface`
id<RCTSurfaceProtocol> const surface = component.surface;
if (surface != _surface) {
if (_surface.delegate == self) {
_surface.delegate = nil;
}
_surface = surface;
_surface.delegate = self;
}
}
- (void)setIntrinsicSize:(CGSize)intrinsicSize
{
__weak __typeof(self) weakSelf = self;
dispatch_async(dispatch_get_main_queue(), ^{
__strong __typeof(self) strongSelf = weakSelf;
if (!strongSelf) {
return;
}
[strongSelf.component
updateState:^(RCTSurfaceHostingComponentState *state) {
return [RCTSurfaceHostingComponentState newWithStage:state.stage intrinsicSize:intrinsicSize];
}
mode:[strongSelf suitableStateUpdateMode]];
});
}
- (void)setStage:(RCTSurfaceStage)stage
{
__weak __typeof(self) weakSelf = self;
dispatch_async(dispatch_get_main_queue(), ^{
__strong __typeof(self) strongSelf = weakSelf;
if (!strongSelf) {
return;
}
[strongSelf.component
updateState:^(RCTSurfaceHostingComponentState *state) {
return [RCTSurfaceHostingComponentState newWithStage:stage intrinsicSize:state.intrinsicSize];
}
mode:[strongSelf suitableStateUpdateMode]];
});
}
- (CKUpdateMode)suitableStateUpdateMode
{
return ((RCTSurfaceHostingComponent *)self.component).options.synchronousStateUpdates && RCTIsMainQueue()
? CKUpdateModeSynchronous
: CKUpdateModeAsynchronous;
}
- (void)mountSurfaceView
{
UIView *const surfaceView = _surface.view;
const CKComponentViewContext &context = [[self component] viewContext];
UIView *const superview = context.view;
superview.clipsToBounds = YES;
RCTAssert([superview.subviews count] <= 1, @"Should never have more than a single stateful subview.");
UIView *const existingSurfaceView = [superview.subviews lastObject];
if (existingSurfaceView != surfaceView) {
[existingSurfaceView removeFromSuperview];
surfaceView.frame = superview.bounds;
surfaceView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[superview addSubview:surfaceView];
}
}
- (void)unmountSurfaceView
{
const CKComponentViewContext &context = [[self component] viewContext];
UIView *const superview = context.view;
RCTAssert([superview.subviews count] <= 1, @"Should never have more than a single stateful subview.");
UIView *const existingSurfaceView = [superview.subviews lastObject];
[existingSurfaceView removeFromSuperview];
}
#pragma mark - RCTSurfaceDelegate
- (void)surface:(RCTSurface *)surface didChangeIntrinsicSize:(CGSize)intrinsicSize
{
[self setIntrinsicSize:intrinsicSize];
}
- (void)surface:(RCTSurface *)surface didChangeStage:(RCTSurfaceStage)stage
{
[self setStage:stage];
}
@end
@@ -1,20 +0,0 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#import <UIKit/UIKit.h>
#import <ComponentKit/CKComponent.h>
typedef CKComponent * (^RCTSurfaceHostingComponentOptionsActivityIndicatorComponentFactory)();
struct RCTSurfaceHostingComponentOptions {
NSTimeInterval synchronousLayoutingTimeout = 0.350;
BOOL synchronousStateUpdates = YES;
CGSize activityIndicatorSize = {44.0, 44.0};
BOOL boundsAnimations = YES;
RCTSurfaceHostingComponentOptionsActivityIndicatorComponentFactory activityIndicatorComponentFactory = nil;
};
@@ -1,19 +0,0 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#import <UIKit/UIKit.h>
#import <React/RCTSurfaceStage.h>
@interface RCTSurfaceHostingComponentState : NSObject
@property (nonatomic, readonly, assign) CGSize intrinsicSize;
@property (nonatomic, readonly, assign) RCTSurfaceStage stage;
+ (instancetype)newWithStage:(RCTSurfaceStage)stage intrinsicSize:(CGSize)intrinsicSize;
@end
@@ -1,36 +0,0 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#import "RCTSurfaceHostingComponentState.h"
@implementation RCTSurfaceHostingComponentState
+ (instancetype)newWithStage:(RCTSurfaceStage)stage intrinsicSize:(CGSize)intrinsicSize
{
return [[self alloc] initWithStage:stage intrinsicSize:intrinsicSize];
}
- (instancetype)initWithStage:(RCTSurfaceStage)stage intrinsicSize:(CGSize)intrinsicSize
{
if (self = [super init]) {
_stage = stage;
_intrinsicSize = intrinsicSize;
}
return self;
}
- (BOOL)isEqual:(RCTSurfaceHostingComponentState *)other
{
if (other == self) {
return YES;
}
return _stage == other->_stage && CGSizeEqualToSize(_intrinsicSize, other->_intrinsicSize);
}
@end