| 1 |
package partybus |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"sync" |
| 6 |
) |
| 7 |
|
| 8 |
var ErrUnsubscribe = fmt.Errorf("unable to find subscription to unsubscribe") |
| 9 |
|
| 10 |
type Responder interface { |
| 11 |
RespondsTo() []EventType |
| 12 |
} |
| 13 |
|
| 14 |
type Handler interface { |
| 15 |
Handle(Event) error |
| 16 |
} |
| 17 |
|
| 18 |
type Publisher interface { |
| 19 |
Publish(Event) |
| 20 |
} |
| 21 |
|
| 22 |
type Subscriber interface { |
| 23 |
Subscribe(...EventType) *Subscription |
| 24 |
} |
| 25 |
|
| 26 |
type Unsubscribable interface { |
| 27 |
Unsubscribe() error |
| 28 |
} |
| 29 |
|
| 30 |
type Bus struct { |
| 31 |
selectSubs map[EventType][]*Subscription |
| 32 |
fullSubs []*Subscription |
| 33 |
allSubs []*Subscription |
| 34 |
lock sync.RWMutex |
| 35 |
} |
| 36 |
|
| 37 |
func NewBus() *Bus { |
| 38 |
return &Bus{ |
| 39 |
selectSubs: make(map[EventType][]*Subscription), |
| 40 |
fullSubs: make([]*Subscription, 0), |
| 41 |
allSubs: make([]*Subscription, 0), |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
func (bus *Bus) Publish(event Event) { |
| 46 |
bus.lock.RLock() |
| 47 |
defer bus.lock.RUnlock() |
| 48 |
|
| 49 |
|
| 50 |
for _, sub := range bus.fullSubs { |
| 51 |
sub.sender <- event |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
if subs, ok := bus.selectSubs[event.Type]; ok { |
| 56 |
for _, sub := range subs { |
| 57 |
sub.sender <- event |
| 58 |
} |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
func (bus *Bus) Subscribe(eTypes ...EventType) *Subscription { |
| 63 |
bus.lock.Lock() |
| 64 |
defer bus.lock.Unlock() |
| 65 |
|
| 66 |
sub := newSubscription(bus, eTypes) |
| 67 |
|
| 68 |
if len(eTypes) == 0 { |
| 69 |
|
| 70 |
bus.fullSubs = append(bus.fullSubs, sub) |
| 71 |
} else { |
| 72 |
|
| 73 |
for _, eType := range eTypes { |
| 74 |
bus.selectSubs[eType] = append(bus.selectSubs[eType], sub) |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
bus.allSubs = append(bus.allSubs, sub) |
| 79 |
|
| 80 |
return sub |
| 81 |
} |
| 82 |
|
| 83 |
func (bus *Bus) Unsubscribe(sub *Subscription) error { |
| 84 |
bus.lock.Lock() |
| 85 |
defer bus.lock.Unlock() |
| 86 |
|
| 87 |
var found bool |
| 88 |
|
| 89 |
for _, eType := range sub.eventTypes { |
| 90 |
if _, ok := bus.selectSubs[eType]; ok { |
| 91 |
|
| 92 |
for idx, eSub := range bus.selectSubs[eType] { |
| 93 |
if eSub.id == sub.id { |
| 94 |
found = true |
| 95 |
bus.selectSubs[eType] = append(bus.selectSubs[eType][:idx], bus.selectSubs[eType][idx+1:]...) |
| 96 |
} |
| 97 |
} |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
for idx, eSub := range bus.fullSubs { |
| 103 |
if eSub.id == sub.id { |
| 104 |
found = true |
| 105 |
bus.fullSubs = append(bus.fullSubs[:idx], bus.fullSubs[idx+1:]...) |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
for idx, eSub := range bus.allSubs { |
| 111 |
if eSub.id == sub.id { |
| 112 |
found = true |
| 113 |
close(eSub.sender) |
| 114 |
bus.allSubs = append(bus.fullSubs[:idx], bus.allSubs[idx+1:]...) |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
if !found { |
| 119 |
return ErrUnsubscribe |
| 120 |
} |
| 121 |
return nil |
| 122 |
} |
| 123 |
|
| 124 |
func (bus *Bus) Close() { |
| 125 |
bus.lock.Lock() |
| 126 |
defer bus.lock.Unlock() |
| 127 |
|
| 128 |
for _, sub := range bus.allSubs { |
| 129 |
close(sub.sender) |
| 130 |
} |
| 131 |
|
| 132 |
bus.selectSubs = make(map[EventType][]*Subscription) |
| 133 |
bus.fullSubs = make([]*Subscription, 0) |
| 134 |
bus.allSubs = make([]*Subscription, 0) |
| 135 |
} |