37 #if !defined(RXCPP_OPERATORS_RX_TAKE_UNTIL_HPP) 38 #define RXCPP_OPERATORS_RX_TAKE_UNTIL_HPP 40 #include "../rx-includes.hpp" 49 struct take_until_invalid_arguments {};
52 struct take_until_invalid :
public rxo::operator_base<take_until_invalid_arguments<AN...>> {
53 using type = observable<take_until_invalid_arguments<
AN...>, take_until_invalid<
AN...>>;
56 using take_until_invalid_t =
typename take_until_invalid<
AN...>::type;
58 template<
class T,
class Observable,
class TriggerObservable,
class Coordination>
61 typedef rxu::decay_t<Observable> source_type;
62 typedef rxu::decay_t<TriggerObservable> trigger_source_type;
63 typedef rxu::decay_t<Coordination> coordination_type;
64 typedef typename coordination_type::coordinator_type coordinator_type;
67 values(source_type s, trigger_source_type t, coordination_type sf)
68 : source(std::move(s))
69 , trigger(std::move(t))
70 , coordination(std::move(sf))
74 trigger_source_type trigger;
75 coordination_type coordination;
79 take_until(source_type s, trigger_source_type t, coordination_type sf)
80 : initial(std::move(s), std::move(t), std::move(sf))
95 template<
class Subscriber>
96 void on_subscribe(Subscriber s)
const {
98 typedef Subscriber output_type;
99 struct take_until_state_type
100 :
public std::enable_shared_from_this<take_until_state_type>
103 take_until_state_type(
const values& i, coordinator_type coor,
const output_type& oarg)
105 , mode_value(mode::taking)
106 , coordinator(std::move(coor))
109 out.add(trigger_lifetime);
110 out.add(source_lifetime);
112 typename mode::type mode_value;
113 composite_subscription trigger_lifetime;
114 composite_subscription source_lifetime;
115 coordinator_type coordinator;
119 auto coordinator = initial.coordination.create_coordinator(s.get_subscription());
122 auto state = std::make_shared<take_until_state_type>(initial, std::move(coordinator), std::move(s));
125 [&](){
return state->coordinator.in(state->trigger);},
127 if (trigger.empty()) {
132 [&](){
return state->coordinator.in(state->source);},
134 if (source.empty()) {
138 auto sinkTrigger = make_subscriber<typename trigger_source_type::value_type>(
142 state->trigger_lifetime,
144 [state](
const typename trigger_source_type::value_type&) {
145 if (state->mode_value != mode::taking) {
return;}
146 state->mode_value = mode::triggered;
147 state->out.on_completed();
150 [state](std::exception_ptr e) {
151 if (state->mode_value != mode::taking) {
return;}
152 state->mode_value = mode::errored;
153 state->out.on_error(e);
157 if (state->mode_value != mode::taking) {
return;}
158 state->mode_value = mode::clear;
162 [&](){
return state->coordinator.out(sinkTrigger);},
164 if (selectedSinkTrigger.empty()) {
167 trigger->subscribe(std::move(selectedSinkTrigger.get()));
169 auto sinkSource = make_subscriber<T>(
171 state->source_lifetime,
177 if (state->mode_value < mode::triggered) {
178 state->out.on_next(t);
182 [state](std::exception_ptr e) {
183 if (state->mode_value > mode::clear) {
return;}
184 state->mode_value = mode::errored;
185 state->out.on_error(e);
189 if (state->mode_value > mode::clear) {
return;}
190 state->mode_value = mode::stopped;
191 state->out.on_completed();
195 [&](){
return state->coordinator.out(sinkSource);},
197 if (selectedSinkSource.empty()) {
200 source->subscribe(std::move(selectedSinkSource.get()));
208 template<
class...
AN>
211 return operator_factory<take_until_tag,
AN...>(std::make_tuple(std::forward<AN>(an)...));
219 template<
class Observable,
class TimePoint,
222 std::is_convertible<TimePoint, rxsc::scheduler::clock_type::time_point>>,
227 class TakeUntil = rxo::detail::take_until<SourceValue, rxu::decay_t<Observable>, TriggerObservable,
identity_one_worker>,
230 static Result
member(Observable&& o, TimePoint&& when) {
232 return Result(TakeUntil(std::forward<Observable>(o),
rxs::timer(std::forward<TimePoint>(when), cn), cn));
235 template<
class Observable,
class TimePoint,
class Coordination,
237 is_observable<Observable>,
239 std::is_convertible<TimePoint, rxsc::scheduler::clock_type::time_point>>,
244 class TakeUntil = rxo::detail::take_until<SourceValue, rxu::decay_t<Observable>, TriggerObservable,
rxu::decay_t<Coordination>>,
247 static Result
member(Observable&& o, TimePoint&& when, Coordination cn) {
248 return Result(TakeUntil(std::forward<Observable>(o),
rxs::timer(std::forward<TimePoint>(when), cn), cn));
251 template<
class Observable,
class TriggerObservable,
258 static Result
member(Observable&& o, TriggerObservable&& t) {
259 return Result(TakeUntil(std::forward<Observable>(o), std::forward<TriggerObservable>(t),
identity_current_thread()));
262 template<
class Observable,
class TriggerObservable,
class Coordination,
264 all_observables<Observable, TriggerObservable>,
265 is_coordination<Coordination>>,
267 class TakeUntil = rxo::detail::take_until<SourceValue, rxu::decay_t<Observable>, rxu::decay_t<TriggerObservable>, rxu::decay_t<Coordination>>,
270 static Result
member(Observable&& o, TriggerObservable&& t, Coordination&& cn) {
271 return Result(TakeUntil(std::forward<Observable>(o), std::forward<TriggerObservable>(t), std::forward<Coordination>(cn)));
274 template<
class...
AN>
275 static operators::detail::take_until_invalid_t<
AN...>
member(
AN...) {
278 static_assert(
sizeof...(
AN) == 10000,
"take_until takes (TriggerObservable, optional Coordination) or (TimePoint, optional Coordination)");
Definition: rx-util.hpp:100
Definition: rx-all.hpp:26
typename std::decay< T >::type::value_type value_type_t
Definition: rx-util.hpp:35
static Result member(Observable &&o, TimePoint &&when, Coordination cn)
Definition: rx-take_until.hpp:247
Definition: rx-operators.hpp:69
auto AN
Definition: rx-finally.hpp:105
static operators::detail::take_until_invalid_t< AN... > member(AN...)
Definition: rx-take_until.hpp:275
typename std::decay< T >::type decay_t
Definition: rx-util.hpp:36
Definition: rx-operators.hpp:47
Definition: rx-operators.hpp:438
typename std::enable_if< all_true_type< BN... >::value >::type enable_if_all_true_type_t
Definition: rx-util.hpp:114
a source of values. subscribe or use one of the operator methods that return a new observable...
Definition: rx-observable.hpp:510
Definition: rx-util.hpp:325
static Result member(Observable &&o, TriggerObservable &&t)
Definition: rx-take_until.hpp:258
static Result member(Observable &&o, TimePoint &&when)
Definition: rx-take_until.hpp:230
auto timer(TimePointOrDuration when) -> typename std::enable_if< detail::defer_timer< TimePointOrDuration, identity_one_worker >::value, typename detail::defer_timer< TimePointOrDuration, identity_one_worker >::observable_type >::type
Returns an observable that emits an integer at the specified time point.
Definition: rx-timer.hpp:114
auto on_exception(const F &f, const OnError &c) -> typename std::enable_if< detail::is_on_error< OnError >::value, typename detail::maybe_from_result< F >::type >::type
Definition: rx-observer.hpp:639
Definition: rx-coordination.hpp:114
static Result member(Observable &&o, TriggerObservable &&t, Coordination &&cn)
Definition: rx-take_until.hpp:270
identity_one_worker identity_current_thread()
Definition: rx-coordination.hpp:175
auto take_until(AN &&...an) -> operator_factory< take_until_tag, AN... >
For each item from this observable until on_next occurs on the trigger observable or until the specif...
Definition: rx-take_until.hpp:209
Definition: rx-predef.hpp:177
Definition: rx-coordination.hpp:37