-
Notifications
You must be signed in to change notification settings - Fork 0
/
dispatcher.go
56 lines (42 loc) · 1.16 KB
/
dispatcher.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
package event
import (
"context"
"golang.org/x/sync/errgroup"
)
type Dispatcher struct {
listeners map[Type][]Listener
}
func NewDispatcher() *Dispatcher {
return &Dispatcher{
listeners: make(map[Type][]Listener),
}
}
func (d *Dispatcher) HandleEvent(ctx context.Context, e Event) error {
listeners := d.listeners[e.Type()]
g, ctx := errgroup.WithContext(ctx)
for _, listener := range listeners {
g.Go(func() error {
return listener.HandleEvent(ctx, e)
})
}
return g.Wait()
}
func (d *Dispatcher) Listen(_type Type, listener Listener) {
d.listeners[_type] = append(d.listeners[_type], listener)
}
func (d *Dispatcher) ListenFunc(_type Type, listener func(context.Context, Event) error) {
d.Listen(_type, ListenerFunc(listener))
}
func (d *Dispatcher) HasListener(_type Type) bool {
_, ok := d.listeners[_type]
return ok
}
func (d *Dispatcher) Subscribe(subscriber Subscriber) {
events := subscriber.SubscribedEvents().(map[Type][]Listener)
for _type, listeners := range events {
d.listeners[_type] = append(d.listeners[_type], listeners...)
}
}
func (d *Dispatcher) SubscribeFunc(subscriber func() any) {
d.Subscribe(SubscriberFunc(subscriber))
}