Support creating react::source with an immediate subscriber

This commit is contained in:
Nikita Lisitsa 2023-04-18 15:30:30 +03:00
parent 9f0179a86a
commit e21692743c
2 changed files with 27 additions and 0 deletions

View file

@ -22,6 +22,15 @@ namespace psemek::react
set(std::move(x));
}
source(T x, typename util::signal<T>::subscriber subscriber)
: source(std::move(x))
{
subscription_token_ = this->subscribe(std::move(subscriber));
}
source & operator = (source const &) = default;
source & operator = (source &&) = default;
void set(T x) const
{
this->node_->cached_value = std::nullopt;
@ -55,6 +64,9 @@ namespace psemek::react
return result;
}
private:
typename util::signal<T>::subscription_token subscription_token_;
};
template <>
@ -68,11 +80,23 @@ namespace psemek::react
: value<void>(detail::internal_tag{}, std::make_shared<detail::node<void>>())
{}
source(util::signal<void>::subscriber subscriber)
: source()
{
subscribtion_token_ = subscribe(std::move(subscriber));
}
source & operator = (source const &) = default;
source & operator = (source &&) = default;
void set() const
{
this->node_->internal_signal();
this->node_->external_signal();
}
private:
util::signal<void>::subscription_token subscribtion_token_;
};
template <typename T>

View file

@ -84,6 +84,9 @@ namespace psemek::react
: node_(node)
{}
value & operator = (value const &) = default;
value & operator = (value &&) = default;
explicit operator bool() const
{
return static_cast<bool>(node_);