Skip to content

Instantly share code, notes, and snippets.

@DejanBelic
Last active February 3, 2019 11:34
Show Gist options
  • Save DejanBelic/f9bb07693058e5af8c4b14ef7f116212 to your computer and use it in GitHub Desktop.
Save DejanBelic/f9bb07693058e5af8c4b14ef7f116212 to your computer and use it in GitHub Desktop.
Redux store
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