diff --git a/React/Fabric/Mounting/ComponentViews/LegacyViewManagerInterop/RCTLegacyViewManagerInteropComponentView.mm b/React/Fabric/Mounting/ComponentViews/LegacyViewManagerInterop/RCTLegacyViewManagerInteropComponentView.mm index e03cd552627..6a91d86e8ef 100644 --- a/React/Fabric/Mounting/ComponentViews/LegacyViewManagerInterop/RCTLegacyViewManagerInteropComponentView.mm +++ b/React/Fabric/Mounting/ComponentViews/LegacyViewManagerInterop/RCTLegacyViewManagerInteropComponentView.mm @@ -10,15 +10,15 @@ #import #import #import -#import #import +#import "RCTLegacyViewManagerInteropCoordinatorAdapter.h" using namespace facebook::react; @implementation RCTLegacyViewManagerInteropComponentView { - UIView *_paperView; NSMutableDictionary *_viewsToBeMounted; NSMutableArray *_viewsToBeUnmounted; + RCTLegacyViewManagerInteropCoordinatorAdapter *_adapter; LegacyViewManagerInteropShadowNode::ConcreteState::Shared _state; } @@ -34,12 +34,6 @@ using namespace facebook::react; return self; } -- (void)setTag:(NSInteger)tag -{ - [self.coordinator removeObserveForTag:self.tag]; - [super setTag:tag]; -} - + (NSMutableSet *)supportedViewManagers { static NSMutableSet *supported = @@ -78,10 +72,9 @@ using namespace facebook::react; - (void)prepareForRecycle { + _adapter = nil; [_viewsToBeMounted removeAllObjects]; [_viewsToBeUnmounted removeAllObjects]; - [_paperView removeFromSuperview]; - _paperView = nil; _state.reset(); [super prepareForRecycle]; } @@ -110,41 +103,38 @@ using namespace facebook::react; { [super finalizeUpdates:updateMask]; - if (!_paperView) { + if (!_adapter) { + _adapter = [[RCTLegacyViewManagerInteropCoordinatorAdapter alloc] initWithCoordinator:self.coordinator + reactTag:self.tag]; __weak __typeof(self) weakSelf = self; - _paperView = self.coordinator.paperView; - [self.coordinator addObserveForTag:self.tag - usingBlock:^(std::string eventName, folly::dynamic event) { - if (weakSelf) { - __typeof(self) strongSelf = weakSelf; - auto eventEmitter = - std::static_pointer_cast( - strongSelf->_eventEmitter); - eventEmitter->dispatchEvent(eventName, event); - } - }]; - - _paperView.reactTag = [NSNumber numberWithInteger:self.tag]; - self.contentView = _paperView; + _adapter.eventInterceptor = ^(std::string eventName, folly::dynamic event) { + if (weakSelf) { + __typeof(self) strongSelf = weakSelf; + auto eventEmitter = + std::static_pointer_cast(strongSelf->_eventEmitter); + eventEmitter->dispatchEvent(eventName, event); + } + }; + self.contentView = _adapter.paperView; } for (NSNumber *key in _viewsToBeMounted) { - [_paperView insertReactSubview:_viewsToBeMounted[key] atIndex:key.integerValue]; + [_adapter.paperView insertReactSubview:_viewsToBeMounted[key] atIndex:key.integerValue]; } [_viewsToBeMounted removeAllObjects]; for (UIView *view in _viewsToBeUnmounted) { - [_paperView removeReactSubview:view]; + [_adapter.paperView removeReactSubview:view]; } [_viewsToBeUnmounted removeAllObjects]; - [_paperView didUpdateReactSubviews]; + [_adapter.paperView didUpdateReactSubviews]; if (updateMask & RNComponentViewUpdateMaskProps) { const auto &newProps = *std::static_pointer_cast(_props); - [self.coordinator setProps:newProps.otherProps forView:_paperView]; + [_adapter setProps:newProps.otherProps]; } } diff --git a/React/Fabric/Mounting/ComponentViews/LegacyViewManagerInterop/RCTLegacyViewManagerInteropCoordinatorAdapter.h b/React/Fabric/Mounting/ComponentViews/LegacyViewManagerInterop/RCTLegacyViewManagerInteropCoordinatorAdapter.h new file mode 100644 index 00000000000..941dbc4be29 --- /dev/null +++ b/React/Fabric/Mounting/ComponentViews/LegacyViewManagerInterop/RCTLegacyViewManagerInteropCoordinatorAdapter.h @@ -0,0 +1,25 @@ +/* + * Copyright (c) Facebook, Inc. and its affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +#import +#import + +NS_ASSUME_NONNULL_BEGIN + +@interface RCTLegacyViewManagerInteropCoordinatorAdapter : NSObject + +- (instancetype)initWithCoordinator:(RCTLegacyViewManagerInteropCoordinator *)coordinator reactTag:(NSInteger)tag; + +@property (strong, nonatomic) UIView *paperView; + +@property (nonatomic, copy, nullable) void (^eventInterceptor)(std::string eventName, folly::dynamic event); + +- (void)setProps:(folly::dynamic const &)props; + +@end + +NS_ASSUME_NONNULL_END diff --git a/React/Fabric/Mounting/ComponentViews/LegacyViewManagerInterop/RCTLegacyViewManagerInteropCoordinatorAdapter.mm b/React/Fabric/Mounting/ComponentViews/LegacyViewManagerInterop/RCTLegacyViewManagerInteropCoordinatorAdapter.mm new file mode 100644 index 00000000000..08785668418 --- /dev/null +++ b/React/Fabric/Mounting/ComponentViews/LegacyViewManagerInterop/RCTLegacyViewManagerInteropCoordinatorAdapter.mm @@ -0,0 +1,53 @@ +/* + * Copyright (c) Facebook, Inc. and its affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +#import "RCTLegacyViewManagerInteropCoordinatorAdapter.h" +#import + +@implementation RCTLegacyViewManagerInteropCoordinatorAdapter { + RCTLegacyViewManagerInteropCoordinator *_coordinator; + NSInteger _tag; +} + +- (instancetype)initWithCoordinator:(RCTLegacyViewManagerInteropCoordinator *)coordinator reactTag:(NSInteger)tag +{ + if (self = [super init]) { + _coordinator = coordinator; + _tag = tag; + } + return self; +} + +- (void)dealloc +{ + [_paperView removeFromSuperview]; + [_coordinator removeObserveForTag:_tag]; +} + +- (UIView *)paperView +{ + if (!_paperView) { + _paperView = _coordinator.paperView; + _paperView.reactTag = [NSNumber numberWithInteger:_tag]; + + __weak __typeof(self) weakSelf = self; + [_coordinator addObserveForTag:_tag + usingBlock:^(std::string eventName, folly::dynamic event) { + if (weakSelf.eventInterceptor) { + weakSelf.eventInterceptor(eventName, event); + } + }]; + } + return _paperView; +} + +- (void)setProps:(const folly::dynamic &)props +{ + [_coordinator setProps:props forView:self.paperView]; +} + +@end