-
Notifications
You must be signed in to change notification settings - Fork 0
/
bus.go
64 lines (53 loc) · 1.58 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
package protobus
import (
"context"
"errors"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/reflect/protoreflect"
)
// HandlerFunc defines a handler function interface.
type HandlerFunc func(ctx context.Context, msg Msg) error
type HandlerFuncT[T Msg] func(ctx context.Context, msg T) error
// Msg defines a message interface.
type Msg = proto.Message
type eventKey = protoreflect.FullName
// Bus type defines the bus interface structure.
type Bus interface {
Publish(ctx context.Context, msg Msg) error
AddEventListener(handler HandlerFunc)
}
// InProcBus defines the bus structure.
type InProcBus struct {
listeners map[eventKey][]HandlerFunc
}
func ProvideBus() *InProcBus {
return &InProcBus{
listeners: make(map[eventKey][]HandlerFunc),
}
}
// Publish function publish a message to the bus listener.
func (b *InProcBus) Publish(ctx context.Context, msg Msg) error {
var msgName = msg.ProtoReflect().Descriptor().FullName()
var errs []error
if listeners, exists := b.listeners[msgName]; exists {
for _, listenerHandler := range listeners {
if err := listenerHandler(ctx, msg); err != nil {
errs = append(errs, err)
}
}
}
if len(errs) == 0 {
return nil
}
return errors.Join(errs...)
}
func AddEventListener[T Msg](b *InProcBus, handler HandlerFuncT[T]) {
eventName := (*new(T)).ProtoReflect().Descriptor().FullName()
_, exists := b.listeners[eventName]
if !exists {
b.listeners[eventName] = make([]HandlerFunc, 0)
}
b.listeners[eventName] = append(b.listeners[eventName], func(ctx context.Context, msg Msg) error {
return handler(ctx, msg.(T))
})
}