-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.go
247 lines (199 loc) · 5.22 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
package bus
import (
"fmt"
"sync"
"sync/atomic"
)
var mapper sync.Map // holds key (event name - string) versus topic values
// we allow developers to override event names. They should be careful about name collisions
type iEventName interface {
EventID() string //
}
// if developers implement this interface, we're spinning a goroutine if the event says it is async
type iAsync interface {
Async() bool
}
// Listener is being returned when you subscribe to a topic, so you can unsubscribe or access the parent topic
type Listener[T any] struct {
parent *Topic[T] // so we can call unsubscribe from parent
callback func(event T) // the function that we're going to call
}
// Topic keeps the subscribers of one topic
type Topic[T any] struct {
subs []*Listener[T] // list of listeners
rwMu sync.RWMutex // guards subs
lisnsPool sync.Pool // a pool of listeners
}
// NewTopic creates a new topic for a specie of events
func NewTopic[T any]() *Topic[T] {
result := &Topic[T]{}
result.lisnsPool.New = func() any {
return &Listener[T]{
parent: result,
}
}
return result
}
// Sub adds a callback to be called when an event of that type is being published
func (b *Topic[T]) Sub(callback func(v T)) *Listener[T] {
result := b.lisnsPool.Get().(*Listener[T])
result.callback = callback
result.parent = b
b.rwMu.Lock()
b.subs = append(b.subs, result)
b.rwMu.Unlock()
return result
}
// cancel is private to the topic, but can be accessed via Listener
func (b *Topic[T]) cancel(who *Listener[T]) {
b.rwMu.Lock()
for i := range b.subs {
if b.subs[i] != who {
continue
}
b.subs[i] = b.subs[len(b.subs)-1]
b.subs[len(b.subs)-1] = nil
b.subs = b.subs[:len(b.subs)-1]
break
}
b.rwMu.Unlock()
who.callback = nil
b.lisnsPool.Put(who)
}
// NumSubs in case you need to perform tests and check the number of subscribers of this particular topic
func (b *Topic[T]) NumSubs() int {
b.rwMu.RLock()
result := len(b.subs)
b.rwMu.RUnlock()
return result
}
// Cancel forgets the indicated callback
func (s *Listener[T]) Cancel() {
s.parent.cancel(s)
}
// Topic gives access to the underlying topic
func (s *Listener[T]) Topic() *Topic[T] {
return s.parent
}
// Pub allows you to publish an event in that topic
func (b *Topic[T]) Pub(event T) {
b.rwMu.RLock()
isAsync := false
switch m := any(event).(type) {
case iAsync:
isAsync = m.Async()
}
for sub := range b.subs {
if isAsync {
go b.subs[sub].callback(event)
continue
}
b.subs[sub].callback(event)
}
b.rwMu.RUnlock()
}
func (b *Topic[T]) PubAsync(event T) {
b.rwMu.RLock()
for sub := range b.subs {
go b.subs[sub].callback(event)
}
b.rwMu.RUnlock()
}
// Bus is being returned when you subscribe, so you can manually Cancel
type Bus[T any] struct {
listener *Listener[T]
stop atomic.Uint32 // flag for unsubscribing after receiving one event
}
// Cancel allows callers to manually unsubscribe, in case they don't want to use SubCancel
func (o *Bus[T]) Cancel() {
if o.stop.CompareAndSwap(0, 1) {
go o.listener.Cancel()
}
}
// SubCancel can be used if you need to unsubscribe immediately after receiving an event, by making your function return true
func SubCancel[T any](callback func(event T) bool) *Bus[T] {
var (
event T
key string
)
switch m := any(event).(type) {
case iEventName:
key = m.EventID()
default:
key = fmt.Sprintf("%T", event)
}
topic, ok := mapper.Load(key)
if !ok || topic == nil {
topic, _ = mapper.LoadOrStore(key, NewTopic[T]())
}
var result Bus[T]
result.listener = topic.(*Topic[T]).Sub(func(v T) {
if result.stop.Load() == 1 {
return
}
shouldCancel := callback(v)
if shouldCancel {
result.Cancel()
}
})
return &result
}
// Sub subscribes a callback function to listen for a specie of events
func Sub[T any](callback func(event T)) *Bus[T] {
var (
event T
key string
)
switch m := any(event).(type) {
case iEventName:
key = m.EventID()
default:
key = fmt.Sprintf("%T", event)
}
topic, ok := mapper.Load(key)
if !ok || topic == nil {
topic, _ = mapper.LoadOrStore(key, NewTopic[T]())
}
var result Bus[T]
result.listener = topic.(*Topic[T]).Sub(func(v T) {
if result.stop.Load() == 1 {
return
}
callback(v)
})
return &result
}
// Pub publishes an event which will be dispatched to all listeners
func Pub[T any](event T) {
var key string
switch m := any(event).(type) {
case iEventName:
key = m.EventID()
default:
key = fmt.Sprintf("%T", event)
}
topic, ok := mapper.Load(key)
if !ok || topic == nil { // create a new topic, even if there are no listeners (otherwise we will have to panic)
topic, _ = mapper.LoadOrStore(key, NewTopic[T]())
}
topic.(*Topic[T]).Pub(event)
}
// PubAsync publishes an event which will be dispatched to all listeners
func PubAsync[T any](event T) {
var key string
switch m := any(event).(type) {
case iEventName:
key = m.EventID()
default:
key = fmt.Sprintf("%T", event)
}
topic, ok := mapper.Load(key)
if !ok || topic == nil { // create a new topic, even if there are no listeners (otherwise we will have to panic)
topic, _ = mapper.LoadOrStore(key, NewTopic[T]())
}
topic.(*Topic[T]).PubAsync(event)
}
// Range gives access to mapper Range
func Range(f func(k, v any) bool) {
mapper.Range(f)
}