Skip to content

Instantly share code, notes, and snippets.

@anotherxx
Created September 5, 2017 10:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anotherxx/da8b1ce3b951553a5e3930e330ad4ddc to your computer and use it in GitHub Desktop.
Save anotherxx/da8b1ce3b951553a5e3930e330ad4ddc to your computer and use it in GitHub Desktop.
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);
console.log(listeners);
return () =>
{
listeners = listeners.filter((cb) =>
{
return cb != listener;
});
}
};
//initial state
dispatch({});
return {getState , dispatch, subscribe};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment