summaryrefslogtreecommitdiff
path: root/Rx/v2/src/rxcpp/operators/rx-replay.hpp
blob: b2db8d826c9f31295536ea739c7d86f622ffeacd (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
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.

#pragma once

/*! \file rx-replay.hpp
   
    \brief 1) replay(optional Coordination, optional CompositeSubscription)
              Turn a cold observable hot, send all earlier emitted values to any new subscriber, and allow connections to the source to be independent of subscriptions.

           2) replay(Count, optional Coordination, optional CompositeSubscription)
              Turn a cold observable hot, send at most count of earlier emitted values to any new subscriber, and allow connections to the source to be independent of subscriptions.

           3) replay(Duration, optional Coordination, optional CompositeSubscription)
              Turn a cold observable hot, send values emitted within a specified time window to any new subscriber, and allow connections to the source to be independent of subscriptions.

           4) replay(Count, Duration, optional Coordination, optional CompositeSubscription)
              Turn a cold observable hot, send at most count of values emitted within a specified time window to any new subscriber, and allow connections to the source to be independent of subscriptions.

    \tparam Duration  the type of the time interval (optional).
    \tparam Count  the type of the maximum number of the most recent items sent to new observers (optional).
    \tparam Coordination  the type of the scheduler (optional).

    \param count  the maximum number of the most recent items sent to new observers (optional).
    \param d  the duration of the window in which the replayed items must be emitted
    \param cn  a scheduler all values are queued and delivered on (optional).
    \param cs  the subscription to control lifetime (optional).

    \return  rxcpp::connectable_observable that shares a single subscription to the underlying observable that will replay all of its items and notifications to any future observer.

    \sample
    \snippet replay.cpp replay sample
    \snippet output.txt replay sample

    \sample
    \snippet replay.cpp threaded replay sample
    \snippet output.txt threaded replay sample

    \sample
    \snippet replay.cpp replay count sample
    \snippet output.txt replay count sample

    \sample
    \snippet replay.cpp threaded replay count sample
    \snippet output.txt threaded replay count sample

    \sample
    \snippet replay.cpp replay period sample
    \snippet output.txt replay period sample

    \sample
    \snippet replay.cpp threaded replay period sample
    \snippet output.txt threaded replay period sample

    \sample
    \snippet replay.cpp replay count+period sample
    \snippet output.txt replay count+period sample

    \sample
    \snippet replay.cpp threaded replay count+period sample
    \snippet output.txt threaded replay count+period sample
*/

#if !defined(RXCPP_OPERATORS_RX_REPLAY_HPP)
#define RXCPP_OPERATORS_RX_REPLAY_HPP

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

namespace rxcpp {

namespace operators {

namespace detail {

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

template<class... AN>
struct replay_invalid : public rxo::operator_base<replay_invalid_arguments<AN...>> {
    using type = observable<replay_invalid_arguments<AN...>, replay_invalid<AN...>>;
};
template<class... AN>
using replay_invalid_t = typename replay_invalid<AN...>::type;

}

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

 template<>
struct member_overload<replay_tag>
{
    template<class Observable,
        class Enabled = rxu::enable_if_all_true_type_t<
            is_observable<Observable>>,
        class SourceValue = rxu::value_type_t<Observable>,
        class Subject = rxsub::replay<SourceValue, identity_one_worker>,
        class Multicast = rxo::detail::multicast<SourceValue, rxu::decay_t<Observable>, Subject>,
        class Result = connectable_observable<SourceValue, Multicast>
        >
    static Result member(Observable&& o) {
        return Result(Multicast(std::forward<Observable>(o), Subject(identity_current_thread(), composite_subscription())));
    }

    template<class Observable,
        class Enabled = rxu::enable_if_all_true_type_t<
            is_observable<Observable>>,
        class SourceValue = rxu::value_type_t<Observable>,
        class Subject = rxsub::replay<SourceValue, identity_one_worker>,
        class Multicast = rxo::detail::multicast<SourceValue, rxu::decay_t<Observable>, Subject>,
        class Result = connectable_observable<SourceValue, Multicast>
        >
    static Result member(Observable&& o, composite_subscription cs) {
        return Result(Multicast(std::forward<Observable>(o), Subject(identity_current_thread(), cs)));
    }

    template<class Observable, class Coordination,
        class Enabled = rxu::enable_if_all_true_type_t<
            is_observable<Observable>,
            is_coordination<Coordination>>,
        class SourceValue = rxu::value_type_t<Observable>,
        class Subject = rxsub::replay<SourceValue, rxu::decay_t<Coordination>>,
        class Multicast = rxo::detail::multicast<SourceValue, rxu::decay_t<Observable>, Subject>,
        class Result = connectable_observable<SourceValue, Multicast>
        >
    static Result member(Observable&& o, Coordination&& cn, composite_subscription cs = composite_subscription()) {
        return Result(Multicast(std::forward<Observable>(o), Subject(std::forward<Coordination>(cn), cs)));
    }

    template<class Observable, class Count,
        class Enabled = rxu::enable_if_all_true_type_t<
            is_observable<Observable>,
            std::is_integral<Count>>,
        class SourceValue = rxu::value_type_t<Observable>,
        class Subject = rxsub::replay<SourceValue, identity_one_worker>,
        class Multicast = rxo::detail::multicast<SourceValue, rxu::decay_t<Observable>, Subject>,
        class Result = connectable_observable<SourceValue, Multicast>
    >
    static Result member(Observable&& o, Count count, composite_subscription cs = composite_subscription()) {
        return Result(Multicast(std::forward<Observable>(o), Subject(count, identity_current_thread(), cs)));
    }

    template<class Observable, class Count, class Coordination,
        class Enabled = rxu::enable_if_all_true_type_t<
            is_observable<Observable>,
            std::is_integral<Count>,
            is_coordination<Coordination>>,
        class SourceValue = rxu::value_type_t<Observable>,
        class Subject = rxsub::replay<SourceValue, rxu::decay_t<Coordination>>,
        class Multicast = rxo::detail::multicast<SourceValue, rxu::decay_t<Observable>, Subject>,
        class Result = connectable_observable<SourceValue, Multicast>
        >
    static Result member(Observable&& o, Count count, Coordination&& cn, composite_subscription cs = composite_subscription()) {
        return Result(Multicast(std::forward<Observable>(o), Subject(count, std::forward<Coordination>(cn), cs)));
    }

    template<class Observable, class Duration,
        class IsDuration = rxu::is_duration<Duration>,
        class Enabled = rxu::enable_if_all_true_type_t<
            is_observable<Observable>,
            IsDuration>,
        class SourceValue = rxu::value_type_t<Observable>,
        class Subject = rxsub::replay<SourceValue, identity_one_worker>,
        class Multicast = rxo::detail::multicast<SourceValue, rxu::decay_t<Observable>, Subject>,
        class Result = connectable_observable<SourceValue, Multicast>
    >
    static Result member(Observable&& o, Duration&& d, composite_subscription cs = composite_subscription()) {
        return Result(Multicast(std::forward<Observable>(o), Subject(std::forward<Duration>(d), identity_current_thread(), cs)));
    }

    template<class Observable, class Duration, class Coordination,
        class IsDuration = rxu::is_duration<Duration>,
        class Enabled = rxu::enable_if_all_true_type_t<
            is_observable<Observable>,
            IsDuration,
            is_coordination<Coordination>>,
        class SourceValue = rxu::value_type_t<Observable>,
        class Subject = rxsub::replay<SourceValue, rxu::decay_t<Coordination>>,
        class Multicast = rxo::detail::multicast<SourceValue, rxu::decay_t<Observable>, Subject>,
        class Result = connectable_observable<SourceValue, Multicast>
        >
    static Result member(Observable&& o, Duration&& d, Coordination&& cn, composite_subscription cs = composite_subscription()) {
        return Result(Multicast(std::forward<Observable>(o), Subject(std::forward<Duration>(d), std::forward<Coordination>(cn), cs)));
    }

    template<class Observable, class Count, class Duration,
        class IsDuration = rxu::is_duration<Duration>,
        class Enabled = rxu::enable_if_all_true_type_t<
            is_observable<Observable>,
            std::is_integral<Count>,
            IsDuration>,
        class SourceValue = rxu::value_type_t<Observable>,
        class Subject = rxsub::replay<SourceValue, identity_one_worker>,
        class Multicast = rxo::detail::multicast<SourceValue, rxu::decay_t<Observable>, Subject>,
        class Result = connectable_observable<SourceValue, Multicast>
    >
    static Result member(Observable&& o, Count count, Duration&& d, composite_subscription cs = composite_subscription()) {
        return Result(Multicast(std::forward<Observable>(o), Subject(count, std::forward<Duration>(d), identity_current_thread(), cs)));
    }

    template<class Observable, class Count, class Duration, class Coordination,
        class IsDuration = rxu::is_duration<Duration>,
        class Enabled = rxu::enable_if_all_true_type_t<
            is_observable<Observable>,
            std::is_integral<Count>,
            IsDuration,
            is_coordination<Coordination>>,
        class SourceValue = rxu::value_type_t<Observable>,
        class Subject = rxsub::replay<SourceValue, rxu::decay_t<Coordination>>,
        class Multicast = rxo::detail::multicast<SourceValue, rxu::decay_t<Observable>, Subject>,
        class Result = connectable_observable<SourceValue, Multicast>
    >
    static Result member(Observable&& o, Count count, Duration&& d, Coordination&& cn, composite_subscription cs = composite_subscription()) {
        return Result(Multicast(std::forward<Observable>(o), Subject(count, std::forward<Duration>(d), std::forward<Coordination>(cn), cs)));
    }

    template<class... AN>
    static operators::detail::replay_invalid_t<AN...> member(AN...) {
        std::terminate();
        return {};
        static_assert(sizeof...(AN) == 10000, "replay takes (optional Count, optional Duration, optional Coordination, optional CompositeSubscription)");
    }
};   
    
}

#endif