Last active
February 3, 2019 11:34
-
-
Save DejanBelic/f9bb07693058e5af8c4b14ef7f116212 to your computer and use it in GitHub Desktop.
Redux 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
function createStore(reducer) { | |
// Store should have 4 parts. | |
// 1. The state. | |
// 2. Get the state. | |
// 3. Listen to state change. | |
// 4. Update the state. | |
let state; | |
let listeners = []; | |
const getState = () => state; | |
const subscribe = (listener) => { | |
listeners.push(listener); | |
return () => { | |
listeners = listeners.filter(l => l !== listener); | |
}; | |
}; | |
const dispatch = (action) => { | |
state = reducer(state, action); | |
listeners.forEach(listener => listener()); | |
}; | |
return { | |
getState, | |
subscribe, | |
dispatch, | |
}; | |
} | |
// Combine reducers. | |
function app(state = {}, action) { | |
return { | |
todos: todos(state.todos, action), | |
goals: goals(state.goals, action), | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment