gstore 0.1.0

Global state management for GTK applications
Documentation

gstore

Pipeline status API

Global state management for GTK apps in redux style.

A State can be any kind of data structure an application is based on. For a 'counter' app it might be a struct with a single u32 field. Actions represent the possible features of the app affecting the state. They should be enums.

Implementation

gstore works with two threads: The main UI thread and a background thread for the reducers.

gstore only uses std::sync::mpsc::{Receiver, Sender} for the communication between these threads.

The gtk feature provides a small facade to a bootstrap gtk+libhandy app. It does so by polling every few milliseconds for new actions from the background thread. Thus the UI thread is never blocked.

Usage

#[macro_use]
extern crate lazy_static;

use std::rc::Rc;
use std::sync::Mutex;
use gstore::{ combine_reducers, Store };

#[derive(Debug, Clone)]
struct State {
   count: u32,
}

#[derive(Debug, Clone, Eq, PartialEq)]
enum Action {
    Increment,
    Decrement,
    Shutdown,
}

lazy_static! {
    static ref STATE: Mutex<State> = Mutex::new(State { count: 0 });
}

fn main() {
    let mutex = Mutex::new(State { count: 0 });
    let store: Rc<Store<Action, State>> = Rc::new(Store::new(&STATE));
    let join = combine_reducers(store.clone(), &STATE, |action, state| match action {
        Action::Increment => Some(State {
            count: state.count + 1,
        }),
        Action::Decrement => Some(State {
         count: state.count - 1,
        }),
        Action::Shutdown => None,
    });
    store.send(Action::Increment);
    store.send(Action::Increment);
    store.send(Action::Increment);
    store.send(Action::Decrement);
    store.send(Action::Shutdown);
    join.join().unwrap().expect("Error during Store handling.");
    assert_eq!(STATE.lock().unwrap().count, 2);
}

License

gstore is distributed under the terms of the MIT license. See LICENSE for details.

Acknowledgements

Dan Abramov eveyone who invented/contributes to Redux: https://redux.js.org/.

Thanks for inventing redux.