-
Notifications
You must be signed in to change notification settings - Fork 37
Add "sample_with_time" operator #280
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
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
d224db3
Sample prototype
AlexInLog 4725698
simplify delay
AlexInLog d89324d
start tests for sample
AlexInLog fdb5793
tests + fixes
AlexInLog 35185c5
Add docs
AlexInLog 1d25553
status
AlexInLog e9c503f
fix interval tests
AlexInLog 9f9b3e5
Update spinlock.hpp
AlexInLog 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| #include <rpp/rpp.hpp> | ||
| #include <iostream> | ||
|
|
||
| /** | ||
| * \example sample.cpp | ||
| **/ | ||
| int main() | ||
| { | ||
| //! [sample_with_time] | ||
| rpp::source::interval(std::chrono::milliseconds{100}, rpp::schedulers::trampoline{}) | ||
| .sample_with_time(std::chrono::milliseconds{300}, rpp::schedulers::trampoline{}) | ||
| .take(5) | ||
| .subscribe([](int v) { std::cout << v << " "; }); | ||
| // Output: 1 4 7 10 13 | ||
| //! [sample_with_time] | ||
| return 0; | ||
| } |
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
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,63 @@ | ||
| // 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/observables/details/member_overload.hpp> | ||
| #include <rpp/schedulers/constraints.hpp> | ||
| #include <rpp/schedulers/trampoline_scheduler.hpp> | ||
|
|
||
| namespace rpp::details | ||
| { | ||
| struct sample_tag; | ||
| } | ||
|
|
||
| namespace rpp::details | ||
| { | ||
| template<constraint::decayed_type Type, schedulers::constraint::scheduler TScheduler> | ||
| struct sample_with_time_impl; | ||
|
|
||
| template<constraint::decayed_type Type, typename SpecificObservable> | ||
| struct member_overload<Type, SpecificObservable, sample_tag> | ||
| { | ||
| /** | ||
| * \brief Emit most recent emitted from original observable emission obtained during last period of time. | ||
| * \details Emit item immediately in case of completion of the original observable | ||
| * | ||
| * \marble sample_with_time | ||
| { | ||
| source observable : +--1---2-3-4---5-6-7-| | ||
| operator "sample_with_time(2)" : +--1---2---4---5---7-| | ||
| } | ||
| * | ||
| * \param period sampling period | ||
| * \scheduler scheduler to use to schedule emissions with provided sampling period | ||
| * \return new specific_observable with the sample_with_time operator as most recent operator. | ||
| * \warning #include <rpp/operators/sample.hpp> | ||
| * | ||
| * \par Example | ||
| * \snippet sample.cpp sample_with_time | ||
| * | ||
| * \ingroup filtering_operators | ||
| * \see https://reactivex.io/documentation/operators/sample.htmlhttps://reactivex.io/documentation/operators/sample.html | ||
| */ | ||
| template<schedulers::constraint::scheduler TScheduler, typename ...Args> | ||
| auto sample_with_time(schedulers::duration period, const TScheduler& scheduler) const & requires is_header_included<sample_tag, TScheduler, Args...> | ||
| { | ||
| return static_cast<const SpecificObservable*>(this)->template lift<Type>(sample_with_time_impl<Type, TScheduler>{period, scheduler}); | ||
| } | ||
|
|
||
| template<schedulers::constraint::scheduler TScheduler, typename ...Args> | ||
| auto sample_with_time(schedulers::duration period, const TScheduler& scheduler) && requires is_header_included<sample_tag, TScheduler, Args...> | ||
| { | ||
| return std::move(*static_cast<SpecificObservable*>(this)).template lift<Type>(sample_with_time_impl<Type, TScheduler>{period, scheduler}); | ||
| } | ||
| }; | ||
| } // namespace rpp::details |
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,106 @@ | ||
| // 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/operators/details/early_unsubscribe.hpp> | ||
| #include <rpp/operators/details/serialized_subscriber.hpp> | ||
| #include <rpp/operators/fwd/sample.hpp> | ||
| #include <rpp/subscribers/constraints.hpp> | ||
| #include <rpp/utils/spinlock.hpp> | ||
|
|
||
| #include <mutex> | ||
| #include <optional> | ||
|
|
||
| IMPLEMENTATION_FILE(sample_tag); | ||
|
|
||
| namespace rpp::details | ||
| { | ||
| template<constraint::decayed_type Type> | ||
| struct sample_state : early_unsubscribe_state | ||
| { | ||
| using early_unsubscribe_state::early_unsubscribe_state; | ||
|
|
||
| std::mutex value_mutex{}; | ||
| std::optional<Type> value{}; | ||
| }; | ||
|
|
||
| template<constraint::decayed_type Type> | ||
| struct sample_state_with_serialized_spinlock : sample_state<Type> | ||
| { | ||
| using sample_state<Type>::sample_state; | ||
|
|
||
| utils::spinlock spinlock{}; | ||
| }; | ||
|
|
||
| struct sample_on_next | ||
| { | ||
| template<typename Value> | ||
| void operator()(Value&& value, const auto&, const std::shared_ptr<sample_state<std::decay_t<Value>>>& state) const | ||
| { | ||
| std::lock_guard lock{state->value_mutex}; | ||
| state->value.emplace(std::forward<Value>(value)); | ||
| } | ||
| }; | ||
|
|
||
| using sample_on_error = early_unsubscribe_on_error; | ||
|
|
||
| struct sample_on_completed | ||
| { | ||
| void operator()(const auto& subscriber, const auto& state) const | ||
| { | ||
| state->children_subscriptions.unsubscribe(); | ||
|
|
||
| { | ||
| std::lock_guard lock{state->value_mutex}; | ||
| if (state->value.has_value()) | ||
| subscriber.on_next(std::move(state->value.value())); | ||
| } | ||
| subscriber.on_completed(); | ||
| } | ||
| }; | ||
|
|
||
| template<constraint::decayed_type Type, schedulers::constraint::scheduler TScheduler> | ||
| struct sample_with_time_impl | ||
| { | ||
| schedulers::duration period; | ||
| TScheduler scheduler; | ||
|
|
||
| template<constraint::subscriber_of_type<Type> TSub> | ||
| auto operator()(TSub&& in_subscriber) const | ||
| { | ||
| auto state = std::make_shared<sample_state_with_serialized_spinlock<Type>>(in_subscriber.get_subscription()); | ||
| // change subscriber to serialized to avoid manual using of mutex | ||
| auto subscriber = make_serialized_subscriber(std::forward<TSub>(in_subscriber), | ||
| std::shared_ptr<utils::spinlock>{state, &state->spinlock}); | ||
|
|
||
| scheduler.create_worker(state->children_subscriptions) | ||
| .schedule(period, | ||
| [period = period, subscriber = subscriber, state]() -> rpp::schedulers::optional_duration | ||
| { | ||
| std::optional<Type> extracted{}; | ||
| { | ||
| std::lock_guard lock{state->value_mutex}; | ||
| std::swap(extracted, state->value); | ||
| } | ||
| if (extracted.has_value()) | ||
| subscriber.on_next(std::move(extracted.value())); | ||
| return period; | ||
| }); | ||
|
|
||
| return create_subscriber_with_state<Type>(state->children_subscriptions, | ||
| sample_on_next{}, | ||
| sample_on_error{}, | ||
| sample_on_completed{}, | ||
| std::move(subscriber), | ||
| std::move(state)); | ||
| } | ||
| }; | ||
| } // namespace rpp::details |
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
Oops, something went wrong.
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.
no member named
this_threadin namespacestd