Skip to content

Commit

Permalink
Fabric: State-related mounting logic for iOS
Browse files Browse the repository at this point in the history
Summary: The changes allows to get the State object on mounting layer and initiate the updates.

Reviewed By: mdvacca

Differential Revision: D14217185

fbshipit-source-id: 370644e06e350932e93c39adbe46544b071c28b3
  • Loading branch information
shergin authored and facebook-github-bot committed Feb 27, 2019
1 parent 802534e commit 9207377
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 0 deletions.
27 changes: 27 additions & 0 deletions React/Fabric/Mounting/MountItems/RCTUpdateStateMountItem.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* 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 <UIKit/UIKit.h>

#import <React/RCTMountItemProtocol.h>
#import <React/RCTPrimitives.h>
#import <react/core/State.h>

NS_ASSUME_NONNULL_BEGIN

/**
* Updates state of a component view.
*/
@interface RCTUpdateStateMountItem : NSObject <RCTMountItemProtocol>

- (instancetype)initWithTag:(ReactTag)tag
oldState:(facebook::react::State::Shared)oldState
newState:(facebook::react::State::Shared)newState;

@end

NS_ASSUME_NONNULL_END
39 changes: 39 additions & 0 deletions React/Fabric/Mounting/MountItems/RCTUpdateStateMountItem.mm
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* 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 "RCTUpdateStateMountItem.h"

#import "RCTComponentViewRegistry.h"

using namespace facebook::react;

@implementation RCTUpdateStateMountItem {
ReactTag _tag;
State::Shared _oldState;
State::Shared _newState;
}

- (instancetype)initWithTag:(ReactTag)tag
oldState:(facebook::react::State::Shared)oldState
newState:(facebook::react::State::Shared)newState
{
if (self = [super init]) {
_tag = tag;
_oldState = oldState;
_newState = newState;
}

return self;
}

- (void)executeWithRegistry:(RCTComponentViewRegistry *)registry
{
UIView<RCTComponentViewProtocol> *componentView = [registry componentViewByTag:_tag];
[componentView updateState:_newState oldState:_oldState];
}

@end
7 changes: 7 additions & 0 deletions React/Fabric/Mounting/RCTComponentViewProtocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#import <react/core/LayoutMetrics.h>
#import <react/core/LocalData.h>
#import <react/core/Props.h>
#import <react/core/State.h>

NS_ASSUME_NONNULL_BEGIN

Expand Down Expand Up @@ -56,6 +57,12 @@ NS_ASSUME_NONNULL_BEGIN
- (void)updateLocalData:(facebook::react::SharedLocalData)localData
oldLocalData:(facebook::react::SharedLocalData)oldLocalData;

/*
* Called for updating component's state.
* Receiver must update native view according to changed state.
*/
- (void)updateState:(facebook::react::State::Shared)state oldState:(facebook::react::State::Shared)oldState;

/*
* Called for updating component's event handlers set.
* Receiver must cache `eventEmitter` object inside and use it for emitting
Expand Down
16 changes: 16 additions & 0 deletions React/Fabric/Mounting/RCTMountingManager.mm
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#import "RCTUpdateLayoutMetricsMountItem.h"
#import "RCTUpdateLocalDataMountItem.h"
#import "RCTUpdatePropsMountItem.h"
#import "RCTUpdateStateMountItem.h"

using namespace facebook::react;

Expand Down Expand Up @@ -87,6 +88,13 @@ - (void)performTransactionWithMutations:(facebook::react::ShadowViewMutationList
newLocalData:mutation.newChildShadowView.localData]];
}

// State
if (mutation.newChildShadowView.state) {
[mountItems addObject:[[RCTUpdateStateMountItem alloc] initWithTag:mutation.newChildShadowView.tag
oldState:nullptr
newState:mutation.newChildShadowView.state]];
}

// Layout
if (mutation.newChildShadowView.layoutMetrics != EmptyLayoutMetrics) {
[mountItems addObject:[[RCTUpdateLayoutMetricsMountItem alloc]
Expand Down Expand Up @@ -142,6 +150,14 @@ - (void)performTransactionWithMutations:(facebook::react::ShadowViewMutationList
[mountItems addObject:mountItem];
}

// State
if (oldChildShadowView.state != newChildShadowView.state) {
RCTUpdateStateMountItem *mountItem = [[RCTUpdateStateMountItem alloc] initWithTag:newChildShadowView.tag
oldState:oldChildShadowView.state
newState:newChildShadowView.state];
[mountItems addObject:mountItem];
}

// Layout
if (oldChildShadowView.layoutMetrics != newChildShadowView.layoutMetrics) {
RCTUpdateLayoutMetricsMountItem *mountItem =
Expand Down
2 changes: 2 additions & 0 deletions React/Fabric/Mounting/UIView+ComponentViewProtocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ NS_ASSUME_NONNULL_BEGIN
- (void)updateLocalData:(facebook::react::SharedLocalData)localData
oldLocalData:(facebook::react::SharedLocalData)oldLocalData;

- (void)updateState:(facebook::react::State::Shared)state oldState:(facebook::react::State::Shared)oldState;

- (void)updateLayoutMetrics:(facebook::react::LayoutMetrics)layoutMetrics
oldLayoutMetrics:(facebook::react::LayoutMetrics)oldLayoutMetrics;

Expand Down
5 changes: 5 additions & 0 deletions React/Fabric/Mounting/UIView+ComponentViewProtocol.mm
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ - (void)updateLocalData:(SharedLocalData)localData oldLocalData:(SharedLocalData
// Default implementation does nothing.
}

- (void)updateState:(facebook::react::State::Shared)state oldState:(facebook::react::State::Shared)oldState
{
// Default implementation does nothing.
}

- (void)updateLayoutMetrics:(LayoutMetrics)layoutMetrics oldLayoutMetrics:(LayoutMetrics)oldLayoutMetrics
{
if (layoutMetrics.frame != oldLayoutMetrics.frame) {
Expand Down

0 comments on commit 9207377

Please sign in to comment.