let store = null
const listeners = []
export const getStore = () => store
export const subscribe = fn => {
listeners.push(fn)
+ return () => listeners.filter(l => l !== fn) // return `unsubscribe` function
}
export const changeStore = newStore => {
store = newState
- listeners.forEach(listener => listener())
+ listeners.forEach(listener => listener(store))
}
// usage
+ subscribe(store => {
- subscribe(() => {
- const store = getStore()
if (store.name !== this.state.name) {
this.setState({ name: store.name })
}
})
Last active
January 25, 2016 17:41
-
-
Save Nitive/f699d25c6e4cd9c5c4f0 to your computer and use it in GitHub Desktop.
Author
Nitive
commented
Jan 25, 2016
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment