2018-03-19 02:04:10 +00:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <string>
|
2018-04-27 00:51:27 +00:00
|
|
|
#include <memory>
|
2018-03-19 02:04:10 +00:00
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include <fabric/core/Props.h>
|
|
|
|
#include <fabric/core/Sealable.h>
|
|
|
|
#include <fabric/core/ReactPrimitives.h>
|
|
|
|
#include <fabric/debug/DebugStringConvertible.h>
|
|
|
|
|
|
|
|
namespace facebook {
|
|
|
|
namespace react {
|
|
|
|
|
|
|
|
class ShadowNode;
|
|
|
|
|
|
|
|
using SharedShadowNode = std::shared_ptr<const ShadowNode>;
|
|
|
|
using SharedShadowNodeList = std::vector<std::shared_ptr<const ShadowNode>>;
|
|
|
|
using SharedShadowNodeSharedList = std::shared_ptr<const SharedShadowNodeList>;
|
|
|
|
using SharedShadowNodeUnsharedList = std::shared_ptr<SharedShadowNodeList>;
|
2018-03-26 05:43:26 +00:00
|
|
|
using WeakShadowNode = std::weak_ptr<const ShadowNode>;
|
2018-03-19 02:04:10 +00:00
|
|
|
|
2018-04-27 00:51:27 +00:00
|
|
|
using ShadowNodeCloneFunction = std::function<SharedShadowNode(SharedShadowNode shadowNode, SharedProps props, SharedShadowNodeSharedList children)>;
|
|
|
|
|
2018-03-19 02:04:10 +00:00
|
|
|
class ShadowNode:
|
|
|
|
public virtual Sealable,
|
2018-04-27 00:51:27 +00:00
|
|
|
public virtual DebugStringConvertible,
|
|
|
|
public std::enable_shared_from_this<ShadowNode> {
|
2018-03-19 02:04:10 +00:00
|
|
|
public:
|
|
|
|
static SharedShadowNodeSharedList emptySharedShadowNodeSharedList();
|
|
|
|
|
|
|
|
#pragma mark - Constructors
|
|
|
|
|
|
|
|
ShadowNode(
|
2018-04-16 14:43:23 +00:00
|
|
|
const Tag &tag,
|
|
|
|
const Tag &rootTag,
|
|
|
|
const InstanceHandle &instanceHandle,
|
|
|
|
const SharedProps &props = SharedProps(),
|
2018-04-27 00:51:27 +00:00
|
|
|
const SharedShadowNodeSharedList &children = SharedShadowNodeSharedList(),
|
|
|
|
const ShadowNodeCloneFunction &cloneFunction = nullptr
|
2018-03-19 02:04:10 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
ShadowNode(
|
2018-04-16 14:43:23 +00:00
|
|
|
const SharedShadowNode &shadowNode,
|
|
|
|
const SharedProps &props = nullptr,
|
|
|
|
const SharedShadowNodeSharedList &children = nullptr
|
2018-03-19 02:04:10 +00:00
|
|
|
);
|
|
|
|
|
2018-04-27 00:51:27 +00:00
|
|
|
/*
|
|
|
|
* Clones the shadow node using stored `cloneFunction`.
|
|
|
|
*/
|
|
|
|
SharedShadowNode clone(
|
|
|
|
const SharedProps &props = nullptr,
|
|
|
|
const SharedShadowNodeSharedList &children = nullptr
|
|
|
|
) const;
|
|
|
|
|
2018-03-19 02:04:10 +00:00
|
|
|
#pragma mark - Getters
|
|
|
|
|
|
|
|
virtual ComponentHandle getComponentHandle() const = 0;
|
|
|
|
virtual ComponentName getComponentName() const = 0;
|
|
|
|
|
|
|
|
SharedShadowNodeSharedList getChildren() const;
|
|
|
|
SharedProps getProps() const;
|
|
|
|
Tag getTag() const;
|
2018-03-23 23:46:09 +00:00
|
|
|
Tag getRootTag() const;
|
|
|
|
InstanceHandle getInstanceHandle() const;
|
2018-04-10 19:46:12 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Returns the node which was used as a prototype in clone constructor.
|
|
|
|
* The node is held as a weak reference so that the method may return
|
|
|
|
* `nullptr` in cases where the node was constructed using the explicit
|
|
|
|
* constructor or the node was already deallocated.
|
|
|
|
*/
|
2018-03-19 23:51:39 +00:00
|
|
|
SharedShadowNode getSourceNode() const;
|
2018-04-10 19:46:12 +00:00
|
|
|
|
2018-03-19 23:51:39 +00:00
|
|
|
void sealRecursive() const;
|
2018-03-19 02:04:10 +00:00
|
|
|
|
|
|
|
#pragma mark - Mutating Methods
|
|
|
|
|
|
|
|
void appendChild(const SharedShadowNode &child);
|
|
|
|
void replaceChild(const SharedShadowNode &oldChild, const SharedShadowNode &newChild);
|
2018-03-19 23:51:39 +00:00
|
|
|
void clearSourceNode();
|
2018-03-19 02:04:10 +00:00
|
|
|
|
2018-04-10 19:46:04 +00:00
|
|
|
/*
|
|
|
|
* Replaces the current source node with its source node.
|
|
|
|
* This method might be used for illuminating side-effects caused by the last
|
|
|
|
* cloning operation which are not desirable from the diffing algorithm
|
|
|
|
* perspective.
|
|
|
|
*/
|
|
|
|
void shallowSourceNode();
|
|
|
|
|
2018-04-10 19:45:30 +00:00
|
|
|
#pragma mark - Equality
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Equality operators.
|
|
|
|
* Use this to compare `ShadowNode`s values for equality (and non-equality).
|
|
|
|
* Same values indicates that nodes must not produce mutation instructions
|
|
|
|
* during tree diffing process.
|
|
|
|
* Child nodes are not considered as part of the value.
|
|
|
|
*/
|
|
|
|
virtual bool operator==(const ShadowNode& rhs) const;
|
|
|
|
virtual bool operator!=(const ShadowNode& rhs) const;
|
|
|
|
|
2018-03-19 02:04:10 +00:00
|
|
|
#pragma mark - DebugStringConvertible
|
|
|
|
|
|
|
|
std::string getDebugName() const override;
|
|
|
|
std::string getDebugValue() const override;
|
|
|
|
SharedDebugStringConvertibleList getDebugChildren() const override;
|
|
|
|
SharedDebugStringConvertibleList getDebugProps() const override;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
Tag tag_;
|
2018-03-19 02:04:16 +00:00
|
|
|
Tag rootTag_;
|
2018-03-19 02:04:10 +00:00
|
|
|
InstanceHandle instanceHandle_;
|
|
|
|
SharedProps props_;
|
|
|
|
SharedShadowNodeSharedList children_;
|
2018-03-26 05:43:26 +00:00
|
|
|
WeakShadowNode sourceNode_;
|
2018-04-10 19:45:28 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
|
2018-04-27 00:51:27 +00:00
|
|
|
/*
|
|
|
|
* A reference to a cloning function that understands how to clone
|
|
|
|
* the specific type of ShadowNode.
|
|
|
|
*/
|
|
|
|
ShadowNodeCloneFunction cloneFunction_;
|
|
|
|
|
2018-04-10 19:45:28 +00:00
|
|
|
/*
|
|
|
|
* A number of the generation of the ShadowNode instance;
|
|
|
|
* is used and useful for debug-printing purposes *only*.
|
|
|
|
* Do not access this value in any circumstances.
|
|
|
|
*/
|
|
|
|
const int revision_;
|
2018-03-19 02:04:10 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace react
|
|
|
|
} // namespace facebook
|