Skip to content

Instantly share code, notes, and snippets.

@Nicktho
Created April 6, 2016 01:28
Show Gist options
  • Save Nicktho/c0fa0ce115808cc37f56391a3293dd4a to your computer and use it in GitHub Desktop.
Save Nicktho/c0fa0ce115808cc37f56391a3293dd4a to your computer and use it in GitHub Desktop.
export function createStore(reducer, initialState) {
let listeners = [];
let currentState = initialState;
function getState() {
return currentState;
}
function subscribe(listener) {
listeners.push(listener);
}
function dispatch(action) {
currentState = reducer(currentState, action);
listeners.forEach(listener => listener());
}
dispatch({ type: 'INIT' });
return { getState, subscribe, dispatch };
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment