summaryrefslogtreecommitdiff
path: root/Rx/v2/src/rxcpp/operators/rx-window_toggle.hpp
blob: b9f119ac3f393f5890b4a8bad696d3a7d81a5e54 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.

#pragma once

/*! \file rx-window_toggle.hpp

    \brief Return an observable that emits observables every period time interval and collects items from this observable for period of time into each produced observable, on the specified scheduler.

    \tparam Openings        observable<OT>
    \tparam ClosingSelector a function of type observable<CT>(OT)
    \tparam Coordination    the type of the scheduler (optional).

    \param opens         each value from this observable opens a new window.
    \param closes        this function is called for each opened window and returns an observable. the first value from the returned observable will close the window.
    \param coordination  the scheduler for the windows (optional).

    \return  Observable that emits an observable for each opened window.

    \sample
    \snippet window.cpp window toggle+coordination sample
    \snippet output.txt window toggle+coordination sample

    \sample
    \snippet window.cpp window toggle sample
    \snippet output.txt window toggle sample
*/

#if !defined(RXCPP_OPERATORS_RX_WINDOW_TOGGLE_HPP)
#define RXCPP_OPERATORS_RX_WINDOW_TOGGLE_HPP

#include "../rx-includes.hpp"

namespace rxcpp {

namespace operators {

namespace detail {

template<class... AN>
struct window_toggle_invalid_arguments {};

template<class... AN>
struct window_toggle_invalid : public rxo::operator_base<window_toggle_invalid_arguments<AN...>> {
    using type = observable<window_toggle_invalid_arguments<AN...>, window_toggle_invalid<AN...>>;
};
template<class... AN>
using window_toggle_invalid_t = typename window_toggle_invalid<AN...>::type;

template<class T, class Openings, class ClosingSelector, class Coordination>
struct window_toggle
{
    typedef window_toggle<T, Openings, ClosingSelector, Coordination> this_type;

    using source_value_type = rxu::decay_t<T>;
    using coordination_type = rxu::decay_t<Coordination>;
    using coordinator_type = typename coordination_type::coordinator_type;
    using openings_type = rxu::decay_t<Openings>;
    using openings_value_type = typename openings_type::value_type;
    using closing_selector_type = rxu::decay_t<ClosingSelector>;
    using closings_type = rxu::result_of_t<closing_selector_type(openings_value_type)>;
    using closings_value_type = typename closings_type::value_type;

    struct window_toggle_values
    {
        window_toggle_values(openings_type opens, closing_selector_type closes, coordination_type c)
            : openings(opens)
            , closingSelector(closes)
            , coordination(c)
        {
        }
        openings_type openings;
        mutable closing_selector_type closingSelector;
        coordination_type coordination;
    };
    window_toggle_values initial;

    window_toggle(openings_type opens, closing_selector_type closes, coordination_type coordination)
        : initial(opens, closes, coordination)
    {
    }

    template<class Subscriber>
    struct window_toggle_observer
    {
        typedef window_toggle_observer<Subscriber> this_type;
        typedef rxu::decay_t<T> value_type;
        typedef rxu::decay_t<Subscriber> dest_type;
        typedef observer<T, this_type> observer_type;

        struct window_toggle_subscriber_values : public window_toggle_values
        {
            window_toggle_subscriber_values(composite_subscription cs, dest_type d, window_toggle_values v, coordinator_type c)
                : window_toggle_values(v)
                , cs(std::move(cs))
                , dest(std::move(d))
                , coordinator(std::move(c))
                , worker(coordinator.get_worker())
            {
            }
            composite_subscription cs;
            dest_type dest;
            coordinator_type coordinator;
            rxsc::worker worker;
            mutable std::list<rxcpp::subjects::subject<T>> subj;
        };
        std::shared_ptr<window_toggle_subscriber_values> state;

        window_toggle_observer(composite_subscription cs, dest_type d, window_toggle_values v, coordinator_type c)
            : state(std::make_shared<window_toggle_subscriber_values>(window_toggle_subscriber_values(std::move(cs), std::move(d), v, std::move(c))))
        {
            auto localState = state;

            composite_subscription innercs;

            // when the out observer is unsubscribed all the
            // inner subscriptions are unsubscribed as well
            auto innerscope = localState->dest.add(innercs);

            innercs.add([=](){
                localState->dest.remove(innerscope);
            });

            localState->dest.add(localState->cs);

            auto source = on_exception(
                [&](){return localState->coordinator.in(localState->openings);},
                localState->dest);
            if (source.empty()) {
                return;
            }

            // this subscribe does not share the observer subscription
            // so that when it is unsubscribed the observer can be called
            // until the inner subscriptions have finished
            auto sink = make_subscriber<openings_value_type>(
                localState->dest,
                innercs,
            // on_next
                [localState](const openings_value_type& ov) {
                    auto closer = localState->closingSelector(ov);

                    auto it = localState->subj.insert(localState->subj.end(), rxcpp::subjects::subject<T>());
                    localState->dest.on_next(it->get_observable().as_dynamic());

                    composite_subscription innercs;

                    // when the out observer is unsubscribed all the
                    // inner subscriptions are unsubscribed as well
                    auto innerscope = localState->dest.add(innercs);

                    innercs.add([=](){
                        localState->dest.remove(innerscope);
                    });

                    auto source = localState->coordinator.in(closer);

                    auto sit = std::make_shared<decltype(it)>(it);
                    auto close = [localState, sit]() {
                        auto it = *sit;
                        *sit = localState->subj.end();
                        if (it != localState->subj.end()) {
                            it->get_subscriber().on_completed();
                            localState->subj.erase(it);
                        }
                    };

                    // this subscribe does not share the observer subscription
                    // so that when it is unsubscribed the observer can be called
                    // until the inner subscriptions have finished
                    auto sink = make_subscriber<closings_value_type>(
                        localState->dest,
                        innercs,
                    // on_next
                        [close, innercs](closings_value_type) {
                            close();
                            innercs.unsubscribe();
                        },
                    // on_error
                        [localState](rxu::error_ptr e) {
                            localState->dest.on_error(e);
                        },
                    // on_completed
                        close
                    );
                    auto selectedSink = localState->coordinator.out(sink);
                    source.subscribe(std::move(selectedSink));
                },
            // on_error
                [localState](rxu::error_ptr e) {
                    localState->dest.on_error(e);
                },
            // on_completed
                []() {
                }
            );
            auto selectedSink = on_exception(
                [&](){return localState->coordinator.out(sink);},
                localState->dest);
            if (selectedSink.empty()) {
                return;
            }
            source->subscribe(std::move(selectedSink.get()));
        }

        void on_next(T v) const {
            auto localState = state;
            auto work = [v, localState](const rxsc::schedulable&){
                for (auto s : localState->subj) {
                    s.get_subscriber().on_next(v);
                }
            };
            auto selectedWork = on_exception(
                [&](){return localState->coordinator.act(work);},
                localState->dest);
            if (selectedWork.empty()) {
                return;
            }
            localState->worker.schedule(selectedWork.get());
        }

        void on_error(rxu::error_ptr e) const {
            auto localState = state;
            auto work = [e, localState](const rxsc::schedulable&){
                for (auto s : localState->subj) {
                    s.get_subscriber().on_error(e);
                }
                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&){
                for (auto s : localState->subj) {
                    s.get_subscriber().on_completed();
                }
                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<T, observer_type> make(dest_type d, window_toggle_values v) {
            auto cs = composite_subscription();
            auto coordinator = v.coordination.create_coordinator(d.get_subscription());

            return make_subscriber<T>(cs, observer_type(this_type(cs, std::move(d), std::move(v), std::move(coordinator))));
        }
    };

    template<class Subscriber>
    auto operator()(Subscriber dest) const
        -> decltype(window_toggle_observer<Subscriber>::make(std::move(dest), initial)) {
        return      window_toggle_observer<Subscriber>::make(std::move(dest), initial);
    }
};

}

/*! @copydoc rx-window_toggle.hpp
*/
template<class... AN>
auto window_toggle(AN&&... an)
    ->      operator_factory<window_toggle_tag, AN...> {
     return operator_factory<window_toggle_tag, AN...>(std::make_tuple(std::forward<AN>(an)...));
}

}

template<>
struct member_overload<window_toggle_tag>
{
    template<class Observable, class Openings, class ClosingSelector,
        class ClosingSelectorType = rxu::decay_t<ClosingSelector>,
        class OpeningsType = rxu::decay_t<Openings>,
        class OpeningsValueType = typename OpeningsType::value_type,
        class Enabled = rxu::enable_if_all_true_type_t<
            all_observables<Observable, Openings, rxu::result_of_t<ClosingSelectorType(OpeningsValueType)>>>,
        class SourceValue = rxu::value_type_t<Observable>,
        class WindowToggle = rxo::detail::window_toggle<SourceValue, rxu::decay_t<Openings>, rxu::decay_t<ClosingSelector>, identity_one_worker>,
        class Value = observable<SourceValue>>
    static auto member(Observable&& o, Openings&& openings, ClosingSelector&& closingSelector)
        -> decltype(o.template lift<Value>(WindowToggle(std::forward<Openings>(openings), std::forward<ClosingSelector>(closingSelector), identity_immediate()))) {
        return      o.template lift<Value>(WindowToggle(std::forward<Openings>(openings), std::forward<ClosingSelector>(closingSelector), identity_immediate()));
    }

    template<class Observable, class Openings, class ClosingSelector, class Coordination,
        class ClosingSelectorType = rxu::decay_t<ClosingSelector>,
        class OpeningsType = rxu::decay_t<Openings>,
        class OpeningsValueType = typename OpeningsType::value_type,
        class Enabled = rxu::enable_if_all_true_type_t<
            all_observables<Observable, Openings, rxu::result_of_t<ClosingSelectorType(OpeningsValueType)>>,
            is_coordination<Coordination>>,
        class SourceValue = rxu::value_type_t<Observable>,
        class WindowToggle = rxo::detail::window_toggle<SourceValue, rxu::decay_t<Openings>, rxu::decay_t<ClosingSelector>, rxu::decay_t<Coordination>>,
        class Value = observable<SourceValue>>
    static auto member(Observable&& o, Openings&& openings, ClosingSelector&& closingSelector, Coordination&& cn)
        -> decltype(o.template lift<Value>(WindowToggle(std::forward<Openings>(openings), std::forward<ClosingSelector>(closingSelector), std::forward<Coordination>(cn)))) {
        return      o.template lift<Value>(WindowToggle(std::forward<Openings>(openings), std::forward<ClosingSelector>(closingSelector), std::forward<Coordination>(cn)));
    }

    template<class... AN>
    static operators::detail::window_toggle_invalid_t<AN...> member(AN...) {
        std::terminate();
        return {};
        static_assert(sizeof...(AN) == 10000, "window_toggle takes (Openings, ClosingSelector, optional Coordination)");
    }
};

}

#endif