[−][src]Trait reducer::Reactor
Trait for types that react to state transitions.
Reactors connect the state to the view components. They can implement arbitrary logic in response to state transitions, but it's often better to think of Reactors as channels that transmit the current state to other parts of your application.
Reactor as a Data Channel
For GUI applications, it is a good practice to have a separate thread dedicated to rendering.
To help wiring up the Flux pattern in such multi-threaded scenarios, Reactor is implemented
for mpsc::Sender
out of the box.
Example
use reducer::*; fn main() { // Create a channel for the current state. let (tx, rx) = std::sync::mpsc::channel(); // Start the rendering thread. std::thread::spawn(move || { while let Ok(Countdown(t)) = rx.recv() { // Render the current state to the screen. match t { 6 => println!("T-6 seconds - Main engine start."), 0 => println!("T-0 seconds - Solid rocket booster ignition and liftoff!"), t if t > 0 => println!("T-{} seconds", t), _ => break, } } }); #[derive(Clone)] struct Countdown(i32); struct Tick; impl Reducer<Tick> for Countdown { fn reduce(&mut self, _: Tick) { self.0 -= 1; } } // Set-up the initial state. let mut store = Store::new(Countdown(10), tx); // Count down to liftoff! while let Ok(()) = store.dispatch(Tick) {} }
Associated Types
type Output
The result of reacting to S
.
Required methods
fn react(&self, state: &S) -> Self::Output
Reacts to S
and produces Self::Output
.
Example
use reducer::*; use std::fmt::Debug; use std::io::{self, Write}; struct Console; impl<T: Debug> Reactor<T> for Console { type Output = io::Result<()>; fn react(&self, state: &T) -> Self::Output { io::stdout().write_fmt(format_args!("{:?}\n", state)) } }
Implementations on Foreign Types
impl<S, T> Reactor<S> for [T; 0] where
T: Reactor<S>,
[src]
T: Reactor<S>,
Notifies all reactors in the array in order.
Currently implemented for arrays of up to 32 elements.
impl<S, T: ?Sized> Reactor<S> for Box<T> where
T: Reactor<S>,
[src]
T: Reactor<S>,
Forwards the event to the potentially unsized nested reactor.
impl<S, T> Reactor<S> for Option<T> where
T: Reactor<S>,
[src]
T: Reactor<S>,
Forwards the event if Some
, ignores if None
.
impl<'a, S, T: ?Sized> Reactor<S> for &'a T where
T: Reactor<S>,
[src]
T: Reactor<S>,
Forwards the event to a potentially stack allocated reactor.
impl<S> Reactor<S> for Sender<S> where
S: Clone,
[src]
S: Clone,
impl<S, T> Reactor<S> for [T] where
T: Reactor<S>,
[src]
T: Reactor<S>,
Notifies all reactors in the slice in order.
impl<S, _01> Reactor<S> for (_01,) where
_01: Reactor<S>,
[src]
_01: Reactor<S>,
Notifies all reactors in the tuple in order.
Currently implemented for tuples of up to 12 elements.