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
use std::{
cell::{Ref, RefCell},
rc::Rc,
};
/// Simple store with subscription capability.
pub struct Store<T> {
previous_state: RefCell<Rc<T>>,
state: RefCell<Rc<T>>,
subscriptions: RefCell<Vec<Box<dyn Fn(&T, &T) -> bool>>>,
}
impl<T> Store<T> {
/// Create a new instance of a store with the given state as initial state.
/// ```rust
/// use yewv::Store;
///
/// let store = Store::new(0);
/// assert_eq!(*store.state(), 0);
/// ```
pub fn new(initial_state: T) -> Self {
let state = Rc::new(initial_state);
Self {
previous_state: RefCell::new(state.clone()),
state: RefCell::new(state),
subscriptions: RefCell::new(vec![]),
}
}
/// Give a reference to the current store state.
/// ```rust
/// use yewv::Store;
///
/// let store = Store::new(0);
/// assert_eq!(*store.state(), 0);
/// store.set_state(1);
/// assert_eq!(*store.state(), 1);
/// ```
pub fn state(&self) -> Rc<T> {
self.state.borrow().clone()
}
/// Set store next state.
/// ```rust
/// use yewv::Store;
///
/// let store = Store::new(0);
/// assert_eq!(*store.state(), 0);
/// store.set_state(1);
/// assert_eq!(*store.state(), 1);
/// ```
pub fn set_state(&self, new_state: T) {
{
let mut state = self.state.borrow_mut();
*self.previous_state.borrow_mut() = state.clone();
*state = Rc::new(new_state);
}
self.notify();
}
/// Subscibe to changes made to the store state.
/// Your subscription will stay active as long as your `callback` returns `true`.
/// When the `callback` returns `false` the subscription will be dropped.
/// ```rust
/// use yewv::Store;
///
/// let store = Store::new(0);
/// store.subscribe(|prev_state, current_state| {
/// /* Put your own subscription logic. */
/// true // Should be the condition for unsubscription.
/// } );
/// ```
pub fn subscribe(&self, callback: impl Fn(&T, &T) -> bool + 'static) {
self.subscriptions.borrow_mut().push(Box::from(callback));
}
pub(crate) fn notify(&self) {
let mut subs = std::mem::take(&mut *self.subscriptions.borrow_mut());
let prev = &self.previous_state.borrow();
let next = &self.state_ref();
subs.retain(|s| s(prev, next));
self.subscriptions.borrow_mut().append(&mut subs);
}
pub(crate) fn state_ref(&self) -> Ref<Rc<T>> {
self.state.borrow()
}
}
#[cfg(test)]
mod tests {
use super::*;
struct TestContext<T> {
notified_values: Rc<RefCell<Vec<(T, T)>>>,
is_sub_active: Rc<RefCell<bool>>,
store: Store<T>,
}
fn setup<T: Clone + 'static>(initial_state: T) -> TestContext<T> {
let store = Store::new(initial_state);
let notified_values = Rc::new(RefCell::new(vec![]));
let is_sub_active = Rc::new(RefCell::new(true));
store.subscribe({
let notified_values = notified_values.clone();
let is_sub_active = is_sub_active.clone();
move |prev, next| {
notified_values
.borrow_mut()
.push((prev.clone(), next.clone()));
*is_sub_active.borrow()
}
});
TestContext {
notified_values,
is_sub_active,
store,
}
}
#[test]
fn set_state_with_new_state_should_update_current_state() {
//Given
let ctx = setup(0);
//When
ctx.store.set_state(1);
//Then
assert_eq!(*ctx.store.state(), 1);
}
#[test]
fn set_state_with_new_state_should_update_previous_state() {
//Given
let ctx = setup(0);
ctx.store.set_state(1);
//When
ctx.store.set_state(2);
//Then
assert_eq!(**ctx.store.previous_state.borrow(), 1);
}
#[test]
fn set_state_with_new_state_should_notify() {
//Given
let ctx = setup(0);
//When
ctx.store.set_state(1);
//Then
assert_eq!(*ctx.notified_values.borrow(), &[(0, 1)]);
}
#[test]
fn set_state_with_subscription_no_longer_active_should_no_longer_notify() {
//Given
let ctx = setup(0);
*ctx.is_sub_active.borrow_mut() = false;
ctx.store.set_state(1);
let notify_count = ctx.notified_values.borrow().len();
//When
ctx.store.set_state(2);
//Then
assert_eq!(ctx.notified_values.borrow().len(), notify_count);
}
#[test]
fn set_state_with_subscription_no_longer_active_should_drop_subscription() {
//Given
let ctx = setup(0);
let sub_count = ctx.store.subscriptions.borrow().len();
*ctx.is_sub_active.borrow_mut() = false;
ctx.store.set_state(1);
//When
ctx.store.set_state(2);
//Then
assert_eq!(ctx.store.subscriptions.borrow().len(), sub_count - 1);
}
#[test]
fn subscribe_with_callback_should_add_callback_to_subscriptions() {
//Given
let ctx = setup(0);
let sub_count = ctx.store.subscriptions.borrow().len();
//When
ctx.store.subscribe(|_, _| false);
//Then
assert_eq!(ctx.store.subscriptions.borrow().len(), sub_count + 1);
}
}