// // Created by Marc Rousavy on 19.08.26. // #pragma once #include "RawPropsCompat.hpp" #include #include #include #include namespace margelo::nitro { using namespace facebook; /** * A `react::ConcreteComponentDescriptor` implementation for a Nitro View. * * Requires the `TShadowNode` to be a `react::ShadowNode` composited of `Props` * which support Raw Props Parsing, and `State` which supports holding `Props` * for direct transfer to JNI on Android. */ template class ViewComponentDescriptor final : public react::ConcreteComponentDescriptor { using Base = react::ConcreteComponentDescriptor; using Props = typename TShadowNode::ConcreteProps; using State = typename TShadowNode::ConcreteStateData; #ifdef ANDROID static_assert(std::constructible_from&>, "TShadowNode::ConcreteStateData must be constructible from " "const std::shared_ptr& on Android."); #endif public: explicit ViewComponentDescriptor(const react::ComponentDescriptorParameters& parameters) : Base(parameters, RawPropsCompat::makePropsParser()) {} public: /** * A faster path for cloning props - reuses the caching logic from the `Props`. */ std::shared_ptr cloneProps(const react::PropsParserContext& context, const std::shared_ptr& props, react::RawProps rawProps) const override { // 1. Prepare raw props parser rawProps.parse(this->rawPropsParser_); // 2. Copy props with Nitro's cached copy constructor return TShadowNode::Props(context, /* & */ rawProps, props); } #ifdef ANDROID void adopt(react::ShadowNode& shadowNode) const override { Base::adopt(shadowNode); // This is called immediately after `ShadowNode` is created, cloned or in progress. // On Android, we need to wrap props in our state, which gets routed through Java and later unwrapped in JNI/C++. auto& concreteShadowNode = static_cast(shadowNode); // Start from the stable shared pointer stored by ShadowNode. Some React Native versions implement // `getConcreteSharedProps()` by returning a reference to a temporary cast result. auto constBaseProps = concreteShadowNode.getProps(); auto constProps = std::static_pointer_cast(constBaseProps); const std::shared_ptr& previousProps = concreteShadowNode.getStateData().getProps(); if (previousProps != nullptr && constProps->hasSameProps(*previousProps)) { // None of the Nitro props changed. Keep the existing State identity so // Fabric does not schedule an Android State update for base View props. return; } State state{std::move(constProps)}; concreteShadowNode.setStateData(std::move(state)); } #endif }; } // namespace margelo::nitro