Crate reducer

source ·
Expand description

A predictable reactive framework for Rust apps.

Overview

Reducer is inspired by the Flux pattern, popular in the JavaScript community as an effective idiom to build scalable and maintainable apps.

The core mental model behind Reducer is the unidirectional data flow depicted below.

               Reducer               |
+------------------------------------|-----------------+
|                                    |                 |
|    ----------        ---------     |      --------   |
+--> | Action | -----> | Store | --- | ---> | View | --+
     ----------        ---------     |      --------
                                     |

The view, often a [G]UI, dispatches actions on the store, which in turn updates its internal state and notifies back the view.

Usage

extern crate reducer;

use reducer::*;
use std::io::{self, Write};

// The state of your app.
struct Calculator(i32);

// Actions the user can trigger.
struct Add(i32);
struct Sub(i32);
struct Mul(i32);
struct Div(i32);

impl Reducer<Add> for Calculator {
    fn reduce(&mut self, Add(x): Add) {
        self.0 += x;
    }
}

impl Reducer<Sub> for Calculator {
    fn reduce(&mut self, Sub(x): Sub) {
        self.0 -= x;
    }
}

impl Reducer<Mul> for Calculator {
    fn reduce(&mut self, Mul(x): Mul) {
        self.0 *= x;
    }
}

impl Reducer<Div> for Calculator {
    fn reduce(&mut self, Div(x): Div) {
        self.0 /= x;
    }
}

// The user interface.
struct Display;

impl Reactor<Calculator> for Display {
    type Output = io::Result<()>;
    fn react(&self, state: &Calculator) -> Self::Output {
        io::stdout().write_fmt(format_args!("{}\n", state.0))
    }
}

fn main() {
    let mut store = Store::new(Calculator(0), Display);

    store.dispatch(Add(5)).unwrap(); // displays "5"
    store.dispatch(Mul(3)).unwrap(); // displays "15"
    store.dispatch(Sub(1)).unwrap(); // displays "14"
    store.dispatch(Div(7)).unwrap(); // displays "2"
}

Structs

A reactive state container that manages the state of your application.

Traits

Trait for types that react to state transitions.
Trait for types that represent the logical state of an application.