[][src]Trait reducer::Reactor

pub trait Reactor<S> {
    type Output;
    fn react(&self, state: &S) -> Self::Output;
}

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.

Loading content...

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))
    }
}
Loading content...

Implementations on Foreign Types

impl<S, T> Reactor<S> for [T; 0] where
    T: Reactor<S>, 
[src]

Notifies all Reactors in the array in order.

Currently implemented for arrays of up to 32 elements.

type Output = [T::Output; 0]

impl<S, T: ?Sized> Reactor<S> for Box<T> where
    T: Reactor<S>, 
[src]

Forwards the event to the potentially unsized nested Reactor.

type Output = T::Output

impl<S> Reactor<S> for Sender<S> where
    S: Clone
[src]

Turns std::sync::mpsc::Sender into a Reactor.

Warning: this implementation is deprecated.

type Output = Result<(), SendError<S>>

impl<S> Reactor<S> for AsyncSender<S> where
    S: Clone
[src]

Turns futures::channel::mpsc::Sender into a Reactor (requires async).

type Output = Result<(), AsyncSendError>

impl<S> Reactor<S> for UnboundedSender<S> where
    S: Clone
[src]

type Output = Result<(), TrySendError<S>>

impl<S, T> Reactor<S> for Option<T> where
    T: Reactor<S>, 
[src]

Forwards the event if Some, ignores if None.

Warning: this implementation is deprecated.

type Output = Option<T::Output>

impl<'a, S, T: ?Sized> Reactor<S> for &'a T where
    T: Reactor<S>, 
[src]

Forwards the event to a potentially stack allocated Reactor.

type Output = T::Output

impl<S, T> Reactor<S> for [T] where
    T: Reactor<S>, 
[src]

Notifies all Reactors in the slice in order.

type Output = Box<[T::Output]>

impl<S, _01> Reactor<S> for (_01,) where
    _01: Reactor<S>, 
[src]

Notifies all Reactors in the tuple in order.

Currently implemented for tuples of up to 12 elements.

type Output = (_01::Output,)

Loading content...

Implementors

Loading content...