Skip to content

Instantly share code, notes, and snippets.

@sarjumulmi
Last active March 29, 2018 20:12
Show Gist options
  • Save sarjumulmi/3103f444bb131cf765db997ff289b197 to your computer and use it in GitHub Desktop.
Save sarjumulmi/3103f444bb131cf765db997ff289b197 to your computer and use it in GitHub Desktop.
export const createStore = (reducer, initialState = {}) => {
let currentState = initialState;
const listeners = [];
function getState() {
return currentState;
}
function dispatch(action) {
currentState = reducer(currentState, action);
listeners.forEach(listener => listener());
}
function subscribe(listener) {
listeners.push(listener);
}
return {
getState,
dispatch,
subscribe
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment