10type Broker[T any] struct {
11 subs map[chan Event[T]]struct{}
18func NewBroker[T any]() *Broker[T] {
19 return NewBrokerWithOptions[T](bufferSize, 1000)
22func NewBrokerWithOptions[T any](channelBufferSize, maxEvents int) *Broker[T] {
24 subs: make(map[chan Event[T]]struct{}),
25 done: make(chan struct{}),
32func (b *Broker[T]) Shutdown() {
34 case <-b.done: // Already closed
43 for ch := range b.subs {
51func (b *Broker[T]) Subscribe(ctx context.Context) <-chan Event[T] {
57 ch := make(chan Event[T])
63 sub := make(chan Event[T], bufferSize)
64 b.subs[sub] = struct{}{}
87func (b *Broker[T]) GetSubscriberCount() int {
93func (b *Broker[T]) Publish(t EventType, payload T) {
102 subscribers := make([]chan Event[T], 0, len(b.subs))
103 for sub := range b.subs {
104 subscribers = append(subscribers, sub)
108 event := Event[T]{Type: t, Payload: payload}
110 for _, sub := range subscribers {
114 // Channel is full, subscriber is slow - skip this event
115 // This prevents blocking the publisher
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list