Created
December 16, 2016 14:53
-
-
Save inooid/c9e3709da0098605b3edb68b278099e3 to your computer and use it in GitHub Desktop.
Simple Redux source code... kinda
This file contains hidden or 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
function createStore(reducer, initialState) { | |
let currentState = initialState; | |
let listeners = []; | |
function getState() { | |
return currentState; | |
} | |
function dispatch(action) { | |
// Create the state from the reducer | |
currentState = reducer(currentState, action); | |
// Notify listeners | |
listeners.forEach(listener => { | |
listener(); | |
}); | |
return action; | |
} | |
function subscribe(callback) { | |
listeners.push(callback); | |
} | |
return { | |
getState, | |
dispatch, | |
subscribe, | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment