From e21692743c6c3b1cc046a8cb7a3ecbad3b3ada9c Mon Sep 17 00:00:00 2001 From: lisyarus Date: Tue, 18 Apr 2023 15:30:30 +0300 Subject: [PATCH] Support creating react::source with an immediate subscriber --- libs/react/include/psemek/react/source.hpp | 24 ++++++++++++++++++++++ libs/react/include/psemek/react/value.hpp | 3 +++ 2 files changed, 27 insertions(+) diff --git a/libs/react/include/psemek/react/source.hpp b/libs/react/include/psemek/react/source.hpp index 087647ea..8bb50cab 100644 --- a/libs/react/include/psemek/react/source.hpp +++ b/libs/react/include/psemek/react/source.hpp @@ -22,6 +22,15 @@ namespace psemek::react set(std::move(x)); } + source(T x, typename util::signal::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::subscription_token subscription_token_; }; template <> @@ -68,11 +80,23 @@ namespace psemek::react : value(detail::internal_tag{}, std::make_shared>()) {} + source(util::signal::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::subscription_token subscribtion_token_; }; template diff --git a/libs/react/include/psemek/react/value.hpp b/libs/react/include/psemek/react/value.hpp index 9f4017eb..14fb3721 100644 --- a/libs/react/include/psemek/react/value.hpp +++ b/libs/react/include/psemek/react/value.hpp @@ -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(node_);