Files
react-native/ReactCommon/react/renderer/core/State.h
T
Andres Suarez 8bd3edec88 Update copyright headers from Facebook to Meta
Reviewed By: aaronabramov

Differential Revision: D33367752

fbshipit-source-id: 4ce94d184485e5ee0a62cf67ad2d3ba16e285c8f
2021-12-30 15:11:21 -08:00

115 lines
3.0 KiB
C++
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/*
* 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
#ifdef ANDROID
#include <folly/dynamic.h>
#include <react/renderer/mapbuffer/MapBuffer.h>
#include <react/renderer/mapbuffer/MapBufferBuilder.h>
#endif
#include <react/renderer/core/ShadowNodeFamily.h>
namespace facebook {
namespace react {
/*
* An abstract interface of State.
* State is used to control and continuously advance a single vision of some
* state (arbitrary data) associated with a family of shadow nodes.
*/
class State {
public:
using Shared = std::shared_ptr<const State>;
static size_t constexpr initialRevisionValue = 1;
protected:
/*
* Constructors are protected to make calling them directly with
* type-erasured arguments impossible.
*/
explicit State(StateData::Shared data, State const &state);
explicit State(
StateData::Shared data,
ShadowNodeFamily::Shared const &family);
public:
virtual ~State() = default;
/*
  * Returns a momentary value of the most recently committed state
  * associated with a family of nodes which this state belongs to.
  * Sequential calls might return different values.
*/
State::Shared getMostRecentState() const;
/*
* Returns the most recent state (same as `getMostRecentState()` method)
* if this state is obsolete, otherwise returns `nullptr`.
*/
State::Shared getMostRecentStateIfObsolete() const;
/*
* Returns a revision number of the `State` object.
* The number is being automatically assigned during the creation of `State`
* objects.
* Revision `0` represents a case when we don't have any info about state
* object (actual State instances cannot have it).
* Revision `1` represents a newly created initial state object.
*/
size_t getRevision() const;
#ifdef ANDROID
virtual folly::dynamic getDynamic() const = 0;
virtual MapBuffer getMapBuffer() const = 0;
virtual void updateState(folly::dynamic data) const = 0;
#endif
protected:
friend class ShadowNodeFamily;
friend class UIManager;
/*
* Returns a shared pointer to data.
* To be used by `UIManager` only.
*/
StateData::Shared const &getDataPointer() const {
return data_;
}
/*
* A family of a node with this state is associated.
* Must be a weak pointer to avoid retain cycle among `State`, `ShadowNode`,
* and `ShadowNodeFamily` instances.
*/
ShadowNodeFamily::Weak family_;
/*
* Type-erasured pointer to arbitrary component-specific data held by the
* `State`.
*/
StateData::Shared data_;
/*
* Indicates that the state was committed once and then was replaced by a
* newer one.
* To be used by `StateCoordinator` only.
* Protected by mutex inside `StateCoordinator`.
*/
mutable bool isObsolete_{false};
/*
* Revision of the State object.
*/
size_t revision_;
};
} // namespace react
} // namespace facebook