elm inspired state manager in rust
```bash git clone github.com/AlexanderChen1989/halo cd halo cargo run --example counter
```rust
pub struct Model {
num: i64,
others: Vec
impl Model { pub fn new(num: i64) -> Model { Model { num: num, others: vec![], } } } ```
```rust
pub enum Msg { Incr(i64), Decr(i64), Add(i64), Nothing, }
pub fn update(msg: Msg, model: Model) -> Model { let mut model = model; match msg { Msg::Incr(v) => { model.num += v; model } Msg::Decr(v) => { model.num -= v; model } Msg::Add(v) => { model.others.push(v); model } Msg::Nothing => model, } } ```
```rust
pub struct Stores {
pub a: Store
impl Stores { pub fn new() -> Stores { Stores { a: Store::new(Model::new(10), update), b: Store::new(Model::new(20), update), } } } ```
```rust extern crate halo;
mod stores; mod counter;
use stores::Stores; use counter::{Model, Msg};
fn main() { let mut stores = Stores::new();
// subsribe function to listen store's model change
stores.a.subscribe(sub);
stores.b.subscribe(sub);
// dispatch action(Msg) to store
stores.a.dispatch(Msg::Incr(10));
stores.a.dispatch(Msg::Nothing);
stores.b.dispatch(Msg::Decr(20));
stores.b.dispatch(Msg::Add(100));
fn sub(model: &Model) {
println!("Sub {:?}", model);
}
} ```
bash
cargo run --example counter
...
Sub Model { num: 20, others: [] }
Sub Model { num: 0, others: [] }
Sub Model { num: 0, others: [100] }