// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information. #pragma once /*! \file rx-buffer_time_count.hpp \brief Return an observable that emits connected, non-overlapping buffers of items from the source observable that were emitted during a fixed duration of time or when the buffer has reached maximum capacity (whichever occurs first), on the specified scheduler. \tparam Duration the type of the time interval. \tparam Coordination the type of the scheduler (optional). \param period the period of time each buffer collects items before it is emitted and replaced with a new buffer. \param count the maximum size of each buffer before it is emitted and new buffer is created. \param coordination the scheduler for the buffers (optional). \return Observable that emits connected, non-overlapping buffers of items from the source observable that were emitted during a fixed duration of time or when the buffer has reached maximum capacity (whichever occurs first). \sample \snippet buffer.cpp buffer period+count+coordination sample \snippet output.txt buffer period+count+coordination sample \sample \snippet buffer.cpp buffer period+count sample \snippet output.txt buffer period+count sample */ #if !defined(RXCPP_OPERATORS_RX_BUFFER_WITH_TIME_OR_COUNT_HPP) #define RXCPP_OPERATORS_RX_BUFFER_WITH_TIME_OR_COUNT_HPP #include "../rx-includes.hpp" namespace rxcpp { namespace operators { namespace detail { template struct buffer_with_time_or_count_invalid_arguments {}; template struct buffer_with_time_or_count_invalid : public rxo::operator_base> { using type = observable, buffer_with_time_or_count_invalid>; }; template using buffer_with_time_or_count_invalid_t = typename buffer_with_time_or_count_invalid::type; template struct buffer_with_time_or_count { typedef rxu::decay_t source_value_type; typedef std::vector value_type; typedef rxu::decay_t coordination_type; typedef typename coordination_type::coordinator_type coordinator_type; typedef rxu::decay_t duration_type; struct buffer_with_time_or_count_values { buffer_with_time_or_count_values(duration_type p, int n, coordination_type c) : period(p) , count(n) , coordination(c) { } duration_type period; int count; coordination_type coordination; }; buffer_with_time_or_count_values initial; buffer_with_time_or_count(duration_type period, int count, coordination_type coordination) : initial(period, count, coordination) { } template struct buffer_with_time_or_count_observer { typedef buffer_with_time_or_count_observer this_type; typedef std::vector value_type; typedef rxu::decay_t dest_type; typedef observer observer_type; struct buffer_with_time_or_count_subscriber_values : public buffer_with_time_or_count_values { buffer_with_time_or_count_subscriber_values(composite_subscription cs, dest_type d, buffer_with_time_or_count_values v, coordinator_type c) : buffer_with_time_or_count_values(std::move(v)) , cs(std::move(cs)) , dest(std::move(d)) , coordinator(std::move(c)) , worker(coordinator.get_worker()) , chunk_id(0) { } composite_subscription cs; dest_type dest; coordinator_type coordinator; rxsc::worker worker; mutable int chunk_id; mutable value_type chunk; }; typedef std::shared_ptr state_type; state_type state; buffer_with_time_or_count_observer(composite_subscription cs, dest_type d, buffer_with_time_or_count_values v, coordinator_type c) : state(std::make_shared(buffer_with_time_or_count_subscriber_values(std::move(cs), std::move(d), std::move(v), std::move(c)))) { auto new_id = state->chunk_id; auto produce_time = state->worker.now() + state->period; auto localState = state; auto disposer = [=](const rxsc::schedulable&){ localState->cs.unsubscribe(); localState->dest.unsubscribe(); localState->worker.unsubscribe(); }; auto selectedDisposer = on_exception( [&](){return localState->coordinator.act(disposer);}, localState->dest); if (selectedDisposer.empty()) { return; } localState->dest.add([=](){ localState->worker.schedule(selectedDisposer.get()); }); localState->cs.add([=](){ localState->worker.schedule(selectedDisposer.get()); }); // // The scheduler is FIFO for any time T. Since the observer is scheduling // on_next/on_error/oncompleted the timed schedule calls must be resheduled // when they occur to ensure that production happens after on_next/on_error/oncompleted // localState->worker.schedule(produce_time, [new_id, produce_time, localState](const rxsc::schedulable&){ localState->worker.schedule(produce_buffer(new_id, produce_time, localState)); }); } static std::function produce_buffer(int id, rxsc::scheduler::clock_type::time_point expected, state_type state) { auto produce = [id, expected, state](const rxsc::schedulable&) { if (id != state->chunk_id) return; state->dest.on_next(state->chunk); state->chunk.resize(0); auto new_id = ++state->chunk_id; auto produce_time = expected + state->period; state->worker.schedule(produce_time, [new_id, produce_time, state](const rxsc::schedulable&){ state->worker.schedule(produce_buffer(new_id, produce_time, state)); }); }; auto selectedProduce = on_exception( [&](){return state->coordinator.act(produce);}, state->dest); if (selectedProduce.empty()) { return std::function(); } return std::function(selectedProduce.get()); } void on_next(T v) const { auto localState = state; auto work = [v, localState](const rxsc::schedulable& self){ localState->chunk.push_back(v); if (int(localState->chunk.size()) == localState->count) { produce_buffer(localState->chunk_id, localState->worker.now(), localState)(self); } }; auto selectedWork = on_exception( [&](){return localState->coordinator.act(work);}, localState->dest); if (selectedWork.empty()) { return; } localState->worker.schedule(selectedWork.get()); } void on_error(std::exception_ptr e) const { auto localState = state; auto work = [e, localState](const rxsc::schedulable&){ localState->dest.on_error(e); }; auto selectedWork = on_exception( [&](){return localState->coordinator.act(work);}, localState->dest); if (selectedWork.empty()) { return; } localState->worker.schedule(selectedWork.get()); } void on_completed() const { auto localState = state; auto work = [localState](const rxsc::schedulable&){ localState->dest.on_next(localState->chunk); localState->dest.on_completed(); }; auto selectedWork = on_exception( [&](){return localState->coordinator.act(work);}, localState->dest); if (selectedWork.empty()) { return; } localState->worker.schedule(selectedWork.get()); } static subscriber> make(dest_type d, buffer_with_time_or_count_values v) { auto cs = composite_subscription(); auto coordinator = v.coordination.create_coordinator(); return make_subscriber(cs, this_type(cs, std::move(d), std::move(v), std::move(coordinator))); } }; template auto operator()(Subscriber dest) const -> decltype(buffer_with_time_or_count_observer::make(std::move(dest), initial)) { return buffer_with_time_or_count_observer::make(std::move(dest), initial); } }; } /*! @copydoc rx-buffer_time_count.hpp */ template auto buffer_with_time_or_count(AN&&... an) -> operator_factory { return operator_factory(std::make_tuple(std::forward(an)...)); } } template<> struct member_overload { template, std::is_convertible>, class SourceValue = rxu::value_type_t, class BufferTimeCount = rxo::detail::buffer_with_time_or_count, identity_one_worker>, class Value = rxu::value_type_t> static auto member(Observable&& o, Duration&& period, int count) -> decltype(o.template lift(BufferTimeCount(std::forward(period), count, identity_current_thread()))) { return o.template lift(BufferTimeCount(std::forward(period), count, identity_current_thread())); } template, std::is_convertible, is_coordination>, class SourceValue = rxu::value_type_t, class BufferTimeCount = rxo::detail::buffer_with_time_or_count, rxu::decay_t>, class Value = rxu::value_type_t> static auto member(Observable&& o, Duration&& period, int count, Coordination&& cn) -> decltype(o.template lift(BufferTimeCount(std::forward(period), count, std::forward(cn)))) { return o.template lift(BufferTimeCount(std::forward(period), count, std::forward(cn))); } template static operators::detail::buffer_with_time_or_count_invalid_t member(AN...) { std::terminate(); return {}; static_assert(sizeof...(AN) == 10000, "buffer_with_time_or_count takes (Duration, Count, optional Coordination)"); } }; } #endif