const createStore = (rootReducer) => {
let state
let listeners
const getState = (state) => state
const dispatch = (action) => {
state = rootReducer(action, state)
listeners.forEach(l => l())
}
const subscribe = (listener) => {
listeners.push(listener)
return function unsubscribe() {
listeners.filter(l => l !=== listener)
}
}
return { dispatch, subscribe, getState }
}
export default createStore
Last active
September 12, 2019 08:08
-
-
Save lxynox/72a61829261f343fc0105c6325284b81 to your computer and use it in GitHub Desktop.
simple redux
const logger = (store) => (next) => (action) => {
console.group()
console.log(‘next action’, action)
next(action)
console.log(‘next state’, store.getState())
console.groupEnd()
}
const thunk = (store) => (next) => (action) => {
typeof action === ‘function’
? action(store.dispatch, store.getState)
: next(action)
}
const applyMiddlewares = (store, middlewares) => {
middlewares = middlewares.slice()
middlewares.reverse()
const { dispatch } = store
middlewares.forEach(middleware => {
dispatch = middleware(store)(dispatch)
}
return Object.assign({}, store, { dispatch })
}
export default applyMiddlewares
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment