Skip to content

Instantly share code, notes, and snippets.

@sword-jin
Created May 1, 2016 15:18
Show Gist options
  • Save sword-jin/5787c5adb3d3439c1063cb218bd734bd to your computer and use it in GitHub Desktop.
Save sword-jin/5787c5adb3d3439c1063cb218bd734bd to your computer and use it in GitHub Desktop.
redux createStore code
const createStore = (reducer) => {
let state;
let listeners = [];
const getState = () => state;
const dispatch = action => {
state = reducer(state, action);
listeners.forEach(l => l());
}
const subscribe = listener => {
listeners.push(listener);
return () => {
listeners = listeners.filter(l => l !== listener);
}
}
dispatch({});
return {getState, dispatch, subscribe};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment