Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -136,32 +136,42 @@ ShadowNode::ShadowNode(
}
}

// [macOS Backport react/react-native@923e7ddcaf4d (#52646).
std::shared_ptr<ShadowNode> ShadowNode::clone(
const ShadowNodeFragment& fragment) const {
const auto& family = *family_;
const auto& componentDescriptor = family.componentDescriptor_;
if (family.nativeProps_DEPRECATED != nullptr) {
auto propsParserContext = PropsParserContext{family_->getSurfaceId(), {}};
if (fragment.props == ShadowNodeFragment::propsPlaceholder()) {
// Clone existing `props_` with `family.nativeProps_DEPRECATED` to apply
// previously set props via `setNativeProps` API.
auto props = componentDescriptor.cloneProps(
propsParserContext, props_, RawProps(*family.nativeProps_DEPRECATED));
auto clonedNode = componentDescriptor.cloneShadowNode(
*this,
{.props = props,
.children = fragment.children,
.state = fragment.state});
return clonedNode;
} else {
// TODO: We might need to merge fragment.priops with
// `family.nativeProps_DEPRECATED`.
return componentDescriptor.cloneShadowNode(*this, fragment);

std::optional<RawProps> propsOverride;
{
std::lock_guard<std::mutex> lock(family.nativePropsMutex);
if (family.nativeProps_DEPRECATED != nullptr) {
if (fragment.props == ShadowNodeFragment::propsPlaceholder()) {
propsOverride.emplace(*family.nativeProps_DEPRECATED);
} else {
// TODO: We might need to merge fragment.props with
// `family.nativeProps_DEPRECATED`.
}
}
} else {
return componentDescriptor.cloneShadowNode(*this, fragment);
}

if (propsOverride) {
// Clone existing `props_` with `family.nativeProps_DEPRECATED` to
// apply previously set props via `setNativeProps` API. The parsed
// result escapes the lock so the rest of the clone work runs
// unblocked.
auto propsParserContext = PropsParserContext{family_->getSurfaceId(), {}};
return componentDescriptor.cloneShadowNode(
*this,
{.props = componentDescriptor.cloneProps(
propsParserContext, props_, std::move(*propsOverride)),
.children = fragment.children,
.state = fragment.state});
}

return componentDescriptor.cloneShadowNode(*this, fragment);
}
// macOS]

std::shared_ptr<const ContextContainer> ShadowNode::getContextContainer()
const {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#pragma once

#include <memory>
#include <mutex> // [macOS] Backport upstream #52646.
#include <shared_mutex>

#include <react/renderer/core/EventEmitter.h>
Expand Down Expand Up @@ -114,6 +115,7 @@ class ShadowNodeFamily final : public jsi::NativeState {
* architecture and will be removed in the future.
*/
mutable std::unique_ptr<folly::dynamic> nativeProps_DEPRECATED;
mutable std::mutex nativePropsMutex; // [macOS] Backport upstream #52646.

/**
* @return tag for the ShadowNodeFamily.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ std::shared_ptr<ShadowNode> UIManager::createNode(
return shadowNode;
}

// [macOS Backport react/react-native@923e7ddcaf4d (#52646).
std::shared_ptr<ShadowNode> UIManager::cloneNode(
const ShadowNode& shadowNode,
const ShadowNode::SharedListOfShared& children,
Expand All @@ -117,49 +118,51 @@ std::shared_ptr<ShadowNode> UIManager::cloneNode(

auto& componentDescriptor = shadowNode.getComponentDescriptor();
auto& family = shadowNode.getFamily();
auto props = ShadowNodeFragment::propsPlaceholder();

auto props = ShadowNodeFragment::propsPlaceholder();
if (!rawProps.isEmpty()) {
if (family.nativeProps_DEPRECATED != nullptr) {
// 1. update the nativeProps_DEPRECATED props.
//
// In this step, we want the most recent value for the props
// managed by setNativeProps.
// Values in `rawProps` patch (take precedence over)
// `nativeProps_DEPRECATED`. For example, if both
// `nativeProps_DEPRECATED` and `rawProps` contain key 'A'.
// Value from `rawProps` overrides what was previously in
// `nativeProps_DEPRECATED`. Notice that the `nativeProps_DEPRECATED`
// patch will not get more props from `rawProps`: if the key is not
// present in `nativeProps_DEPRECATED`, it will not be added.
//
// The result of this operation is the new `nativeProps_DEPRECATED`.
family.nativeProps_DEPRECATED =
std::make_unique<folly::dynamic>(mergeDynamicProps(
*family.nativeProps_DEPRECATED, // source
(folly::dynamic)rawProps, // patch
NullValueStrategy::Ignore));

// 2. Compute the final set of props.
//
// This step takes the new props handled by `setNativeProps` and
// merges them in the `rawProps` managed by React.
// The new props handled by `nativeProps` now takes precedence
// on the props handled by React, as we want to make sure that
// all the props are applied to the component.
// We use these finalProps as source of truth for the component.
auto finalProps = mergeDynamicProps(
(folly::dynamic)rawProps, // source
*family.nativeProps_DEPRECATED, // patch
NullValueStrategy::Override);

// 3. Clone the props by using finalProps.
props = componentDescriptor.cloneProps(
propsParserContext, shadowNode.getProps(), RawProps(finalProps));
} else {
props = componentDescriptor.cloneProps(
propsParserContext, shadowNode.getProps(), std::move(rawProps));
std::optional<folly::dynamic> finalProps;
{
std::lock_guard<std::mutex> lock(family.nativePropsMutex);
if (family.nativeProps_DEPRECATED != nullptr) {
// 1. update the nativeProps_DEPRECATED props.
//
// In this step, we want the most recent value for the props
// managed by setNativeProps.
// Values in `rawProps` patch (take precedence over)
// `nativeProps_DEPRECATED`. For example, if both
// `nativeProps_DEPRECATED` and `rawProps` contain key 'A'.
// Value from `rawProps` overrides what was previously in
// `nativeProps_DEPRECATED`. Notice that the `nativeProps_DEPRECATED`
// patch will not get more props from `rawProps`: if the key is not
// present in `nativeProps_DEPRECATED`, it will not be added.
//
// The result of this operation is the new `nativeProps_DEPRECATED`.
family.nativeProps_DEPRECATED =
std::make_unique<folly::dynamic>(mergeDynamicProps(
*family.nativeProps_DEPRECATED, // source
(folly::dynamic)rawProps, // patch
NullValueStrategy::Ignore));

// 2. Compute the final set of props.
//
// This step takes the new props handled by `setNativeProps` and
// merges them in the `rawProps` managed by React.
// The new props handled by `nativeProps` now takes precedence
// on the props handled by React, as we want to make sure that
// all the props are applied to the component.
// We use these finalProps as source of truth for the component.
finalProps = mergeDynamicProps(
(folly::dynamic)rawProps, // source
*family.nativeProps_DEPRECATED, // patch
NullValueStrategy::Override);
}
}

props = componentDescriptor.cloneProps(
propsParserContext,
shadowNode.getProps(),
finalProps ? RawProps(std::move(*finalProps)) : std::move(rawProps));
}

auto clonedShadowNode = componentDescriptor.cloneShadowNode(
Expand All @@ -172,6 +175,7 @@ std::shared_ptr<ShadowNode> UIManager::cloneNode(

return clonedShadowNode;
}
// macOS]

void UIManager::appendChild(
const std::shared_ptr<const ShadowNode>& parentShadowNode,
Expand Down Expand Up @@ -428,20 +432,25 @@ void UIManager::setNativeProps_DEPRECATED(
const std::shared_ptr<const ShadowNode>& shadowNode,
RawProps rawProps) const {
auto& family = shadowNode->getFamily();
if (family.nativeProps_DEPRECATED) {
// Values in `rawProps` patch (take precedence over)
// `nativeProps_DEPRECATED`. For example, if both `nativeProps_DEPRECATED`
// and `rawProps` contain key 'A'. Value from `rawProps` overrides what was
// previously in `nativeProps_DEPRECATED`.
family.nativeProps_DEPRECATED =
std::make_unique<folly::dynamic>(mergeDynamicProps(
*family.nativeProps_DEPRECATED,
(folly::dynamic)rawProps,
NullValueStrategy::Override));
} else {
family.nativeProps_DEPRECATED =
std::make_unique<folly::dynamic>((folly::dynamic)rawProps);
// [macOS Backport react/react-native@923e7ddcaf4d (#52646).
{
std::lock_guard<std::mutex> lock(family.nativePropsMutex);
if (family.nativeProps_DEPRECATED) {
// Values in `rawProps` patch (take precedence over)
// `nativeProps_DEPRECATED`. For example, if both `nativeProps_DEPRECATED`
// and `rawProps` contain key 'A'. Value from `rawProps` overrides what
// was previously in `nativeProps_DEPRECATED`.
family.nativeProps_DEPRECATED =
std::make_unique<folly::dynamic>(mergeDynamicProps(
*family.nativeProps_DEPRECATED,
(folly::dynamic)rawProps,
NullValueStrategy::Override));
} else {
family.nativeProps_DEPRECATED =
std::make_unique<folly::dynamic>((folly::dynamic)rawProps);
}
}
// macOS]

shadowTreeRegistry_.visit(
family.getSurfaceId(), [&](const ShadowTree& shadowTree) {
Expand Down
Loading
Loading