Skip to content

Instantly share code, notes, and snippets.

@andrIvash
Created November 23, 2017 20:57
Show Gist options
  • Select an option

  • Save andrIvash/88af5e6ad01e0e9c414e56bdba731ac5 to your computer and use it in GitHub Desktop.

Select an option

Save andrIvash/88af5e6ad01e0e9c414e56bdba731ac5 to your computer and use it in GitHub Desktop.
createStore
const createStore = (reducer) => {
let state;
let listeners = [];
const getState = () => state;
const dispatch = (action) => {
state = reducer(state, action);
listeners.forEach(listener => listener());
}
const subscribe = (listener) => {
listeners.push(listener);
return () => {
listeners = listeners.filter(l => l !== listener);
};
}
dispatch({});
return { dispatch, getState, subscribe };
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment