Support subscribing to react::value forever
This commit is contained in:
parent
018f3ae0b0
commit
546c0f2a7b
1 changed files with 20 additions and 7 deletions
|
|
@ -17,6 +17,9 @@ namespace psemek::react
|
||||||
struct internal_tag
|
struct internal_tag
|
||||||
{};
|
{};
|
||||||
|
|
||||||
|
struct forever_tag
|
||||||
|
{};
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
struct node
|
struct node
|
||||||
{
|
{
|
||||||
|
|
@ -65,6 +68,8 @@ namespace psemek::react
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static constexpr detail::forever_tag forever;
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
struct value
|
struct value
|
||||||
{
|
{
|
||||||
|
|
@ -105,16 +110,16 @@ namespace psemek::react
|
||||||
|
|
||||||
[[nodiscard]] auto subscribe(typename detail::node<T>::external_subscriber subscriber, bool call = false) const
|
[[nodiscard]] auto subscribe(typename detail::node<T>::external_subscriber subscriber, bool call = false) const
|
||||||
{
|
{
|
||||||
if (call)
|
if (call) this->call(subscriber);
|
||||||
{
|
|
||||||
if constexpr (std::is_same_v<T, void>)
|
|
||||||
subscriber();
|
|
||||||
else
|
|
||||||
subscriber(node_->value());
|
|
||||||
}
|
|
||||||
return node_->external_signal.subscribe(std::move(subscriber));
|
return node_->external_signal.subscribe(std::move(subscriber));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void subscribe(typename detail::node<T>::external_subscriber subscriber, detail::forever_tag, bool call = false) const
|
||||||
|
{
|
||||||
|
if (call) this->call(subscriber);
|
||||||
|
node_->external_signal.subscribe_forever(std::move(subscriber));
|
||||||
|
}
|
||||||
|
|
||||||
[[nodiscard]] auto subscribe(detail::internal_tag, std::function<void()> subscriber) const
|
[[nodiscard]] auto subscribe(detail::internal_tag, std::function<void()> subscriber) const
|
||||||
{
|
{
|
||||||
return node_->internal_signal.subscribe(std::move(subscriber));
|
return node_->internal_signal.subscribe(std::move(subscriber));
|
||||||
|
|
@ -122,6 +127,14 @@ namespace psemek::react
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
std::shared_ptr<detail::node<T>> node_;
|
std::shared_ptr<detail::node<T>> node_;
|
||||||
|
|
||||||
|
void call(typename detail::node<T>::external_subscriber const & subscriber) const
|
||||||
|
{
|
||||||
|
if constexpr (std::is_same_v<T, void>)
|
||||||
|
subscriber();
|
||||||
|
else
|
||||||
|
subscriber(node_->value());
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue