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
use crate::{Store, StoreContext};
use std::{
any::Any,
cell::{Ref, RefCell},
ops::Deref,
rc::Rc,
};
pub(crate) struct Subscriptions<T> {
pub(crate) states: Vec<Rc<dyn Any>>,
pub(crate) subscriptions: Vec<Box<dyn (Fn(Rc<dyn Any>, &T) -> Rc<dyn Any>)>>,
pub(crate) ref_subscriptions: Vec<Box<dyn (Fn(&T, &T) -> bool)>>,
}
/// Handle exposing subscriptions to the store.
pub struct UseStoreHandle<T: 'static> {
pub(crate) context: StoreContext<T>,
pub(crate) subscriptions: Rc<RefCell<Subscriptions<T>>>,
}
impl<T: 'static> UseStoreHandle<T> {
/// (Hook) Subscribe to the store and return the value mapped.
/// As opposed to `map_ref`, `map` is a hook and is therefore constrained to certain rules:
/// - Should only be called inside Yew function components.
/// - Should not be called inside loops, conditions or nested functions.
///
/// If you only wish to reference a value owned by the store, you should use `map_ref` instead.
/// A change to the observed value will re-render the component.
///
/// ```rust
/// use yew::prelude::*;
/// use yewv::*;
///
/// struct StoreState {
/// value: i32
/// }
///
/// #[function_component(Test)]
/// fn test() -> Html {
/// let store = use_store::<StoreState>();
/// let value = store.map(|state| state.value);
///
/// html!{ { value } }
/// }
/// ```
pub fn map<M: PartialEq + 'static>(&self, map: impl Fn(&T) -> M + 'static) -> Rc<M> {
let mut subscriptions = self.subscriptions.borrow_mut();
let current_index = subscriptions.subscriptions.len();
let value = match subscriptions.states.get(current_index) {
Some(s) => s
.clone()
.downcast()
.expect("Store map was called in a different order."),
None => {
let state = Rc::new(map(&self.state_ref()));
subscriptions.states.push(state.clone());
state
}
};
subscriptions
.subscriptions
.push(Box::new(move |prev, next| {
let next = map(next);
let prev = prev
.downcast::<M>()
.expect("Store map was called in a different order.");
if next.ne(&prev) {
return Rc::new(next);
}
prev
}));
value
}
/// Subscribe to the store and return a reference to the value mapped.
/// A change to the observed value will re-render the component.
/// ```rust
/// use yew::prelude::*;
/// use yewv::*;
///
/// struct StoreState {
/// value: i32
/// }
///
/// #[function_component(Test)]
/// fn test() -> Html {
/// let store = use_store::<StoreState>();
/// let value = store.map_ref(|state| &state.value);
///
/// html!{ { value } }
/// }
/// ```
pub fn map_ref<'a, M: PartialEq + 'a>(&self, map: impl Fn(&T) -> &M + 'static) -> Ref<M> {
let value = Ref::map(self.state_ref(), |s| map(s));
self.subscriptions
.borrow_mut()
.ref_subscriptions
.push(Box::new(move |prev, next| map(prev) != map(next)));
value
}
/// Subscribe to a specific store value.
/// A change to the observed value will re-render the component.
/// ```rust
/// use yew::prelude::*;
/// use yewv::*;
///
/// struct StoreState {
/// value: i32
/// }
///
/// #[function_component(Test)]
/// fn test() -> Html {
/// let store = use_store::<StoreState>();
/// store.watch_ref(|state| &state.value);
///
/// html!{ { store.state().value } }
/// }
/// ```
pub fn watch_ref<W: PartialEq>(&self, watch: impl Fn(&T) -> &W + 'static) {
self.subscriptions
.borrow_mut()
.ref_subscriptions
.push(Box::new(move |prev, next| watch(prev) != watch(next)));
}
/// (Hook) Subscribe to a specific store value.
/// As opposed to `watch_ref`, `watch` is a hook and is therefore constrained to certain rules:
/// - Should only be called inside Yew function components.
/// - Should not be called inside loops, conditions or nested functions.
///
/// A change to the observed value will re-render the component.
/// ```rust
/// use yew::prelude::*;
/// use yewv::*;
///
/// struct StoreState {
/// value: i32
/// }
///
/// #[function_component(Test)]
/// fn test() -> Html {
/// let store = use_store::<StoreState>();
/// store.watch(|state| state.value);
///
/// html!{ { store.state().value } }
/// }
/// ```
pub fn watch<W: PartialEq + 'static>(&self, watch: impl Fn(&T) -> W + 'static) {
let mut subs = self.subscriptions.borrow_mut();
if subs.states.len() == subs.subscriptions.len() {
subs.states.push(Rc::new(watch(&self.state_ref())));
}
subs.subscriptions.push(Box::new(move |prev, next| {
let next = watch(next);
let current = prev
.downcast::<W>()
.expect("Store watch was called in a different order");
if next.ne(¤t) {
return Rc::new(next);
}
current
}));
}
}
impl<T> Deref for UseStoreHandle<T> {
type Target = Rc<Store<T>>;
fn deref(&self) -> &Self::Target {
&self.context.store
}
}