-
Notifications
You must be signed in to change notification settings - Fork 37
Add behavior subject #303
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Add behavior subject #303
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,139 @@ | ||
| // ReactivePlusPlus library | ||
| // | ||
| // Copyright Aleksey Loginov 2022 - present. | ||
| // Distributed under the Boost Software License, Version 1.0. | ||
| // (See accompanying file LICENSE_1_0.txt or copy at | ||
| // https://www.boost.org/LICENSE_1_0.txt) | ||
| // | ||
| // Project home: https://github.com/victimsnino/ReactivePlusPlus | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <rpp/subjects/fwd.hpp> | ||
| #include <rpp/utils/constraints.hpp> | ||
| #include <rpp/subscribers/dynamic_subscriber.hpp> | ||
| #include <rpp/subjects/details/subject_state.hpp> | ||
| #include <rpp/subjects/details/base_subject.hpp> | ||
|
|
||
| namespace rpp::subjects::details | ||
| { | ||
| template<rpp::constraint::decayed_type T> | ||
| class behavior_strategy | ||
| { | ||
| public: | ||
| template<rpp::constraint::decayed_same_as<T> TT, rpp::constraint::decayed_same_as<composite_subscription> TSub> | ||
| behavior_strategy(TT&& v, TSub&& sub) | ||
| : m_state{std::make_shared<behavior_state>(std::forward<TT>(v))} | ||
| , m_sub{std::forward<TSub>(sub)} | ||
| { | ||
| m_sub.add([state = std::weak_ptr{m_state}] | ||
| { | ||
| if (const auto locked = state.lock()) | ||
| locked->on_unsubscribe(); | ||
| }); | ||
| } | ||
|
|
||
| void on_subscribe(const dynamic_subscriber<T>& sub) const | ||
| { | ||
| if (m_sub.is_subscribed()) | ||
| sub.on_next(m_state->get_value()); | ||
|
|
||
| m_state->on_subscribe(sub); | ||
| } | ||
|
|
||
| auto get_subscriber() const | ||
| { | ||
| return rpp::make_specific_subscriber<T>(m_sub, | ||
| [state = m_state](const T& v) | ||
| { | ||
| state->set_value(v); | ||
| state->on_next(v); | ||
| }, | ||
| [state = m_state](const std::exception_ptr& err) | ||
| { | ||
| state->on_error(err); | ||
| }, | ||
| [state = m_state]() | ||
| { | ||
| state->on_completed(); | ||
| }); | ||
| } | ||
|
|
||
| T get_value() const | ||
| { | ||
| return m_state->get_value(); | ||
| } | ||
|
|
||
| private: | ||
| class behavior_state : public subject_state<T> | ||
| { | ||
| public: | ||
| behavior_state(const T& v) | ||
| : subject_state<T>{} | ||
| , value{v} {} | ||
|
|
||
| behavior_state(T&& v) | ||
| : subject_state<T>{} | ||
| , value{std::move(v)} {} | ||
|
|
||
| T get_value() | ||
| { | ||
| std::lock_guard lock{mutex}; | ||
| return value; | ||
| } | ||
|
|
||
|
|
||
| void set_value(const T& v) | ||
| { | ||
| std::lock_guard lock{mutex}; | ||
| value = v; | ||
| } | ||
|
|
||
| private: | ||
|
|
||
| std::mutex mutex; | ||
| T value; | ||
| }; | ||
|
|
||
| std::shared_ptr<behavior_state> m_state; | ||
| composite_subscription m_sub{}; | ||
| }; | ||
| } // namespace rpp::subjects::details | ||
|
|
||
| namespace rpp::subjects | ||
| { | ||
| /** | ||
| * \brief Subject which multicasts values to observers subscribed on it and sends last emitted value (or initial value) on subscribe. It contains two parts: subscriber and observable at the same time. | ||
| * | ||
| * \details Each subscriber obtains only last/initial value + values which emitted after corresponding subscribe. on_error/on_completer/unsubscribe cached and provided to new subscribers if any | ||
| * | ||
| * \warning this subject is not synchronized/serialized! It means, that expected to call callbacks of subscriber in the serialized way to follow observable contract: "Observables must issue notifications to observers serially (not in parallel).". If you are not sure or need extra serialization, please, use serialized_subject. | ||
| * | ||
| * \tparam T value provided by this subject | ||
| * | ||
| * \ingroup subjects | ||
| * \see https://reactivex.io/documentation/subject.html | ||
| */ | ||
| template<rpp::constraint::decayed_type T> | ||
| class behavior_subject final : public details::base_subject<T, details::behavior_strategy<T>> | ||
| { | ||
| public: | ||
| behavior_subject(const T& initial_value, const composite_subscription& sub) | ||
| : details::base_subject<T, details::behavior_strategy<T>>{initial_value, sub} {} | ||
|
|
||
| behavior_subject(T&& initial_value, const composite_subscription& sub) | ||
| : details::base_subject<T, details::behavior_strategy<T>>{std::move(initial_value), sub} {} | ||
|
|
||
| behavior_subject(const T& initial_value, composite_subscription&& sub = composite_subscription{}) | ||
| : details::base_subject<T, details::behavior_strategy<T>>{initial_value, std::move(sub)} {} | ||
|
|
||
| behavior_subject(T&& initial_value, composite_subscription&& sub = composite_subscription{}) | ||
| : details::base_subject<T, details::behavior_strategy<T>>{std::move(initial_value), std::move(sub)} {} | ||
|
|
||
|
|
||
| T get_value() const | ||
| { | ||
| return details::base_subject<T, details::behavior_strategy<T>>::get_strategy().get_value(); | ||
| } | ||
| }; | ||
| } // namespace rpp::subjects |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
constructor accepting a forwarding reference can hide the copy and move constructors