-
Notifications
You must be signed in to change notification settings - Fork 37
Finally operator #531
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
+180
−0
Merged
Finally operator #531
Changes from all commits
Commits
Show all changes
3 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| // ReactivePlusPlus library | ||
| // | ||
| // Copyright Aleksey Loginov 2023 - 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/fwd.hpp> | ||
|
|
||
| #include <rpp/defs.hpp> | ||
| #include <rpp/operators/details/strategy.hpp> | ||
|
|
||
| namespace rpp::operators::details | ||
| { | ||
| template<rpp::constraint::decayed_type LastFn> | ||
| struct finally_t | ||
| { | ||
| template<rpp::constraint::decayed_type T> | ||
| struct operator_traits | ||
| { | ||
| using result_type = T; | ||
| }; | ||
|
|
||
| template<rpp::details::observables::constraint::disposable_strategy Prev> | ||
| using updated_disposable_strategy = typename Prev::template add<1>; | ||
|
|
||
| RPP_NO_UNIQUE_ADDRESS LastFn last_fn; | ||
|
|
||
| template<rpp::constraint::decayed_type Type, rpp::constraint::observer Observer> | ||
| auto lift(Observer&& observer) const | ||
| { | ||
| observer.set_upstream(make_callback_disposable(last_fn)); | ||
| return std::forward<Observer>(observer); | ||
| } | ||
| }; | ||
| } // namespace rpp::operators::details | ||
|
|
||
| namespace rpp::operators | ||
| { | ||
| /** | ||
| * @brief Register callback to be called when execution is done and disposable bound to observer is disposed | ||
| * | ||
| * @param last_fn action callback | ||
| * @warning #include <rpp/operators/finally.hpp> | ||
| * | ||
| * @details action callback needs to be noexcept as it is called on dispose, throwing during this time could potentially break internal disposable state. | ||
| * | ||
| * @ingroup utility_operators | ||
| * @see https://reactivex.io/documentation/operators/do.html | ||
| */ | ||
| template<std::invocable<> LastFn> | ||
| auto finally(LastFn&& last_fn) | ||
| { | ||
| return details::finally_t<std::decay_t<LastFn>>{std::forward<LastFn>(last_fn)}; | ||
| } | ||
| } // namespace rpp::operators |
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,115 @@ | ||
| // ReactivePlusPlus library | ||
| // | ||
| // Copyright Aleksey Loginov 2023 - 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 | ||
| // | ||
|
|
||
| #include <snitch/snitch.hpp> | ||
|
|
||
| #include <rpp/operators/finally.hpp> | ||
| #include <rpp/sources/create.hpp> | ||
|
|
||
| #include "disposable_observable.hpp" | ||
| #include "mock_observer.hpp" | ||
|
|
||
| TEST_CASE("finally executes only at the end") | ||
| { | ||
| auto mock = mock_observer_strategy<int>(); | ||
| SECTION("observable with no emissions") | ||
| { | ||
| auto obs = rpp::source::create<int>([](const auto&) { | ||
| }); | ||
| size_t invoked = 0; | ||
| SECTION("subscribe") | ||
| { | ||
| obs | rpp::operators::finally([&]() noexcept { ++invoked; }) | ||
| | rpp::ops::subscribe(mock); | ||
| SECTION("observer obtains values from observable") | ||
| { | ||
| CHECK(invoked == 1); | ||
| CHECK(mock.get_total_on_next_count() == 0); | ||
| CHECK(mock.get_on_error_count() == 0); | ||
| CHECK(mock.get_on_completed_count() == 0); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| SECTION("observable with on_completed emission") | ||
| { | ||
| auto obs = rpp::source::create<int>([](const auto& sub) { | ||
| sub.on_completed(); | ||
| }); | ||
| size_t invoked = 0; | ||
| SECTION("subscribe") | ||
| { | ||
| obs | rpp::operators::finally([&]() noexcept { ++invoked; }) | ||
| | rpp::ops::subscribe(mock); | ||
| SECTION("observer obtains values from observable") | ||
| { | ||
| CHECK(invoked == 1); | ||
| CHECK(mock.get_total_on_next_count() == 0); | ||
| CHECK(mock.get_on_error_count() == 0); | ||
| CHECK(mock.get_on_completed_count() == 1); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| SECTION("observable with on_next emission") | ||
| { | ||
| auto obs = rpp::source::create<int>([](const auto& sub) { | ||
| sub.on_next(1); | ||
| sub.on_completed(); | ||
| }); | ||
| size_t invoked = 0; | ||
| SECTION("subscribe") | ||
| { | ||
| obs | rpp::operators::finally([&]() noexcept { ++invoked; }) | ||
| | rpp::ops::subscribe(mock); | ||
| SECTION("observer obtains values from observable") | ||
| { | ||
| CHECK(invoked == 1); | ||
| CHECK(mock.get_total_on_next_count() == 1); | ||
| CHECK(mock.get_on_error_count() == 0); | ||
| CHECK(mock.get_on_completed_count() == 1); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| SECTION("observable with on_error emission") | ||
| { | ||
| auto obs = rpp::source::create<int>([](const auto& sub) { | ||
| sub.on_next(1); | ||
| sub.on_error(std::make_exception_ptr(std::runtime_error{""})); | ||
| }); | ||
| size_t invoked = 0; | ||
| SECTION("subscribe") | ||
| { | ||
| obs | rpp::operators::finally([&]() noexcept { ++invoked; }) | ||
| | rpp::ops::subscribe(mock); | ||
| SECTION("observer obtains values from observable") | ||
| { | ||
| CHECK(invoked == 1); | ||
| CHECK(mock.get_total_on_next_count() == 1); | ||
| CHECK(mock.get_on_error_count() == 1); | ||
| CHECK(mock.get_on_completed_count() == 0); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| TEST_CASE("finally satisfies disposable contracts") | ||
| { | ||
| auto observable_disposable = rpp::composite_disposable_wrapper::make(); | ||
| { | ||
| auto observable = observable_with_disposable<int>(observable_disposable); | ||
|
|
||
| test_operator_with_disposable<int>( | ||
| rpp::ops::finally([]() noexcept {})); | ||
| } | ||
|
|
||
| CHECK(observable_disposable.is_disposed() || observable_disposable.lock().use_count() == 2); | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.