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
use std::sync::{Arc, RwLock};
use once_cell::sync::Lazy;
use crate::{Sender, RUNTIME};
use super::SubscriberFn;
/// A trait that implements a reducer function.
///
/// For more information, see [`Reducer`].
pub trait Reducible {
/// The input message type used to modify the data.
type Input;
/// Initialize the data.
fn init() -> Self;
/// Process the input message and update the state.
///
/// Return [`true`] to notify all subscribers.
/// Return [`false`] to ignore all subscribers.
///
/// For example, it makes sense to return [`false`] to indicate
/// that the message had no (noteworthy) effect on the data and
/// the subscribers don't need to be notified.
fn reduce(&mut self, input: Self::Input) -> bool;
}
struct ReducerInner<Data: Reducible> {
sender: Sender<Data::Input>,
subscribers: Arc<RwLock<Vec<SubscriberFn<Data>>>>,
}
impl<Data> Default for ReducerInner<Data>
where
Data: Reducible + Send + 'static,
Data::Input: Send,
{
fn default() -> Self {
let (sender, receiver) = crate::channel();
let subscribers: Arc<RwLock<Vec<SubscriberFn<Data>>>> = Arc::default();
let rt_subscribers = subscribers.clone();
RUNTIME.spawn(async move {
let mut data = Data::init();
while let Some(input) = receiver.recv().await {
if data.reduce(input) {
// Remove all elements which had their senders dropped.
rt_subscribers
.write()
.unwrap()
.retain(|subscriber| subscriber(&data));
}
}
});
Self {
sender,
subscribers,
}
}
}
impl<Data> std::fmt::Debug for ReducerInner<Data>
where
Data: std::fmt::Debug + Reducible,
{
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("ReducerInner")
.field("sender", &self.sender)
.field("subscribers", &self.subscribers.try_read().map(|s| s.len()))
.finish()
}
}
/// A type that allows you to share information across your
/// application easily.
///
/// Reducers receive messages, update their state accordingly
/// and notify their subscribers.
///
/// Unlike [`SharedState`](super::SharedState), this type doesn't
/// allow direct access to the internal data.
/// Instead, it updates its state after receiving messages, similar to components.
/// After the message is processed, all subscribers will be notified.
///
/// # Example
///
/// ```
/// use relm4::{Reducer, Reducible};
///
/// struct CounterReducer(u8);
///
/// enum CounterInput {
/// Increment,
/// Decrement,
/// }
///
/// impl Reducible for CounterReducer {
/// type Input = CounterInput;
///
/// fn init() -> Self {
/// Self(0)
/// }
///
/// fn reduce(&mut self, input: Self::Input) -> bool {
/// match input {
/// CounterInput::Increment => {
/// self.0 += 1;
/// }
/// CounterInput::Decrement => {
/// self.0 -= 1;
/// }
/// }
/// true
/// }
/// }
///
/// // Create the reducer.
/// static REDUCER: Reducer<CounterReducer> = Reducer::new();
///
/// // Update the reducer.
/// REDUCER.emit(CounterInput::Increment);
/// # use std::time::Duration;
/// # std::thread::sleep(Duration::from_millis(10));
///
/// // Create a channel and subscribe to changes.
/// let (sender, receiver) = relm4::channel();
/// REDUCER.subscribe(&sender, |data| data.0);
///
/// // Count up to 2.
/// REDUCER.emit(CounterInput::Increment);
/// assert_eq!(receiver.recv_sync().unwrap(), 2);
/// ```
#[derive(Debug)]
pub struct Reducer<Data: Reducible> {
inner: Lazy<ReducerInner<Data>>,
}
impl<Data> Default for Reducer<Data>
where
Data: Reducible + Send + 'static,
Data::Input: Send,
{
fn default() -> Self {
Self::new()
}
}
impl<Data> Reducer<Data>
where
Data: Reducible + Send + 'static,
Data::Input: Send,
{
/// Create a new [`Reducer`] variable.
///
/// The data will be initialized lazily on the first access.
#[must_use]
pub const fn new() -> Self {
Self {
inner: Lazy::new(ReducerInner::default),
}
}
/// Subscribe to a [`Reducer`].
/// Any subscriber will be notified with a message every time
/// you modify the reducer (by calling [`Self::emit()`]).
pub fn subscribe<Msg, F>(&self, sender: &Sender<Msg>, f: F)
where
F: Fn(&Data) -> Msg + 'static + Send + Sync,
Msg: Send + 'static,
{
let sender = sender.clone();
self.inner
.subscribers
.write()
.unwrap()
.push(Box::new(move |data: &Data| {
let msg = f(data);
sender.send(msg).is_ok()
}));
}
/// An alternative version of [`subscribe()`](Self::subscribe()) that only send a message if
/// the closure returns [`Some`].
pub fn subscribe_optional<Msg, F>(&self, sender: &Sender<Msg>, f: F)
where
F: Fn(&Data) -> Option<Msg> + 'static + Send + Sync,
Msg: Send + 'static,
{
let sender = sender.clone();
self.inner
.subscribers
.write()
.unwrap()
.push(Box::new(move |data: &Data| {
if let Some(msg) = f(data) {
sender.send(msg).is_ok()
} else {
true
}
}));
}
/// Sends a message to the reducer to update its state.
///
/// If the [`Reducible::reduce()`] method returns [`true`],
/// all subscribers will be notified.
pub fn emit(&self, input: Data::Input) {
assert!(
self.inner.sender.send(input).is_ok(),
"Reducer runtime was dropped. Maybe a subscriber or the update function panicked?"
);
}
}
#[cfg(test)]
mod test {
use std::time::Duration;
use super::{Reducer, Reducible};
struct CounterReducer(u8);
enum CounterInput {
Increment,
Decrement,
}
impl Reducible for CounterReducer {
type Input = CounterInput;
fn init() -> Self {
Self(0)
}
fn reduce(&mut self, input: Self::Input) -> bool {
match input {
CounterInput::Increment => {
self.0 += 1;
}
CounterInput::Decrement => {
self.0 -= 1;
}
}
true
}
}
static REDUCER: Reducer<CounterReducer> = Reducer::new();
#[test]
fn shared_state() {
// Count up to 3 and wait for events to be processed.
REDUCER.emit(CounterInput::Increment);
REDUCER.emit(CounterInput::Increment);
REDUCER.emit(CounterInput::Increment);
std::thread::sleep(Duration::from_millis(10));
let (sender, receiver) = crate::channel();
REDUCER.subscribe(&sender, |data| data.0);
// Count up to 4 with receiver.
REDUCER.emit(CounterInput::Increment);
assert_eq!(receiver.recv_sync().unwrap(), 4);
// Count down to 3.
REDUCER.emit(CounterInput::Decrement);
assert_eq!(receiver.recv_sync().unwrap(), 3);
}
}