Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save factoryhr/b6d23eb76b1ddc39717b4f065c1f1673 to your computer and use it in GitHub Desktop.
Save factoryhr/b6d23eb76b1ddc39717b4f065c1f1673 to your computer and use it in GitHub Desktop.
function createStore(reducer, preloadedState) {
var currentState = preloadedState;
// Keep track of all listeners
var listeners = [];
function getState() {
return currentState;
}
// Subscribe new listeners to the store
function subscribe(listener) {
listeners.push(listener);
// Provide method for removing the subscription
return function unsubscribe() {
var index = listeners.indexOf(listener);
listeners.splice(index, 1);
};
}
return {
getState: getState,
// Expose the API
subscribe: subscribe
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment