Skip to content

Instantly share code, notes, and snippets.

@everdimension
Last active January 28, 2016 17:25
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 everdimension/3f20ae9ce5d03563ebee to your computer and use it in GitHub Desktop.
Save everdimension/3f20ae9ce5d03563ebee to your computer and use it in GitHub Desktop.
class Store {
constructor(reducer, initialState) {
this.reducer = reducer;
this.state = this.reducer(initialState, {});
console.log('set state', this.state);
this.listeners = [];
}
getState() {
return this.state;
}
subscribe(listener) {
this.listeners.push(listener);
}
dispatch(action) {
this.state = this.reducer(this.state, action);
this.listeners.forEach(listener => listener());
}
unsubscribe(listener) {
this.listeners = this.listeners.filter((currListener) => {
return currListener !== listener;
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment