Skip to content

Instantly share code, notes, and snippets.

@radum
Created March 6, 2018 09:01
Show Gist options
  • Save radum/830ea74021716fbe8f227ef479dae0e8 to your computer and use it in GitHub Desktop.
Save radum/830ea74021716fbe8f227ef479dae0e8 to your computer and use it in GitHub Desktop.
developit/unistore Example
import createStore from "unistore";
function mapActions(actions, store) {
if (typeof actions === "function") actions = actions(store);
let mapped = {};
for (let i in actions) {
mapped[i] = store.action(actions[i]);
}
return mapped;
}
let store = createStore({ count: 0 });
let actions = {
increment: state => {
return { count: state.count + 1 };
}
};
console.log(store.getState());
var stateActions = mapActions(actions, store);
store.subscribe(function(state, action) {
var actionName = (action && action.name) || "setState";
console.log("state: " + JSON.stringify(state));
console.log("actionName: " + actionName);
});
stateActions.increment();
stateActions.increment();
stateActions.increment();
console.log(store.getState());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment