diff --git a/packages/react-native/ReactCommon/react/renderer/animations/tests/LayoutAnimationTest.cpp b/packages/react-native/ReactCommon/react/renderer/animations/tests/LayoutAnimationTest.cpp index 53fe2d986c4..85775d70ad5 100644 --- a/packages/react-native/ReactCommon/react/renderer/animations/tests/LayoutAnimationTest.cpp +++ b/packages/react-native/ReactCommon/react/renderer/animations/tests/LayoutAnimationTest.cpp @@ -56,8 +56,6 @@ static void testShadowNodeTreeLifeCycleLayoutAnimations( ViewComponentDescriptor(componentDescriptorParameters); auto rootComponentDescriptor = RootComponentDescriptor(componentDescriptorParameters); - auto noopEventEmitter = - std::make_shared(nullptr, -1, eventDispatcher); PropsParserContext parserContext{-1, *contextContainer}; @@ -95,7 +93,7 @@ static void testShadowNodeTreeLifeCycleLayoutAnimations( auto surfaceId = SurfaceId(surfaceIdInt); auto family = rootComponentDescriptor.createFamily( - {Tag(surfaceIdInt), surfaceId, nullptr}, nullptr); + {Tag(surfaceIdInt), surfaceId, nullptr}); // Creating an initial root shadow node. auto emptyRootNode = std::const_pointer_cast( diff --git a/packages/react-native/ReactCommon/react/renderer/componentregistry/ComponentDescriptorRegistry.cpp b/packages/react-native/ReactCommon/react/renderer/componentregistry/ComponentDescriptorRegistry.cpp index 892464bc9cc..f6db0dd8a9d 100644 --- a/packages/react-native/ReactCommon/react/renderer/componentregistry/ComponentDescriptorRegistry.cpp +++ b/packages/react-native/ReactCommon/react/renderer/componentregistry/ComponentDescriptorRegistry.cpp @@ -126,12 +126,13 @@ ShadowNode::Shared ComponentDescriptorRegistry::createNode( std::string const &viewName, SurfaceId surfaceId, folly::dynamic const &propsDynamic, - SharedEventTarget const &eventTarget) const { + InstanceHandle::Shared const &instanceHandle) const { auto unifiedComponentName = componentNameByReactViewName(viewName); auto const &componentDescriptor = this->at(unifiedComponentName); - auto const fragment = ShadowNodeFamilyFragment{tag, surfaceId, nullptr}; - auto family = componentDescriptor.createFamily(fragment, eventTarget); + auto const fragment = + ShadowNodeFamilyFragment{tag, surfaceId, instanceHandle}; + auto family = componentDescriptor.createFamily(fragment); auto const props = componentDescriptor.cloneProps( PropsParserContext{surfaceId, *contextContainer_.get()}, nullptr, diff --git a/packages/react-native/ReactCommon/react/renderer/componentregistry/ComponentDescriptorRegistry.h b/packages/react-native/ReactCommon/react/renderer/componentregistry/ComponentDescriptorRegistry.h index 8c5cfe31009..93f18c1168a 100644 --- a/packages/react-native/ReactCommon/react/renderer/componentregistry/ComponentDescriptorRegistry.h +++ b/packages/react-native/ReactCommon/react/renderer/componentregistry/ComponentDescriptorRegistry.h @@ -14,6 +14,7 @@ #include #include +#include #include namespace facebook::react { @@ -59,7 +60,7 @@ class ComponentDescriptorRegistry { std::string const &viewName, SurfaceId surfaceId, folly::dynamic const &props, - SharedEventTarget const &eventTarget) const; + InstanceHandle::Shared const &instanceHandle) const; void setFallbackComponentDescriptor( const SharedComponentDescriptor &descriptor); diff --git a/packages/react-native/ReactCommon/react/renderer/core/ComponentDescriptor.h b/packages/react-native/ReactCommon/react/renderer/core/ComponentDescriptor.h index c7ced77e41b..8b3fabe9e2e 100644 --- a/packages/react-native/ReactCommon/react/renderer/core/ComponentDescriptor.h +++ b/packages/react-native/ReactCommon/react/renderer/core/ComponentDescriptor.h @@ -8,6 +8,8 @@ #pragma once #include +#include +#include #include #include #include @@ -136,8 +138,13 @@ class ComponentDescriptor { * Creates a shadow node family for particular node. */ virtual ShadowNodeFamily::Shared createFamily( - ShadowNodeFamilyFragment const &fragment, - SharedEventTarget eventTarget) const = 0; + ShadowNodeFamilyFragment const &fragment) const = 0; + + /* + * Creates an event emitter for particular node. + */ + virtual SharedEventEmitter createEventEmitter( + InstanceHandle::Shared const &instanceHandle) const = 0; protected: EventDispatcher::Weak eventDispatcher_; diff --git a/packages/react-native/ReactCommon/react/renderer/core/ConcreteComponentDescriptor.h b/packages/react-native/ReactCommon/react/renderer/core/ConcreteComponentDescriptor.h index f6fbcb9029e..3315e745ab8 100644 --- a/packages/react-native/ReactCommon/react/renderer/core/ConcreteComponentDescriptor.h +++ b/packages/react-native/ReactCommon/react/renderer/core/ConcreteComponentDescriptor.h @@ -180,17 +180,20 @@ class ConcreteComponentDescriptor : public ComponentDescriptor { } ShadowNodeFamily::Shared createFamily( - ShadowNodeFamilyFragment const &fragment, - SharedEventTarget eventTarget) const override { - auto eventEmitter = std::make_shared( - std::move(eventTarget), fragment.tag, eventDispatcher_); + ShadowNodeFamilyFragment const &fragment) const override { return std::make_shared( ShadowNodeFamilyFragment{ - fragment.tag, fragment.surfaceId, eventEmitter}, + fragment.tag, fragment.surfaceId, fragment.instanceHandle}, eventDispatcher_, *this); } + SharedEventEmitter createEventEmitter( + InstanceHandle::Shared const &instanceHandle) const override { + return std::make_shared( + std::make_shared(instanceHandle), eventDispatcher_); + } + protected: /* * Called immediately after `ShadowNode` is created or cloned. diff --git a/packages/react-native/ReactCommon/react/renderer/core/EventEmitter.cpp b/packages/react-native/ReactCommon/react/renderer/core/EventEmitter.cpp index 2426cea3ca2..22a12a2ffb4 100644 --- a/packages/react-native/ReactCommon/react/renderer/core/EventEmitter.cpp +++ b/packages/react-native/ReactCommon/react/renderer/core/EventEmitter.cpp @@ -43,7 +43,6 @@ ValueFactory EventEmitter::defaultPayloadFactory() { EventEmitter::EventEmitter( SharedEventTarget eventTarget, - Tag /*tag*/, EventDispatcher::Weak eventDispatcher) : eventTarget_(std::move(eventTarget)), eventDispatcher_(std::move(eventDispatcher)) {} @@ -131,8 +130,4 @@ void EventEmitter::setEnabled(bool enabled) const { } } -const SharedEventTarget &EventEmitter::getEventTarget() const { - return eventTarget_; -} - } // namespace facebook::react diff --git a/packages/react-native/ReactCommon/react/renderer/core/EventEmitter.h b/packages/react-native/ReactCommon/react/renderer/core/EventEmitter.h index 36eae1ad1ff..c02d605b43b 100644 --- a/packages/react-native/ReactCommon/react/renderer/core/EventEmitter.h +++ b/packages/react-native/ReactCommon/react/renderer/core/EventEmitter.h @@ -38,7 +38,6 @@ class EventEmitter { EventEmitter( SharedEventTarget eventTarget, - Tag tag, EventDispatcher::Weak eventDispatcher); virtual ~EventEmitter() = default; @@ -55,8 +54,6 @@ class EventEmitter { */ void setEnabled(bool enabled) const; - SharedEventTarget const &getEventTarget() const; - protected: #ifdef ANDROID // We need this temporarily due to lack of Java-counterparts for particular diff --git a/packages/react-native/ReactCommon/react/renderer/core/EventTarget.cpp b/packages/react-native/ReactCommon/react/renderer/core/EventTarget.cpp index 38e78e500f8..82118ceee8b 100644 --- a/packages/react-native/ReactCommon/react/renderer/core/EventTarget.cpp +++ b/packages/react-native/ReactCommon/react/renderer/core/EventTarget.cpp @@ -13,14 +13,9 @@ namespace facebook::react { using Tag = EventTarget::Tag; -EventTarget::EventTarget( - jsi::Runtime &runtime, - jsi::Value const &instanceHandle, - Tag tag) - : weakInstanceHandle_( - jsi::WeakObject(runtime, instanceHandle.asObject(runtime))), - strongInstanceHandle_(jsi::Value::null()), - tag_(tag) {} +EventTarget::EventTarget(InstanceHandle::Shared instanceHandle) + : instanceHandle_(std::move(instanceHandle)), + strongInstanceHandle_(jsi::Value::null()) {} void EventTarget::setEnabled(bool enabled) const { enabled_ = enabled; @@ -31,7 +26,7 @@ void EventTarget::retain(jsi::Runtime &runtime) const { return; } - strongInstanceHandle_ = weakInstanceHandle_.lock(runtime); + strongInstanceHandle_ = instanceHandle_->getInstanceHandle(runtime); // Having a `null` or `undefined` object here indicates that // `weakInstanceHandle_` was already deallocated. This should *not* happen by @@ -62,7 +57,7 @@ jsi::Value EventTarget::getInstanceHandle(jsi::Runtime &runtime) const { } Tag EventTarget::getTag() const { - return tag_; + return instanceHandle_->getTag(); } } // namespace facebook::react diff --git a/packages/react-native/ReactCommon/react/renderer/core/EventTarget.h b/packages/react-native/ReactCommon/react/renderer/core/EventTarget.h index f6b29947805..1b023dc6510 100644 --- a/packages/react-native/ReactCommon/react/renderer/core/EventTarget.h +++ b/packages/react-native/ReactCommon/react/renderer/core/EventTarget.h @@ -7,9 +7,9 @@ #pragma once -#include - #include +#include +#include namespace facebook::react { @@ -34,7 +34,7 @@ class EventTarget { /* * Constructs an EventTarget from a weak instance handler and a tag. */ - EventTarget(jsi::Runtime &runtime, jsi::Value const &instanceHandle, Tag tag); + explicit EventTarget(InstanceHandle::Shared instanceHandle); /* * Sets the `enabled` flag that allows creating a strong instance handle from @@ -65,10 +65,9 @@ class EventTarget { Tag getTag() const; private: + const InstanceHandle::Shared instanceHandle_; mutable bool enabled_{false}; // Protected by `EventEmitter::DispatchMutex()`. - mutable jsi::WeakObject weakInstanceHandle_; // Protected by `jsi::Runtime &`. mutable jsi::Value strongInstanceHandle_; // Protected by `jsi::Runtime &`. - Tag tag_; }; using SharedEventTarget = std::shared_ptr; diff --git a/packages/react-native/ReactCommon/react/renderer/core/InstanceHandle.cpp b/packages/react-native/ReactCommon/react/renderer/core/InstanceHandle.cpp new file mode 100644 index 00000000000..9d7ccef277e --- /dev/null +++ b/packages/react-native/ReactCommon/react/renderer/core/InstanceHandle.cpp @@ -0,0 +1,28 @@ +/* + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +#include "InstanceHandle.h" + +namespace facebook::react { + +InstanceHandle::InstanceHandle( + jsi::Runtime &runtime, + jsi::Value const &instanceHandle, + Tag tag) + : weakInstanceHandle_( + jsi::WeakObject(runtime, instanceHandle.asObject(runtime))), + tag_(tag) {} + +jsi::Value InstanceHandle::getInstanceHandle(jsi::Runtime &runtime) const { + return weakInstanceHandle_.lock(runtime); +} + +Tag InstanceHandle::getTag() const { + return tag_; +} + +} // namespace facebook::react diff --git a/packages/react-native/ReactCommon/react/renderer/core/InstanceHandle.h b/packages/react-native/ReactCommon/react/renderer/core/InstanceHandle.h new file mode 100644 index 00000000000..171d03011e9 --- /dev/null +++ b/packages/react-native/ReactCommon/react/renderer/core/InstanceHandle.h @@ -0,0 +1,41 @@ +/* + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +#pragma once + +#include +#include +#include + +namespace facebook::react { + +class InstanceHandle { + public: + using Shared = std::shared_ptr; + + InstanceHandle( + jsi::Runtime &runtime, + jsi::Value const &instanceHandle, + Tag tag); + + /* + * Creates and returns the `instanceHandle`. + * Returns `null` if the `instanceHandle` is not retained at this moment. + */ + jsi::Value getInstanceHandle(jsi::Runtime &runtime) const; + + /* + * Deprecated. Do not use. + */ + Tag getTag() const; + + private: + const jsi::WeakObject weakInstanceHandle_; // Protected by `jsi::Runtime &`. + const Tag tag_; +}; + +} // namespace facebook::react diff --git a/packages/react-native/ReactCommon/react/renderer/core/ReactPrimitives.h b/packages/react-native/ReactCommon/react/renderer/core/ReactPrimitives.h index d77ac597132..93cadde558d 100644 --- a/packages/react-native/ReactCommon/react/renderer/core/ReactPrimitives.h +++ b/packages/react-native/ReactCommon/react/renderer/core/ReactPrimitives.h @@ -17,8 +17,6 @@ namespace facebook::react { * `Tag` and `InstanceHandle` are used to address React Native components. */ using Tag = int32_t; -using InstanceHandle = struct InstanceHandleDummyStruct { -} *; /* * An id of a running Surface instance that is used to refer to the instance. diff --git a/packages/react-native/ReactCommon/react/renderer/core/ShadowNode.cpp b/packages/react-native/ReactCommon/react/renderer/core/ShadowNode.cpp index 6068eb3e822..ba8da5d6f1e 100644 --- a/packages/react-native/ReactCommon/react/renderer/core/ShadowNode.cpp +++ b/packages/react-native/ReactCommon/react/renderer/core/ShadowNode.cpp @@ -174,6 +174,15 @@ const SharedEventEmitter &ShadowNode::getEventEmitter() const { return family_->eventEmitter_; } +jsi::Value ShadowNode::getInstanceHandle(jsi::Runtime &runtime) const { + auto instanceHandle = family_->instanceHandle_; + if (instanceHandle == nullptr) { + return jsi::Value::null(); + } + + return instanceHandle->getInstanceHandle(runtime); +} + Tag ShadowNode::getTag() const { return family_->tag_; } diff --git a/packages/react-native/ReactCommon/react/renderer/core/ShadowNode.h b/packages/react-native/ReactCommon/react/renderer/core/ShadowNode.h index 265dc9a921e..4e428b25997 100644 --- a/packages/react-native/ReactCommon/react/renderer/core/ShadowNode.h +++ b/packages/react-native/ReactCommon/react/renderer/core/ShadowNode.h @@ -123,6 +123,7 @@ class ShadowNode : public Sealable, Props::Shared const &getProps() const; ListOfShared const &getChildren() const; SharedEventEmitter const &getEventEmitter() const; + jsi::Value getInstanceHandle(jsi::Runtime &runtime) const; Tag getTag() const; SurfaceId getSurfaceId() const; diff --git a/packages/react-native/ReactCommon/react/renderer/core/ShadowNodeFamily.cpp b/packages/react-native/ReactCommon/react/renderer/core/ShadowNodeFamily.cpp index 8883a033b9b..5035656aceb 100644 --- a/packages/react-native/ReactCommon/react/renderer/core/ShadowNodeFamily.cpp +++ b/packages/react-native/ReactCommon/react/renderer/core/ShadowNodeFamily.cpp @@ -25,7 +25,9 @@ ShadowNodeFamily::ShadowNodeFamily( : eventDispatcher_(std::move(eventDispatcher)), tag_(fragment.tag), surfaceId_(fragment.surfaceId), - eventEmitter_(fragment.eventEmitter), + instanceHandle_(fragment.instanceHandle), + eventEmitter_( + componentDescriptor.createEventEmitter(fragment.instanceHandle)), componentDescriptor_(componentDescriptor), componentHandle_(componentDescriptor.getComponentHandle()), componentName_(componentDescriptor.getComponentName()) {} diff --git a/packages/react-native/ReactCommon/react/renderer/core/ShadowNodeFamily.h b/packages/react-native/ReactCommon/react/renderer/core/ShadowNodeFamily.h index c2220a2e57f..401686bc806 100644 --- a/packages/react-native/ReactCommon/react/renderer/core/ShadowNodeFamily.h +++ b/packages/react-native/ReactCommon/react/renderer/core/ShadowNodeFamily.h @@ -13,6 +13,7 @@ #include #include +#include #include namespace facebook::react { @@ -36,7 +37,7 @@ class State; struct ShadowNodeFamilyFragment { Tag const tag; SurfaceId const surfaceId; - EventEmitter::Shared const &eventEmitter; + InstanceHandle::Shared const &instanceHandle; }; /* @@ -137,6 +138,11 @@ class ShadowNodeFamily final { */ SurfaceId const surfaceId_; + /* + * Weak reference to the React instance handle + */ + InstanceHandle::Shared const instanceHandle_; + /* * `EventEmitter` associated with all nodes of the family. */ diff --git a/packages/react-native/ReactCommon/react/renderer/core/tests/ComponentDescriptorTest.cpp b/packages/react-native/ReactCommon/react/renderer/core/tests/ComponentDescriptorTest.cpp index 33bd463ab3f..021f62b4f58 100644 --- a/packages/react-native/ReactCommon/react/renderer/core/tests/ComponentDescriptorTest.cpp +++ b/packages/react-native/ReactCommon/react/renderer/core/tests/ComponentDescriptorTest.cpp @@ -29,13 +29,11 @@ TEST(ComponentDescriptorTest, createShadowNode) { const auto &raw = RawProps(folly::dynamic::object("nativeID", "abc")); Props::Shared props = descriptor->cloneProps(parserContext, nullptr, raw); - auto family = descriptor->createFamily( - ShadowNodeFamilyFragment{ - /* .tag = */ 9, - /* .surfaceId = */ 1, - /* .eventEmitter = */ nullptr, - }, - nullptr); + auto family = descriptor->createFamily(ShadowNodeFamilyFragment{ + /* .tag = */ 9, + /* .surfaceId = */ 1, + /* .instanceHandle = */ nullptr, + }); ShadowNode::Shared node = descriptor->createShadowNode( ShadowNodeFragment{ @@ -62,13 +60,11 @@ TEST(ComponentDescriptorTest, cloneShadowNode) { const auto &raw = RawProps(folly::dynamic::object("nativeID", "abc")); Props::Shared props = descriptor->cloneProps(parserContext, nullptr, raw); - auto family = descriptor->createFamily( - ShadowNodeFamilyFragment{ - /* .tag = */ 9, - /* .surfaceId = */ 1, - /* .eventEmitter = */ nullptr, - }, - nullptr); + auto family = descriptor->createFamily(ShadowNodeFamilyFragment{ + /* .tag = */ 9, + /* .surfaceId = */ 1, + /* .instanceHandle = */ nullptr, + }); ShadowNode::Shared node = descriptor->createShadowNode( ShadowNodeFragment{ /* .props = */ props, @@ -97,37 +93,31 @@ TEST(ComponentDescriptorTest, appendChild) { const auto &raw = RawProps(folly::dynamic::object("nativeID", "abc")); Props::Shared props = descriptor->cloneProps(parserContext, nullptr, raw); - auto family1 = descriptor->createFamily( - ShadowNodeFamilyFragment{ - /* .tag = */ 1, - /* .surfaceId = */ 1, - /* .eventEmitter = */ nullptr, - }, - nullptr); + auto family1 = descriptor->createFamily(ShadowNodeFamilyFragment{ + /* .tag = */ 1, + /* .surfaceId = */ 1, + /* .instanceHandle = */ nullptr, + }); ShadowNode::Shared node1 = descriptor->createShadowNode( ShadowNodeFragment{ /* .props = */ props, }, family1); - auto family2 = descriptor->createFamily( - ShadowNodeFamilyFragment{ - /* .tag = */ 2, - /* .surfaceId = */ 1, - /* .eventEmitter = */ nullptr, - }, - nullptr); + auto family2 = descriptor->createFamily(ShadowNodeFamilyFragment{ + /* .tag = */ 2, + /* .surfaceId = */ 1, + /* .instanceHandle = */ nullptr, + }); ShadowNode::Shared node2 = descriptor->createShadowNode( ShadowNodeFragment{ /* .props = */ props, }, family2); - auto family3 = descriptor->createFamily( - ShadowNodeFamilyFragment{ - /* .tag = */ 3, - /* .surfaceId = */ 1, - /* .eventEmitter = */ nullptr, - }, - nullptr); + auto family3 = descriptor->createFamily(ShadowNodeFamilyFragment{ + /* .tag = */ 3, + /* .surfaceId = */ 1, + /* .instanceHandle = */ nullptr, + }); ShadowNode::Shared node3 = descriptor->createShadowNode( ShadowNodeFragment{ /* .props = */ props, diff --git a/packages/react-native/ReactCommon/react/renderer/core/tests/ShadowNodeTest.cpp b/packages/react-native/ReactCommon/react/renderer/core/tests/ShadowNodeTest.cpp index 65e6705cb58..2513d84a5d5 100644 --- a/packages/react-native/ReactCommon/react/renderer/core/tests/ShadowNodeTest.cpp +++ b/packages/react-native/ReactCommon/react/renderer/core/tests/ShadowNodeTest.cpp @@ -41,7 +41,7 @@ class ShadowNodeTest : public ::testing::Test { ShadowNodeFamilyFragment{ /* .tag = */ 11, /* .surfaceId = */ surfaceId_, - /* .eventEmitter = */ nullptr, + /* .instanceHandle = */ nullptr, }, eventDispatcher_, componentDescriptor_); @@ -57,7 +57,7 @@ class ShadowNodeTest : public ::testing::Test { ShadowNodeFamilyFragment{ /* .tag = */ 12, /* .surfaceId = */ surfaceId_, - /* .eventEmitter = */ nullptr, + /* .instanceHandle = */ nullptr, }, eventDispatcher_, componentDescriptor_); @@ -73,7 +73,7 @@ class ShadowNodeTest : public ::testing::Test { ShadowNodeFamilyFragment{ /* .tag = */ 13, /* .surfaceId = */ surfaceId_, - /* .eventEmitter = */ nullptr, + /* .instanceHandle = */ nullptr, }, eventDispatcher_, componentDescriptor_); @@ -92,7 +92,7 @@ class ShadowNodeTest : public ::testing::Test { ShadowNodeFamilyFragment{ /* .tag = */ 15, /* .surfaceId = */ surfaceId_, - /* .eventEmitter = */ nullptr, + /* .instanceHandle = */ nullptr, }, eventDispatcher_, componentDescriptor_); @@ -108,7 +108,7 @@ class ShadowNodeTest : public ::testing::Test { ShadowNodeFamilyFragment{ /* .tag = */ 16, /* .surfaceId = */ surfaceId_, - /* .eventEmitter = */ nullptr, + /* .instanceHandle = */ nullptr, }, eventDispatcher_, componentDescriptor_); @@ -127,7 +127,7 @@ class ShadowNodeTest : public ::testing::Test { ShadowNodeFamilyFragment{ /* .tag = */ 17, /* .surfaceId = */ surfaceId_, - /* .eventEmitter = */ nullptr, + /* .instanceHandle = */ nullptr, }, eventDispatcher_, componentDescriptor_); @@ -143,7 +143,7 @@ class ShadowNodeTest : public ::testing::Test { ShadowNodeFamilyFragment{ /* .tag = */ 18, /* .surfaceId = */ surfaceId_, - /* .eventEmitter = */ nullptr, + /* .instanceHandle = */ nullptr, }, eventDispatcher_, componentDescriptor_); @@ -174,7 +174,7 @@ TEST_F(ShadowNodeTest, handleShadowNodeCreation) { EXPECT_STREQ(nodeZ_->getComponentName(), "Test"); EXPECT_EQ(nodeZ_->getTag(), 18); EXPECT_EQ(nodeZ_->getSurfaceId(), surfaceId_); - EXPECT_EQ(nodeZ_->getEventEmitter(), nullptr); + EXPECT_NE(nodeZ_->getEventEmitter(), nullptr); EXPECT_EQ(nodeZ_->getChildren().size(), 0); } @@ -238,7 +238,7 @@ TEST_F(ShadowNodeTest, handleState) { ShadowNodeFamilyFragment{ /* .tag = */ 9, /* .surfaceId = */ surfaceId_, - /* .eventEmitter = */ nullptr, + /* .instanceHandle = */ nullptr, }, eventDispatcher_, componentDescriptor_); diff --git a/packages/react-native/ReactCommon/react/renderer/element/ComponentBuilder.cpp b/packages/react-native/ReactCommon/react/renderer/element/ComponentBuilder.cpp index 35973c49a2b..10d061cb858 100644 --- a/packages/react-native/ReactCommon/react/renderer/element/ComponentBuilder.cpp +++ b/packages/react-native/ReactCommon/react/renderer/element/ComponentBuilder.cpp @@ -26,10 +26,8 @@ ShadowNode::Unshared ComponentBuilder::build( children.push_back(build(childFragment)); } - auto family = componentDescriptor.createFamily( - ShadowNodeFamilyFragment{ - elementFragment.tag, elementFragment.surfaceId, nullptr}, - nullptr); + auto family = componentDescriptor.createFamily(ShadowNodeFamilyFragment{ + elementFragment.tag, elementFragment.surfaceId, nullptr}); auto initialState = componentDescriptor.createInitialState(elementFragment.props, family); diff --git a/packages/react-native/ReactCommon/react/renderer/mounting/ShadowTree.cpp b/packages/react-native/ReactCommon/react/renderer/mounting/ShadowTree.cpp index 0b9106bbb5d..f504c986b5c 100644 --- a/packages/react-native/ReactCommon/react/renderer/mounting/ShadowTree.cpp +++ b/packages/react-native/ReactCommon/react/renderer/mounting/ShadowTree.cpp @@ -225,9 +225,6 @@ ShadowTree::ShadowTree( ShadowTreeDelegate const &delegate, ContextContainer const &contextContainer) : surfaceId_(surfaceId), delegate_(delegate) { - const auto noopEventEmitter = std::make_shared( - nullptr, -1, std::shared_ptr()); - static auto globalRootComponentDescriptor = std::make_unique( ComponentDescriptorParameters{ @@ -239,9 +236,8 @@ ShadowTree::ShadowTree( layoutConstraints, layoutContext); - auto const fragment = - ShadowNodeFamilyFragment{surfaceId, surfaceId, noopEventEmitter}; - auto family = globalRootComponentDescriptor->createFamily(fragment, nullptr); + auto const fragment = ShadowNodeFamilyFragment{surfaceId, surfaceId, nullptr}; + auto family = globalRootComponentDescriptor->createFamily(fragment); auto rootShadowNode = std::static_pointer_cast( globalRootComponentDescriptor->createShadowNode( diff --git a/packages/react-native/ReactCommon/react/renderer/mounting/tests/MountingTest.cpp b/packages/react-native/ReactCommon/react/renderer/mounting/tests/MountingTest.cpp index 75945738a5a..2ecd5413846 100644 --- a/packages/react-native/ReactCommon/react/renderer/mounting/tests/MountingTest.cpp +++ b/packages/react-native/ReactCommon/react/renderer/mounting/tests/MountingTest.cpp @@ -53,7 +53,7 @@ static ShadowNode::Shared makeNode( return componentDescriptor.createShadowNode( ShadowNodeFragment{ props, std::make_shared(children)}, - componentDescriptor.createFamily({tag, SurfaceId(1), nullptr}, nullptr)); + componentDescriptor.createFamily({tag, SurfaceId(1), nullptr})); } /** @@ -79,8 +79,8 @@ TEST(MountingTest, testReorderingInstructionGeneration) { auto rootComponentDescriptor = RootComponentDescriptor(componentDescriptorParameters); - auto rootFamily = rootComponentDescriptor.createFamily( - {Tag(1), SurfaceId(1), nullptr}, nullptr); + auto rootFamily = + rootComponentDescriptor.createFamily({Tag(1), SurfaceId(1), nullptr}); // Creating an initial root shadow node. auto emptyRootNode = std::const_pointer_cast( @@ -110,8 +110,8 @@ TEST(MountingTest, testReorderingInstructionGeneration) { auto childJ = makeNode(viewComponentDescriptor, 109, {}); auto childK = makeNode(viewComponentDescriptor, 110, {}); - auto family = viewComponentDescriptor.createFamily( - {10, SurfaceId(1), nullptr}, nullptr); + auto family = + viewComponentDescriptor.createFamily({10, SurfaceId(1), nullptr}); // Construct "identical" shadow nodes: they differ only in children. auto shadowNodeV1 = viewComponentDescriptor.createShadowNode( @@ -390,8 +390,8 @@ TEST(MountingTest, testViewReparentingInstructionGeneration) { auto rootComponentDescriptor = RootComponentDescriptor(componentDescriptorParameters); - auto rootFamily = rootComponentDescriptor.createFamily( - {Tag(1), SurfaceId(1), nullptr}, nullptr); + auto rootFamily = + rootComponentDescriptor.createFamily({Tag(1), SurfaceId(1), nullptr}); // Creating an initial root shadow node. auto emptyRootNode = std::const_pointer_cast( @@ -422,8 +422,8 @@ TEST(MountingTest, testViewReparentingInstructionGeneration) { auto childJ = makeNode(viewComponentDescriptor, 109, {}); auto childK = makeNode(viewComponentDescriptor, 110, {}); - auto family = viewComponentDescriptor.createFamily( - {10, SurfaceId(1), nullptr}, nullptr); + auto family = + viewComponentDescriptor.createFamily({10, SurfaceId(1), nullptr}); auto reparentedViewA = makeNode( viewComponentDescriptor, diff --git a/packages/react-native/ReactCommon/react/renderer/mounting/tests/ShadowTreeLifeCycleTest.cpp b/packages/react-native/ReactCommon/react/renderer/mounting/tests/ShadowTreeLifeCycleTest.cpp index 0e53a7ad9b7..02607035ad4 100644 --- a/packages/react-native/ReactCommon/react/renderer/mounting/tests/ShadowTreeLifeCycleTest.cpp +++ b/packages/react-native/ReactCommon/react/renderer/mounting/tests/ShadowTreeLifeCycleTest.cpp @@ -42,8 +42,6 @@ static void testShadowNodeTreeLifeCycle( ViewComponentDescriptor(componentDescriptorParameters); auto rootComponentDescriptor = RootComponentDescriptor(componentDescriptorParameters); - auto noopEventEmitter = - std::make_shared(nullptr, -1, eventDispatcher); PropsParserContext parserContext{-1, *contextContainer}; @@ -52,8 +50,8 @@ static void testShadowNodeTreeLifeCycle( for (int i = 0; i < repeats; i++) { allNodes.clear(); - auto family = rootComponentDescriptor.createFamily( - {Tag(1), SurfaceId(1), nullptr}, nullptr); + auto family = + rootComponentDescriptor.createFamily({Tag(1), SurfaceId(1), nullptr}); // Creating an initial root shadow node. auto emptyRootNode = std::const_pointer_cast( @@ -195,8 +193,6 @@ static void testShadowNodeTreeLifeCycleExtensiveFlatteningUnflattening( ViewComponentDescriptor(componentDescriptorParameters); auto rootComponentDescriptor = RootComponentDescriptor(componentDescriptorParameters); - auto noopEventEmitter = - std::make_shared(nullptr, -1, eventDispatcher); PropsParserContext parserContext{-1, *contextContainer}; @@ -205,8 +201,8 @@ static void testShadowNodeTreeLifeCycleExtensiveFlatteningUnflattening( for (int i = 0; i < repeats; i++) { allNodes.clear(); - auto family = rootComponentDescriptor.createFamily( - {Tag(1), SurfaceId(1), nullptr}, nullptr); + auto family = + rootComponentDescriptor.createFamily({Tag(1), SurfaceId(1), nullptr}); // Creating an initial root shadow node. auto emptyRootNode = std::const_pointer_cast( diff --git a/packages/react-native/ReactCommon/react/renderer/uimanager/UIManager.cpp b/packages/react-native/ReactCommon/react/renderer/uimanager/UIManager.cpp index 291195057ca..355a3a1dad8 100644 --- a/packages/react-native/ReactCommon/react/renderer/uimanager/UIManager.cpp +++ b/packages/react-native/ReactCommon/react/renderer/uimanager/UIManager.cpp @@ -68,7 +68,7 @@ ShadowNode::Shared UIManager::createNode( std::string const &name, SurfaceId surfaceId, const RawProps &rawProps, - SharedEventTarget eventTarget) const { + const InstanceHandle::Shared &instanceHandle) const { SystraceSection s("UIManager::createNode"); auto &componentDescriptor = componentDescriptorRegistry_->at(name); @@ -77,9 +77,9 @@ ShadowNode::Shared UIManager::createNode( PropsParserContext propsParserContext{surfaceId, *contextContainer_.get()}; - auto const fragment = ShadowNodeFamilyFragment{tag, surfaceId, nullptr}; - auto family = - componentDescriptor.createFamily(fragment, std::move(eventTarget)); + auto const fragment = + ShadowNodeFamilyFragment{tag, surfaceId, instanceHandle}; + auto family = componentDescriptor.createFamily(fragment); auto const props = componentDescriptor.cloneProps(propsParserContext, nullptr, rawProps); auto const state = componentDescriptor.createInitialState(props, family); diff --git a/packages/react-native/ReactCommon/react/renderer/uimanager/UIManager.h b/packages/react-native/ReactCommon/react/renderer/uimanager/UIManager.h index 97db13c7927..31db46be2c0 100644 --- a/packages/react-native/ReactCommon/react/renderer/uimanager/UIManager.h +++ b/packages/react-native/ReactCommon/react/renderer/uimanager/UIManager.h @@ -14,6 +14,7 @@ #include #include +#include #include #include #include @@ -134,7 +135,7 @@ class UIManager final : public ShadowTreeDelegate { std::string const &componentName, SurfaceId surfaceId, const RawProps &props, - SharedEventTarget eventTarget) const; + const InstanceHandle::Shared &instanceHandle) const; ShadowNode::Shared cloneNode( ShadowNode const &shadowNode, diff --git a/packages/react-native/ReactCommon/react/renderer/uimanager/UIManagerBinding.cpp b/packages/react-native/ReactCommon/react/renderer/uimanager/UIManagerBinding.cpp index f9c0580a228..54485334feb 100644 --- a/packages/react-native/ReactCommon/react/renderer/uimanager/UIManagerBinding.cpp +++ b/packages/react-native/ReactCommon/react/renderer/uimanager/UIManagerBinding.cpp @@ -186,12 +186,13 @@ jsi::Value UIManagerBinding::get( jsi::Value const & /*thisValue*/, jsi::Value const *arguments, size_t /*count*/) noexcept -> jsi::Value { - auto eventTarget = - eventTargetFromValue(runtime, arguments[4], arguments[0]); - if (!eventTarget) { + auto instanceHandle = + instanceHandleFromValue(runtime, arguments[4], arguments[0]); + if (!instanceHandle) { react_native_assert(false); return jsi::Value::undefined(); } + return valueFromShadowNode( runtime, uiManager->createNode( @@ -199,7 +200,7 @@ jsi::Value UIManagerBinding::get( stringFromValue(runtime, arguments[1]), surfaceIdFromValue(runtime, arguments[2]), RawProps(runtime, arguments[3]), - eventTarget)); + instanceHandle)); }); } @@ -798,7 +799,7 @@ jsi::Value UIManagerBinding::get( return jsi::Value::null(); } - return getInstanceHandleFromShadowNode(parentShadowNode, runtime); + return (*parentShadowNode).getInstanceHandle(runtime); }); } @@ -1006,8 +1007,7 @@ jsi::Value UIManagerBinding::get( return jsi::Array::createWithElements( runtime, - getInstanceHandleFromShadowNode( - newestParentOfShadowNode, runtime), + (*newestParentOfShadowNode).getInstanceHandle(runtime), jsi::Value{runtime, (double)offsetTop}, jsi::Value{runtime, (double)offsetLeft}); }); diff --git a/packages/react-native/ReactCommon/react/renderer/uimanager/primitives.h b/packages/react-native/ReactCommon/react/renderer/uimanager/primitives.h index 6c4893dccf5..629a37b6589 100644 --- a/packages/react-native/ReactCommon/react/renderer/uimanager/primitives.h +++ b/packages/react-native/ReactCommon/react/renderer/uimanager/primitives.h @@ -142,16 +142,16 @@ inline static Tag tagFromValue(jsi::Value const &value) { return (Tag)value.getNumber(); } -inline static SharedEventTarget eventTargetFromValue( +inline static InstanceHandle::Shared instanceHandleFromValue( jsi::Runtime &runtime, - jsi::Value const &eventTargetValue, + jsi::Value const &instanceHandleValue, jsi::Value const &tagValue) { - react_native_assert(!eventTargetValue.isNull()); - if (eventTargetValue.isNull()) { + react_native_assert(!instanceHandleValue.isNull()); + if (instanceHandleValue.isNull()) { return nullptr; } - return std::make_shared( - runtime, eventTargetValue, tagFromValue(tagValue)); + return std::make_shared( + runtime, instanceHandleValue, tagFromValue(tagValue)); } inline static SurfaceId surfaceIdFromValue( @@ -185,21 +185,6 @@ inline static folly::dynamic commandArgsFromValue( return jsi::dynamicFromValue(runtime, value); } -inline static jsi::Value getInstanceHandleFromShadowNode( - ShadowNode::Shared shadowNode, - jsi::Runtime &runtime) { - auto eventTarget = shadowNode->getEventEmitter()->getEventTarget(); - // shadowNode is probably a RootShadowNode and they don't have - // event targets. - if (eventTarget == nullptr) { - return jsi::Value::null(); - } - eventTarget->retain(runtime); - auto instanceHandle = eventTarget->getInstanceHandle(runtime); - eventTarget->release(runtime); - return instanceHandle; -} - inline static jsi::Value getArrayOfInstanceHandlesFromShadowNodes( ShadowNode::ListOfShared const &nodes, jsi::Runtime &runtime) { @@ -209,7 +194,7 @@ inline static jsi::Value getArrayOfInstanceHandlesFromShadowNodes( std::vector nonNullInstanceHandles; nonNullInstanceHandles.reserve(nodes.size()); for (auto const &shadowNode : nodes) { - auto instanceHandle = getInstanceHandleFromShadowNode(shadowNode, runtime); + auto instanceHandle = (*shadowNode).getInstanceHandle(runtime); if (!instanceHandle.isNull()) { nonNullInstanceHandles.push_back(std::move(instanceHandle)); } diff --git a/packages/react-native/ReactCommon/react/test_utils/shadowTreeGeneration.h b/packages/react-native/ReactCommon/react/test_utils/shadowTreeGeneration.h index 65307403d8e..da8aa09e168 100644 --- a/packages/react-native/ReactCommon/react/test_utils/shadowTreeGeneration.h +++ b/packages/react-native/ReactCommon/react/test_utils/shadowTreeGeneration.h @@ -290,7 +290,7 @@ static inline ShadowNode::Shared generateShadowNodeTree( int deviation = 3) { if (size <= 1) { auto family = componentDescriptor.createFamily( - {generateReactTag(), SurfaceId(1), nullptr}, nullptr); + {generateReactTag(), SurfaceId(1), nullptr}); return componentDescriptor.createShadowNode( ShadowNodeFragment{generateDefaultProps(componentDescriptor)}, family); } @@ -306,7 +306,7 @@ static inline ShadowNode::Shared generateShadowNodeTree( } auto family = componentDescriptor.createFamily( - {generateReactTag(), SurfaceId(1), nullptr}, nullptr); + {generateReactTag(), SurfaceId(1), nullptr}); return componentDescriptor.createShadowNode( ShadowNodeFragment{ generateDefaultProps(componentDescriptor),