-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbus.go
109 lines (88 loc) · 2.14 KB
/
bus.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
package eventbus
import (
"fmt"
"sync"
)
var (
ErrInvalidChannelType = fmt.Errorf("invalid channel type")
ErrChannelAlreadyExists = fmt.Errorf("channel already exists")
)
type Bus interface {
Get(string) (any, bool)
Set(string, any) error
}
type simpleBus struct {
mu sync.RWMutex
channels map[string]any // untyped map for typed Channels
}
// NewBus returns a new event bus
func NewBus() Bus {
return &simpleBus{
channels: make(map[string]any),
}
}
// Get a channel of the event bus
// returns true if found, false otherwise
func (b *simpleBus) Get(evt string) (any, bool) {
b.mu.RLock() // read lock
defer b.mu.RUnlock()
ch, exists := b.channels[evt]
return ch, exists
}
// Set stores a channel in the event bus
// errors on duplicate channel names
func (b *simpleBus) Set(evt string, ch any) error {
b.mu.Lock()
defer b.mu.Unlock()
// we dont allow overrites
_, exists := b.channels[evt]
if exists {
return ErrChannelAlreadyExists
}
b.channels[evt] = ch
return nil
}
// Subscribe to the channel in the bus that matches the type parameter
func Subscribe[T any](bus Bus) (Subscription[T], error) {
typedCh, err := getOrCreateTypedChannel[T](bus)
if err != nil {
return nil, err
}
return typedCh.Subscribe()
}
// Publish to the channel in the bus that matches the type parameter
func Publish[T any](bus Bus, evt T) error {
typedCh, err := getOrCreateTypedChannel[T](bus)
if err != nil {
return err
}
typedCh.Publish(evt)
return nil
}
// Get the name of a event from the type parameter
func generateEventName[T any]() string {
var t T
// struct
name := fmt.Sprintf("%T", t)
if name != "<nil>" {
return name
}
// interface
return fmt.Sprintf("%T", new(T))
}
// Get the typed channel that matches the given type parameter
func getOrCreateTypedChannel[T any](bus Bus) (Channel[T], error) {
evtName := generateEventName[T]()
ch, exists := bus.Get(evtName)
var typedCh Channel[T]
var ok bool
if !exists {
typedCh = New[T](0, 100)
if err := bus.Set(evtName, typedCh); err != nil {
return nil, err
}
} else if typedCh, ok = ch.(Channel[T]); !ok {
return nil, ErrInvalidChannelType
}
return typedCh, nil
}