Created
April 19, 2016 20:00
-
-
Save nqvst/9f08358bbec65d3076ca4da3bca4d523 to your computer and use it in GitHub Desktop.
redux CreateStore
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
const createStore = (reducer) => { | |
let state; | |
let listeners = []; | |
const getState = () => { | |
return 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