0.0.3 • Published 7 years ago

@nathanfaucett/store v0.0.3

Weekly downloads
1
License
MIT
Repository
github
Last release
7 years ago

store Build Status

redux like lib

var createStore = require("@nathanfaucett/store");


var count = 0,
    store = createStore();

store.subscribe(function onDispatch(state, action) {
    console.log(state, action);
});

store.addMiddleware(function counterMiddleware(store, action, next) {
    switch (action.type) {
        case "INC":
            store.dispatch({
                type: "INC_DONE",
                count: ++count
            });
            break;
        case "DEC":
            store.dispatch({
                type: "DEC_DONE",
                count: --count
            });
            break;
    }

    next(action);
});
store.add(function counter(state, action) {
    switch (action.type) {
        case "INC_DONE":
            return {
                count: action.count
            };
        case "DEC_DONE":
            return {
                count: action.count
            };
        default:
            return state;
    }
});

store.setInitialState({
    counter: {
        count: 0
    }
});

store.dispatch({
    type: "INC"
});
store.dispatch({
    type: "INC"
});
store.dispatch({
    type: "DEC"
});
store.dispatch({
    type: "DEC"
});