Skip to content

Instantly share code, notes, and snippets.

@Swizec
Created January 12, 2018 07:37
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 Swizec/95bc5e5ce8f40c4b2954e55e411d7332 to your computer and use it in GitHub Desktop.
Save Swizec/95bc5e5ce8f40c4b2954e55e411d7332 to your computer and use it in GitHub Desktop.
function createStore(reducer, preloadedState) {
let state = preloadedState;
let listeners = [];
const subscribe = (callback) => {
listeners = [...listeners, callback];
return function () {
listeners = listeners.filter(f => f !== callback);
}
}
const dispatch = (action) => {
state = reducer(state, action);
listeners.forEach(f => f(state));
}
return {
subscribe,
dispatch
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment