ReactivePlusPlus
ReactiveX implementation for C++20
Loading...
Searching...
No Matches
timeout.hpp
1// ReactivePlusPlus library
2//
3// Copyright Aleksey Loginov 2023 - present.
4// Distributed under the Boost Software License, Version 1.0.
5// (See accompanying file LICENSE_1_0.txt or copy at
6// https://www.boost.org/LICENSE_1_0.txt)
7//
8// Project home: https://github.com/AlexInLog/ReactivePlusPlus
9//
10
11#pragma once
12
13#include <rpp/operators/fwd.hpp>
14
15#include <rpp/operators/details/strategy.hpp>
16#include <rpp/sources/error.hpp>
17#include <rpp/utils/exceptions.hpp>
18#include <rpp/utils/utils.hpp>
19
20namespace rpp::operators::details
21{
22 template<rpp::constraint::observer TObserver, rpp::constraint::observable TFallbackObservable, rpp::details::disposables::constraint::disposables_container Container>
23 class timeout_disposable final : public rpp::composite_disposable_impl<Container>
24 {
25 public:
27 {
28 RPP_NO_UNIQUE_ADDRESS TObserver observer;
29 rpp::schedulers::time_point timeout;
30 };
31
32 timeout_disposable(TObserver&& observer, rpp::schedulers::duration period, const TFallbackObservable& fallback, rpp::schedulers::time_point timeout)
33 : m_observer_with_timeout{observer_with_timeout{std::move(observer), timeout}}
34 , m_period{period}
35 , m_fallback{fallback}
36 {
37 }
38 rpp::utils::pointer_under_lock<observer_with_timeout> get_observer_with_timeout_under_lock() { return m_observer_with_timeout; }
39
40 const TFallbackObservable& get_fallback() const { return m_fallback; }
41
42 rpp::schedulers::duration get_period() const { return m_period; }
43
44 private:
46
47 const rpp::schedulers::duration m_period;
48 RPP_NO_UNIQUE_ADDRESS const TFallbackObservable m_fallback;
49 };
50
51 template<rpp::constraint::observer TObserver, rpp::constraint::observable TFallbackObservable, rpp::details::disposables::constraint::disposables_container Container>
53 {
54 std::shared_ptr<timeout_disposable<TObserver, TFallbackObservable, Container>> disposable;
55
56 bool is_disposed() const { return disposable->is_disposed(); }
57
58 void on_error(const std::exception_ptr& err) const
59 {
60 disposable->get_observer_with_timeout_under_lock()->observer.on_error(err);
61 }
62 };
63
64 template<rpp::constraint::observer TObserver, rpp::constraint::observable TFallbackObservable, rpp::details::disposables::constraint::disposables_container Container, rpp::schedulers::constraint::scheduler TScheduler>
66 {
67 static constexpr auto preferred_disposables_mode = rpp::details::observers::disposables_mode::None;
68
69 std::shared_ptr<timeout_disposable<TObserver, TFallbackObservable, Container>> disposable;
70
71 void set_upstream(const rpp::disposable_wrapper& d) const
72 {
73 disposable->add(d);
74 }
75
76 bool is_disposed() const
77 {
78 return disposable->is_disposed();
79 }
80
81 template<typename T>
82 void on_next(T&& v) const
83 {
84 auto obs_with_timeout = disposable->get_observer_with_timeout_under_lock();
85 // the timer may have disposed and handed the observer to the fallback while we waited for the lock
86 if (disposable->is_disposed())
87 return;
88 obs_with_timeout->observer.on_next(std::forward<T>(v));
89 obs_with_timeout->timeout = rpp::schedulers::utils::get_worker_t<TScheduler>::now() + disposable->get_period();
90 }
91
92 void on_error(const std::exception_ptr& err) const noexcept
93 {
94 const auto obs_with_timeout = disposable->get_observer_with_timeout_under_lock();
95 if (!disposable->is_disposed())
96 obs_with_timeout->observer.on_error(err);
97 }
98
99 void on_completed() const noexcept
100 {
101 const auto obs_with_timeout = disposable->get_observer_with_timeout_under_lock();
102 if (!disposable->is_disposed())
103 obs_with_timeout->observer.on_completed();
104 }
105 };
106
107 template<rpp::constraint::observable TFallbackObservable, rpp::schedulers::constraint::scheduler TScheduler>
109 {
110 template<rpp::constraint::decayed_type T>
112 {
113 static_assert(rpp::constraint::observable_of_type<TFallbackObservable, T>, "TFallbackObservable should be the same type as T");
114
115 using result_type = T;
116
117 constexpr static bool own_current_queue = true;
118 };
119
120 template<rpp::details::observables::constraint::disposables_strategy Prev>
121 using updated_optimal_disposables_strategy = rpp::details::observables::fixed_disposables_strategy<1>;
122
123 rpp::schedulers::duration period;
124 RPP_NO_UNIQUE_ADDRESS TFallbackObservable fallback;
125 RPP_NO_UNIQUE_ADDRESS TScheduler scheduler;
126
127 template<rpp::constraint::decayed_type Type, rpp::details::observables::constraint::disposables_strategy DisposableStrategy, rpp::constraint::observer Observer>
128 auto lift_with_disposables_strategy(Observer&& observer) const
129 {
130 using worker_t = rpp::schedulers::utils::get_worker_t<TScheduler>;
131 using container = typename DisposableStrategy::disposables_container;
132
133 const auto timeout = worker_t::now() + period;
134
135 const auto disposable = disposable_wrapper_impl<timeout_disposable<std::decay_t<Observer>, TFallbackObservable, container>>::make(std::forward<Observer>(observer), period, fallback, timeout);
136 auto ptr = disposable.lock();
137 ptr->get_observer_with_timeout_under_lock()->observer.set_upstream(disposable.as_weak());
138
139 const auto worker = scheduler.create_worker();
140 using wrapper = timeout_disposable_wrapper<std::decay_t<Observer>, TFallbackObservable, container>;
141 worker.schedule(
142 timeout,
143 [](wrapper& handler) -> rpp::schedulers::optional_delay_to {
144 auto locked_obs_with_timeout = handler.disposable->get_observer_with_timeout_under_lock();
145 if (rpp::schedulers::utils::get_worker_t<TScheduler>::now() < locked_obs_with_timeout->timeout)
146 return rpp::schedulers::delay_to(locked_obs_with_timeout->timeout);
147
148 if (!handler.disposable->is_disposed())
149 {
150 handler.disposable->dispose();
151 handler.disposable->get_fallback().subscribe(std::move(locked_obs_with_timeout->observer));
152 }
153 return std::nullopt;
154 },
155 wrapper{ptr});
156
157 return rpp::observer<Type, timeout_observer_strategy<std::decay_t<Observer>, TFallbackObservable, container, TScheduler>>{std::move(ptr)};
158 }
159 };
160
161 template<rpp::schedulers::constraint::scheduler TScheduler>
163 {
164 template<rpp::constraint::decayed_type T>
166 {
167 using result_type = T;
168
169 constexpr static bool own_current_queue = true;
170 };
171
172 template<rpp::details::observables::constraint::disposables_strategy Prev>
173 using updated_optimal_disposables_strategy = typename timeout_t<rpp::error_observable<int>, TScheduler>::template updated_optimal_disposables_strategy<Prev>;
174
175 rpp::schedulers::duration period;
176 RPP_NO_UNIQUE_ADDRESS TScheduler scheduler;
177
178 template<rpp::constraint::decayed_type Type, rpp::details::observables::constraint::disposables_strategy DisposableStrategy, rpp::constraint::observer Observer>
179 auto lift_with_disposables_strategy(Observer&& observer) const
180 {
181 return timeout_t<rpp::error_observable<Type>, TScheduler>{period, rpp::source::error<rpp::utils::extract_observer_type_t<Observer>>(std::make_exception_ptr(rpp::utils::timeout_reached{"Timeout reached"})), scheduler}
182 .template lift_with_disposables_strategy<Type, DisposableStrategy>(std::forward<Observer>(observer));
183 }
184 };
185} // namespace rpp::operators::details
186
187namespace rpp::operators
188{
206 * @ingroup utility_operators
207 * @see https://reactivex.io/documentation/operators/timeout.html
208 */
209 template<rpp::constraint::observable TFallbackObservable, rpp::schedulers::constraint::scheduler TScheduler>
210 auto timeout(rpp::schedulers::duration period, TFallbackObservable&& fallback_observable, const TScheduler& scheduler)
211 {
212 return details::timeout_t<std::decay_t<TFallbackObservable>, TScheduler>{period, std::forward<TFallbackObservable>(fallback_observable), scheduler};
213 }
214
227 * @par Example
228 * @snippet timeout.cpp default
229 *
230 * @ingroup utility_operators
231 * @see https://reactivex.io/documentation/operators/timeout.html
232 */
233 template<rpp::schedulers::constraint::scheduler TScheduler>
234 auto timeout(rpp::schedulers::duration period, const TScheduler& scheduler)
235 {
236 return details::timeout_with_error_t<TScheduler>{period, scheduler};
237 }
238} // namespace rpp::operators
Disposable which can keep some other sub-disposables. When this root disposable is disposed,...
Definition composite_disposable.hpp:31
Main RPP wrapper over disposables.
Definition disposable_wrapper.hpp:142
Base class for any observer used in RPP. It handles core callbacks of observers. Objects of this clas...
Definition observer.hpp:172
Definition utils.hpp:260
auto error(std::exception_ptr err)
Creates rpp::observable that emits no items and terminates with an error.
Definition error.hpp:49
disposable_wrapper_impl< interface_disposable > disposable_wrapper
Wrapper to keep "simple" disposable. Specialization of rpp::disposable_wrapper_impl.
Definition fwd.hpp:34
auto timeout(rpp::schedulers::duration period, TFallbackObservable &&fallback_observable, const TScheduler &scheduler)
Forwards emissions from original observable, but subscribes on fallback observable if no any events d...
Definition timeout.hpp:206
Definition disposables_strategy.hpp:29
Definition timeout.hpp:109
Provide timepoint of next execution explicitly.
Definition fwd.hpp:70
Definition exceptions.hpp:28