-
Notifications
You must be signed in to change notification settings - Fork 3
/
rx-redux.hpp
280 lines (232 loc) · 8.74 KB
/
rx-redux.hpp
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
#pragma once
#include <queue>
#include <rxcpp/rx.hpp>
//=============================================================================
namespace redux {
//=============================================================================
namespace detail
{
template <typename T>
struct function_traits : public function_traits<decltype(&T::operator())> {};
template <typename ClassType, typename ReturnType, typename... Args>
struct function_traits<ReturnType(ClassType::*)(Args...) const>
{
typedef ReturnType result_type;
enum { arity = sizeof...(Args) };
template <size_t i> struct arg
{
typedef typename std::tuple_element<i, std::tuple<Args...>>::type type;
};
};
template<typename Fn>
Fn negate(Fn pred) { return [pred] (auto arg) { return ! pred(arg); }; }
}
//=============================================================================
template<typename StateType, typename ActionType>
class redux_t
{
public:
//=========================================================================
class proxy_t;
class store_t;
class dispatcher_t;
using state_t = StateType;
using action_t = ActionType;
using action_bus_t = rxcpp::subjects::subject<action_t>;
using action_stream_t = rxcpp::observable<action_t>;
using state_stream_t = rxcpp::observable<state_t>;
using reducer_t = std::function<state_t(state_t, action_t)>;
using runoff_pred_t = std::function<bool(action_t)>;
using next_t = std::function<void(action_t)>;
using subscriber_t = std::function<void(state_t)>;
using middleware_t = std::function<void(proxy_t, next_t, action_t)>;
using bottomware_t = std::function<action_stream_t(action_stream_t)>;
//=========================================================================
class proxy_t
{
public:
state_t get_state() const
{
std::lock_guard<std::mutex> lock(*shared_state_mutex);
return *shared_state;
}
void dispatch(action_t action)
{
std::lock_guard<std::recursive_mutex> lock(*dispatch_mutex);
if (std::this_thread::get_id() == dispatch_thread_id)
{
dispatch_queue->push(std::move(action));
}
else
{
shared_next->operator()(std::move(action));
}
}
private:
friend class dispatcher_t;
proxy_t() {}
proxy_t(
std::shared_ptr<next_t> shared_next,
std::shared_ptr<state_t> shared_state,
std::shared_ptr<std::mutex> shared_state_mutex,
std::shared_ptr<std::queue<action_t>> dispatch_queue,
std::shared_ptr<std::recursive_mutex> dispatch_mutex,
std::thread::id dispatch_thread_id)
: shared_next(shared_next)
, shared_state(shared_state)
, shared_state_mutex(shared_state_mutex)
, dispatch_queue(dispatch_queue)
, dispatch_mutex(dispatch_mutex)
, dispatch_thread_id(dispatch_thread_id)
{
}
std::shared_ptr<next_t> shared_next;
std::shared_ptr<state_t> shared_state;
std::shared_ptr<std::mutex> shared_state_mutex;
std::shared_ptr<std::queue<action_t>> dispatch_queue;
std::shared_ptr<std::recursive_mutex> dispatch_mutex;
std::thread::id dispatch_thread_id;
};
//=========================================================================
class dispatcher_t
{
public:
dispatcher_t() {}
dispatcher_t(next_t next, state_t state=state_t())
: shared_next (std::make_shared<next_t>(next))
, shared_state (std::make_shared<state_t>(state))
, shared_state_mutex(std::make_shared<std::mutex>())
, dispatch_queue (std::make_shared<std::queue<action_t>>())
, dispatch_mutex (std::make_shared<std::recursive_mutex>())
, dispatch_thread_id(std::this_thread::get_id())
, proxy(
shared_next,
shared_state,
shared_state_mutex,
dispatch_queue,
dispatch_mutex,
dispatch_thread_id)
{
}
void dispatch(action_t action)
{
std::lock_guard<std::recursive_mutex> lock(*dispatch_mutex);
dispatch_queue->push(std::move(action));
if (std::this_thread::get_id() == dispatch_thread_id)
{
while (! dispatch_queue->empty())
{
shared_next->operator()(std::move(dispatch_queue->front()));
dispatch_queue->pop();
}
}
}
void set_state(state_t next_state)
{
std::lock_guard<std::mutex> lock(*shared_state_mutex);
*shared_state = std::move(next_state);
}
state_t get_state() const
{
std::lock_guard<std::mutex> lock(*shared_state_mutex);
return *shared_state;
}
void apply_middleware(middleware_t middleware)
{
*shared_next = [
middleware,
proxy=proxy,
shared_next=shared_next,
next=*shared_next] (auto action)
{
middleware(proxy, next, std::move(action));
};
}
private:
std::shared_ptr<next_t> shared_next;
std::shared_ptr<state_t> shared_state;
std::shared_ptr<std::mutex> shared_state_mutex;
std::shared_ptr<std::queue<action_t>> dispatch_queue;
std::shared_ptr<std::recursive_mutex> dispatch_mutex;
std::thread::id dispatch_thread_id;
proxy_t proxy;
};
//=========================================================================
class store_t
{
public:
store_t() {}
store_t(
reducer_t reducer,
bottomware_t bottomware=[] (auto o) { return o; },
runoff_pred_t runoff_pred=[] (auto) { return false; },
state_t state=state_t())
: dispatcher(innermost_next(), state)
, action_stream(bottomware(action_bus.get_observable()))
, action_runoff(action_stream.filter(runoff_pred))
, state_stream(action_stream
.filter(detail::negate(runoff_pred))
.scan(state, reducer)
.publish()
.ref_count())
{
// Can't capture the dipatcher by value without mutable...
auto dp = std::make_shared<dispatcher_t>(dispatcher);
action_runoff.subscribe([dispatcher=dp] (auto action)
{
dispatcher->dispatch(std::move(action));
});
state_stream.subscribe([dispatcher=dp] (auto state)
{
dispatcher->set_state(std::move(state));
});
}
void dispatch(action_t action)
{
dispatcher.dispatch(std::move(action));
}
state_t get_state() const
{
return dispatcher.get_state();
}
store_t& apply_middleware(middleware_t middleware)
{
dispatcher.apply_middleware(std::move(middleware));
return *this;
}
auto subscribe(subscriber_t subscriber)
{
return state_stream.subscribe(subscriber);
}
auto get_state_stream() const
{
return state_stream;
}
private:
next_t innermost_next()
{
return [s=action_bus.get_subscriber()] (action_t action) { s.on_next(std::move(action)); };
}
action_bus_t action_bus;
dispatcher_t dispatcher;
action_stream_t action_stream;
action_stream_t action_runoff;
state_stream_t state_stream;
};
};
//=============================================================================
template<
typename ReducerType,
typename StateType = typename detail::function_traits<ReducerType>::template arg<0>::type,
typename ActionType = typename detail::function_traits<ReducerType>::template arg<1>::type,
typename RunoffPredType = typename redux_t<StateType, ActionType>::runoff_pred_t,
typename BottomwareType = typename redux_t<StateType, ActionType>::bottomware_t>
auto create_store(
ReducerType reducer,
BottomwareType bottomware=[] (auto o) { return o; },
RunoffPredType runoff_pred=[] (auto) { return false; },
StateType state=StateType())
{
return typename redux_t<StateType, ActionType>::store_t(reducer, bottomware, runoff_pred, state);
}
} // namespace redux