Skip to content

Instantly share code, notes, and snippets.

@eikeon
Last active December 21, 2015 23:09
Show Gist options
  • Save eikeon/6380435 to your computer and use it in GitHub Desktop.
Save eikeon/6380435 to your computer and use it in GitHub Desktop.
type stateChanged struct {
states chan State
register chan *chan State
unregister chan *chan State
observers map[*chan State]bool
}
func (sc *stateChanged) Register(c *chan State) {
sc.register <- c
}
func (sc *stateChanged) Unregister(c *chan State) {
sc.unregister <- c
}
func (sc *stateChanged) run() {
for {
select {
case o := <-sc.register:
sc.observers[o] = true
case o := <-sc.unregister:
delete(sc.observers, o)
close(*o)
case s := <-sc.states:
for o := range sc.observers {
select {
case *o <- s:
default:
delete(sc.observers, o)
close(*o)
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment