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
This commit is contained in:
Valentin Shergin
2018-05-22 15:48:21 -07:00
committed by Facebook Github Bot
parent 2a3025da97
commit 1064dad9bf
6 changed files with 100 additions and 6 deletions

View File

@@ -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 <UIKit/UIKit.h>
#import <React/RCTMountItemProtocol.h>
#import <React/RCTPrimitives.h>
#import <fabric/core/EventHandlers.h>
NS_ASSUME_NONNULL_BEGIN
/**
* Updates event handlers of a component view.
*/
@interface RCTUpdateEventHandlersMountItem : NSObject <RCTMountItemProtocol>
- (instancetype)initWithTag:(ReactTag)tag
eventHandlers:(facebook::react::SharedEventHandlers)eventHandlers;
@end
NS_ASSUME_NONNULL_END

View File

@@ -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<RCTComponentViewProtocol> *componentView = [registry componentViewByTag:_tag];
[componentView updateEventHandlers:_eventHandlers];
}
@end

View File

@@ -7,6 +7,7 @@
#import <UIKit/UIKit.h>
#import <fabric/core/EventHandlers.h>
#import <fabric/core/LocalData.h>
#import <fabric/core/Props.h>
#import <fabric/core/LayoutMetrics.h>
@@ -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.

View File

@@ -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 =

View File

@@ -9,6 +9,7 @@
#import <React/RCTComponentViewProtocol.h>
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;

View File

@@ -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
{