package bus import ( "sync" ) type ( Event struct { EvType string Data interface{} } listeners []chan<- Event Bus struct { sync.RWMutex subs map[string]listeners } ) func New() *Bus { bus := &Bus{ subs: make(map[string]listeners), } return bus } func (b *Bus) Sub(topic string, ch chan<- Event) { b.Lock() defer b.Unlock() if prev, ok := b.subs[topic]; ok { b.subs[topic] = append(prev, ch) } else { b.subs[topic] = append(listeners{}, ch) } } func (b *Bus) Unsub(topic string, ch chan<- Event) { b.Lock() defer b.Unlock() for i, v := range b.subs[topic] { if v == ch { // we don't care about order, replace and reslice b.subs[topic][i] = b.subs[topic][len(b.subs[topic])-1] b.subs[topic] = b.subs[topic][:len(b.subs[topic])-1] } } } func (b *Bus) Pub(topic string, data interface{}) { b.RLock() defer b.RUnlock() tc, ok := b.subs[topic] if !ok { return } for _, ch := range tc { ch <- Event{EvType: topic, Data: data} } } func (b *Bus) Shutdown() { for _, v := range b.subs { for _, c := range v { close(c) } } }