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
use async_trait::async_trait;
use std::marker::PhantomData;
use tokio::task::JoinHandle;
use crate::{
middleware::{MiddleWare, StoreApi, StoreWithMiddleware},
Reducer, Selector, Subscriber,
};
mod worker;
use worker::{Address, Dispatch, Select, StateWorker, Subscribe};
pub struct Store<State, Action, RootReducer>
where
State: Send,
RootReducer: Send,
{
worker_address: Address<State, Action, RootReducer>,
_worker_handle: JoinHandle<()>,
_types: PhantomData<RootReducer>,
}
impl<State, Action, RootReducer> Store<State, Action, RootReducer>
where
Action: Send + 'static,
RootReducer: Reducer<State, Action> + Send + 'static,
State: Send + 'static,
{
pub fn new(root_reducer: RootReducer) -> Self
where
State: Default,
{
Self::new_with_state(root_reducer, Default::default())
}
pub fn new_with_state(root_reducer: RootReducer, state: State) -> Self {
let mut worker = StateWorker::new(root_reducer, state);
let worker_address = worker.address();
let _worker_handle = tokio::spawn(async move {
worker.run().await;
});
Store {
worker_address,
_worker_handle,
_types: Default::default(),
}
}
pub async fn dispatch(&self, action: Action) {
self.worker_address.send(Dispatch::new(action)).await;
}
pub async fn select<S: Selector<State, Result = Result>, Result>(&self, selector: S) -> Result
where
S: Selector<State, Result = Result> + Send + 'static,
Result: Send + 'static,
{
self.worker_address.send(Select::new(selector)).await
}
pub async fn state_cloned(&self) -> State
where
State: Clone,
{
self.select(|state: &State| state.clone()).await
}
pub async fn subscribe<S: Subscriber<State> + Send + 'static>(&self, subscriber: S) {
self.worker_address.send(Subscribe::new(Box::new(subscriber))).await
}
pub async fn wrap<M, OuterAction>(self, middleware: M) -> StoreWithMiddleware<Self, M, State, Action, OuterAction>
where
M: MiddleWare<State, OuterAction, Self, Action> + Send + Sync,
OuterAction: Send + Sync + 'static,
State: Sync,
Action: Sync,
RootReducer: Sync,
{
StoreWithMiddleware::new(self, middleware).await
}
}
#[async_trait]
impl<State, Action, RootReducer> StoreApi<State, Action> for Store<State, Action, RootReducer>
where
Action: Send + Sync + 'static,
RootReducer: Reducer<State, Action> + Send + Sync + 'static,
State: Send + Sync + 'static,
{
async fn dispatch(&self, action: Action) {
Store::dispatch(self, action).await
}
async fn select<S: Selector<State, Result = Result>, Result>(&self, selector: S) -> Result
where
S: Selector<State, Result = Result> + Send + 'static,
Result: Send + 'static,
{
Store::select(self, selector).await
}
async fn state_cloned(&self) -> State
where
State: Clone,
{
Store::state_cloned(self).await
}
async fn subscribe<S: Subscriber<State> + Send + 'static>(&self, subscriber: S) {
Store::subscribe(self, subscriber).await
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::sync::atomic::{AtomicI32, Ordering};
use std::sync::Arc;
#[derive(Clone, Debug, PartialEq)]
struct Counter {
value: i32,
}
impl Counter {
pub fn new(value: i32) -> Self {
Counter { value }
}
}
impl Default for Counter {
fn default() -> Self {
Self { value: 42 }
}
}
struct ValueSelector;
impl Selector<Counter> for ValueSelector {
type Result = i32;
fn select(&self, state: &Counter) -> Self::Result {
state.value
}
}
enum CounterAction {
Increment,
Decrement,
}
fn counter_reducer(state: Counter, action: CounterAction) -> Counter {
match action {
CounterAction::Increment => Counter { value: state.value + 1 },
CounterAction::Decrement => Counter { value: state.value - 1 },
}
}
#[tokio::test]
async fn counter_default_state() {
let store = Store::new(counter_reducer);
assert_eq!(Counter::default(), store.state_cloned().await);
}
#[tokio::test]
async fn counter_supplied_state() {
let store = Store::new_with_state(counter_reducer, Counter::new(5));
assert_eq!(Counter::new(5), store.state_cloned().await);
}
#[tokio::test]
async fn counter_actions_cloned_state() {
let store = Store::new(counter_reducer);
assert_eq!(Counter::new(42), store.state_cloned().await);
store.dispatch(CounterAction::Increment).await;
assert_eq!(Counter::new(43), store.state_cloned().await);
store.dispatch(CounterAction::Increment).await;
assert_eq!(Counter::new(44), store.state_cloned().await);
store.dispatch(CounterAction::Decrement).await;
assert_eq!(Counter::new(43), store.state_cloned().await);
}
#[tokio::test]
async fn counter_actions_selector_struct() {
let store = Store::new(counter_reducer);
assert_eq!(42, store.select(ValueSelector).await);
store.dispatch(CounterAction::Increment).await;
assert_eq!(43, store.select(ValueSelector).await);
store.dispatch(CounterAction::Increment).await;
assert_eq!(44, store.select(ValueSelector).await);
store.dispatch(CounterAction::Decrement).await;
assert_eq!(43, store.select(ValueSelector).await);
}
#[tokio::test]
async fn counter_actions_selector_lambda() {
let store = Store::new(counter_reducer);
assert_eq!(42, store.select(|state: &Counter| state.value).await);
store.dispatch(CounterAction::Increment).await;
assert_eq!(43, store.select(|state: &Counter| state.value).await);
store.dispatch(CounterAction::Increment).await;
assert_eq!(44, store.select(|state: &Counter| state.value).await);
store.dispatch(CounterAction::Decrement).await;
assert_eq!(43, store.select(|state: &Counter| state.value).await);
}
#[tokio::test]
async fn counter_subscribe() {
let store = Store::new(counter_reducer);
assert_eq!(42, store.select(|state: &Counter| state.value).await);
let sum = Arc::new(AtomicI32::new(0));
let captured_sum = sum.clone();
store
.subscribe(move |state: &Counter| {
captured_sum.fetch_add(state.value, Ordering::Relaxed);
})
.await;
store.dispatch(CounterAction::Increment).await;
store.dispatch(CounterAction::Increment).await;
store.dispatch(CounterAction::Decrement).await;
assert_eq!(sum.load(Ordering::Relaxed), 130);
}
}