Last active
January 28, 2016 17:25
-
-
Save everdimension/3f20ae9ce5d03563ebee to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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