Skip to content

Instantly share code, notes, and snippets.

@anotherxx
Created August 11, 2017 06:28
Show Gist options
  • Save anotherxx/d9dce3062e75a9972f54c552d2d0cb38 to your computer and use it in GitHub Desktop.
Save anotherxx/d9dce3062e75a9972f54c552d2d0cb38 to your computer and use it in GitHub Desktop.
Redux implementation
const createStore = (reducer) =>
{
let state;
let listeners = [];
const getState = () => state;
const dispatch = (action) =>
{
state = reducer(state , action);
listeners.forEach((cb) => cb());
};
const subscribe = (listener) =>
{
listeners.push(listener);
return () => {
listeners = listeners.filter(cb => cb !== listener);
return listeners;
}
}
dispatch({})
return {getState , dispatch , subscribe};
}
let store = createStore(counter);
store.dispatch('INCREMENT');
store.subscribe(() =>
{
console.log("Callback1");
});
store.subscribe(() =>
{
console.log("Callback2");
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment