From 1064dad9bf95a21920b19039cc22579ea01198fa Mon Sep 17 00:00:00 2001 From: Valentin Shergin Date: Tue, 22 May 2018 15:48:21 -0700 Subject: [PATCH] Fabric: Support of updating EventHandlers on mounting layer Summary: Pretty trivial; new type of mount item & new method in the component protocol. The default implementation in `UIView+ComponentViewProtocol` does nothing. Reviewed By: fkgozali Differential Revision: D8053355 fbshipit-source-id: a0418edf17ca75c4b94942b04acd93f3ea5d27e0 --- .../RCTUpdateEventHandlersMountItem.h | 26 +++++++++++++ .../RCTUpdateEventHandlersMountItem.mm | 37 +++++++++++++++++++ .../Mounting/RCTComponentViewProtocol.h | 7 ++++ React/Fabric/Mounting/RCTMountingManager.mm | 28 +++++++++++--- .../Mounting/UIView+ComponentViewProtocol.h | 3 ++ .../Mounting/UIView+ComponentViewProtocol.mm | 5 +++ 6 files changed, 100 insertions(+), 6 deletions(-) create mode 100644 React/Fabric/Mounting/MountItems/RCTUpdateEventHandlersMountItem.h create mode 100644 React/Fabric/Mounting/MountItems/RCTUpdateEventHandlersMountItem.mm diff --git a/React/Fabric/Mounting/MountItems/RCTUpdateEventHandlersMountItem.h b/React/Fabric/Mounting/MountItems/RCTUpdateEventHandlersMountItem.h new file mode 100644 index 000000000..dd11971b8 --- /dev/null +++ b/React/Fabric/Mounting/MountItems/RCTUpdateEventHandlersMountItem.h @@ -0,0 +1,26 @@ +/** + * Copyright (c) 2015-present, Facebook, Inc. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +#import + +#import +#import +#import + +NS_ASSUME_NONNULL_BEGIN + +/** + * Updates event handlers of a component view. + */ +@interface RCTUpdateEventHandlersMountItem : NSObject + +- (instancetype)initWithTag:(ReactTag)tag + eventHandlers:(facebook::react::SharedEventHandlers)eventHandlers; + +@end + +NS_ASSUME_NONNULL_END diff --git a/React/Fabric/Mounting/MountItems/RCTUpdateEventHandlersMountItem.mm b/React/Fabric/Mounting/MountItems/RCTUpdateEventHandlersMountItem.mm new file mode 100644 index 000000000..cc3066b05 --- /dev/null +++ b/React/Fabric/Mounting/MountItems/RCTUpdateEventHandlersMountItem.mm @@ -0,0 +1,37 @@ +/** + * Copyright (c) 2015-present, Facebook, Inc. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +#import "RCTUpdateEventHandlersMountItem.h" + +#import "RCTComponentViewRegistry.h" + +using namespace facebook::react; + +@implementation RCTUpdateEventHandlersMountItem { + ReactTag _tag; + SharedEventHandlers _eventHandlers; +} + +- (instancetype)initWithTag:(ReactTag)tag + eventHandlers:(SharedEventHandlers)eventHandlers +{ + if (self = [super init]) { + _tag = tag; + _eventHandlers = eventHandlers; + } + + return self; +} + +- (void)executeWithRegistry:(RCTComponentViewRegistry *)registry +{ + UIView *componentView = [registry componentViewByTag:_tag]; + + [componentView updateEventHandlers:_eventHandlers]; +} + +@end diff --git a/React/Fabric/Mounting/RCTComponentViewProtocol.h b/React/Fabric/Mounting/RCTComponentViewProtocol.h index fc56aa7e0..47048efa1 100644 --- a/React/Fabric/Mounting/RCTComponentViewProtocol.h +++ b/React/Fabric/Mounting/RCTComponentViewProtocol.h @@ -7,6 +7,7 @@ #import +#import #import #import #import @@ -52,6 +53,12 @@ NS_ASSUME_NONNULL_BEGIN - (void)updateLocalData:(facebook::react::SharedLocalData)localData oldLocalData:(facebook::react::SharedLocalData)oldLocalData; +/* + * Called for updating component's event handlers set. + * Receiver must cache `eventHandlers` object inside and use it for emitting + * events when needed. + */ +- (void)updateEventHandlers:(facebook::react::SharedEventHandlers)eventHandlers; /* * Called for updating component's layout metrics. diff --git a/React/Fabric/Mounting/RCTMountingManager.mm b/React/Fabric/Mounting/RCTMountingManager.mm index 5b7e7f131..93e8f0ea8 100644 --- a/React/Fabric/Mounting/RCTMountingManager.mm +++ b/React/Fabric/Mounting/RCTMountingManager.mm @@ -20,6 +20,7 @@ #import "RCTInsertMountItem.h" #import "RCTRemoveMountItem.h" #import "RCTUpdatePropsMountItem.h" +#import "RCTUpdateEventHandlersMountItem.h" #import "RCTUpdateLocalDataMountItem.h" #import "RCTUpdateLayoutMetricsMountItem.h" @@ -53,6 +54,7 @@ using namespace facebook::react; [mountItems addObject:mountItem]; break; } + case TreeMutationInstruction::Deletion: { NSString *componentName = [NSString stringWithCString:instruction.getOldChildNode()->getComponentName().c_str() encoding:NSASCIIStringEncoding]; @@ -64,17 +66,15 @@ using namespace facebook::react; } case TreeMutationInstruction::Insertion: { - RCTInsertMountItem *mountItem = - [[RCTInsertMountItem alloc] initWithChildTag:instruction.getNewChildNode()->getTag() - parentTag:instruction.getParentNode()->getTag() - index:instruction.getIndex()]; - [mountItems addObject:mountItem]; - // Props [mountItems addObject:[[RCTUpdatePropsMountItem alloc] initWithTag:instruction.getNewChildNode()->getTag() oldProps:nullptr newProps:instruction.getNewChildNode()->getProps()]]; + // EventHandlers + [mountItems addObject:[[RCTUpdateEventHandlersMountItem alloc] initWithTag:instruction.getNewChildNode()->getTag() + eventHandlers:instruction.getNewChildNode()->getEventHandlers()]]; + // LocalData if (instruction.getNewChildNode()->getLocalData()) { [mountItems addObject:[[RCTUpdateLocalDataMountItem alloc] initWithTag:instruction.getNewChildNode()->getTag() @@ -91,6 +91,14 @@ using namespace facebook::react; oldLayoutMetrics:{} newLayoutMetrics:layoutableNewShadowNode->getLayoutMetrics()]]; } + + // Insertion + RCTInsertMountItem *mountItem = + [[RCTInsertMountItem alloc] initWithChildTag:instruction.getNewChildNode()->getTag() + parentTag:instruction.getParentNode()->getTag() + index:instruction.getIndex()]; + [mountItems addObject:mountItem]; + break; } @@ -116,6 +124,14 @@ using namespace facebook::react; [mountItems addObject:mountItem]; } + // EventHandlers + if (oldShadowNode->getEventHandlers() != newShadowNode->getEventHandlers()) { + RCTUpdateEventHandlersMountItem *mountItem = + [[RCTUpdateEventHandlersMountItem alloc] initWithTag:instruction.getOldChildNode()->getTag() + eventHandlers:instruction.getOldChildNode()->getEventHandlers()]; + [mountItems addObject:mountItem]; + } + // LocalData if (oldShadowNode->getLocalData() != newShadowNode->getLocalData()) { RCTUpdateLocalDataMountItem *mountItem = diff --git a/React/Fabric/Mounting/UIView+ComponentViewProtocol.h b/React/Fabric/Mounting/UIView+ComponentViewProtocol.h index cffe51a2f..75524cef7 100644 --- a/React/Fabric/Mounting/UIView+ComponentViewProtocol.h +++ b/React/Fabric/Mounting/UIView+ComponentViewProtocol.h @@ -9,6 +9,7 @@ #import + NS_ASSUME_NONNULL_BEGIN /** @@ -25,6 +26,8 @@ NS_ASSUME_NONNULL_BEGIN - (void)updateProps:(facebook::react::SharedProps)props oldProps:(facebook::react::SharedProps)oldProps; +- (void)updateEventHandlers:(facebook::react::SharedEventHandlers)eventHandlers; + - (void)updateLocalData:(facebook::react::SharedLocalData)localData oldLocalData:(facebook::react::SharedLocalData)oldLocalData; diff --git a/React/Fabric/Mounting/UIView+ComponentViewProtocol.mm b/React/Fabric/Mounting/UIView+ComponentViewProtocol.mm index 5627a699d..223bb7c9b 100644 --- a/React/Fabric/Mounting/UIView+ComponentViewProtocol.mm +++ b/React/Fabric/Mounting/UIView+ComponentViewProtocol.mm @@ -31,6 +31,11 @@ // Default implementation does nothing. } +- (void)updateEventHandlers:(facebook::react::SharedEventHandlers)eventHandlers +{ + // Default implementation does nothing. +} + - (void)updateLocalData:(facebook::react::SharedLocalData)localData oldLocalData:(facebook::react::SharedLocalData)oldLocalData {