Created
August 11, 2017 06:28
-
-
Save anotherxx/d9dce3062e75a9972f54c552d2d0cb38 to your computer and use it in GitHub Desktop.
Redux implementation
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 = () => state; | |
const dispatch = (action) => | |
{ | |
state = reducer(state , action); | |
listeners.forEach((cb) => cb()); | |
}; | |
const subscribe = (listener) => | |
{ | |
listeners.push(listener); | |
return () => { | |
listeners = listeners.filter(cb => cb !== listener); | |
return listeners; | |
} | |
} | |
dispatch({}) | |
return {getState , dispatch , subscribe}; | |
} | |
let store = createStore(counter); | |
store.dispatch('INCREMENT'); | |
store.subscribe(() => | |
{ | |
console.log("Callback1"); | |
}); | |
store.subscribe(() => | |
{ | |
console.log("Callback2"); | |
}) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment