Skip to content

Instantly share code, notes, and snippets.

@wehrhaus
Last active May 18, 2017 14:33
Show Gist options
  • Save wehrhaus/688f1d25fa4aaadcd55e1d7e3db303f7 to your computer and use it in GitHub Desktop.
Save wehrhaus/688f1d25fa4aaadcd55e1d7e3db303f7 to your computer and use it in GitHub Desktop.
createStore
const createStore = (reducer) => {
let state;
let listeners = [];
const getState = () => state;
const dispatch = (action) => {
state = reducer(state, action);
listeners.forEach((listener) => listener());
};
const subscribe = (listener) => {
listeners.push(listener);
return () => {
listeners = listeners.filter(l => l !== listener);
}
};
dispatch({});
return { getState, dispatch, subscribe };
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment