Created
April 6, 2016 01:28
-
-
Save Nicktho/c0fa0ce115808cc37f56391a3293dd4a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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