Created
April 6, 2017 00:19
-
-
Save mozkoq/3926d967470832b6b0776e2bcf244faa to your computer and use it in GitHub Desktop.
My create store
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
const createStore = (reducer, initialState) => { | |
let state = initialState | |
let listeners = [] | |
const getState = () => state | |
const dispatch = action => { | |
state = reducer(state, action) | |
listeners.forEach(listener => listener()) | |
} | |
const subscribe = subscriber => { | |
listeners = append(subscriber, listeners) | |
return () => | |
listeners = listeners.filter(listener => listener != subscriber) | |
} | |
return { | |
getState, | |
dispatch, | |
subscribe, | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment