Documentation
¶
Overview ¶
Package eventbus provides a straightforward concurrent EventBus for Go 1.18+, supporting fanout, and in order, only once delivery.
Index ¶
Constants ¶
const DefaultBufferSize = 10
DefaultBufferSize for the subscription channels. Used when the BufferSize is not configured.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct {
// BufferSize for the subscription channels.
// If not set or zero, it defaults to DefaultBufferSize.
// If negative, it creates the channels with no buffer.
BufferSize int
}
Config can be passed to NewWithConfig to customize the EventBus.
type EventBus ¶
type EventBus[Event any] struct { // contains filtered or unexported fields }
EventBus is a straightforward concurrent EventBus for Go 1.18+, supporting fanout, and in order, only once delivery.
It can be used by initializing a copy of the struct, or by calling the New or NewWithConfig functions.
func NewWithConfig ¶
NewWithConfig creates a new customized EventBus.
func (*EventBus[Event]) Publish ¶
Publish sends the provided event to all of the listed topics. All subscriptions to those topics will be notified of the event.
func (*EventBus[Event]) Subscribe ¶
func (b *EventBus[Event]) Subscribe(topicKeys ...string) *Subscription[Event]
Subscribe creates a new subscription to the listed topics. All events published to any of those topics will be sent to the subscription's channel.
If the same event is sent to multiple of the listed topics, the event will only be delivered once.
type Subscription ¶
type Subscription[Event any] struct { // contains filtered or unexported fields }
Subscription maintains subscriptions to multiple topics. Events are sent to the Channel().
func (*Subscription[Event]) Channel ¶
func (sub *Subscription[Event]) Channel() <-chan Event
Channel exposes a read only view of the subscription's channel. All events published to the subscribed topics will be published to this channel.
If the same event is sent to multiple of the listed topics, the event will only be delivered once.
func (*Subscription[Event]) Unsubscribe ¶
func (s *Subscription[Event]) Unsubscribe()
Unsubscribe closes the subscription to the topics. It also closes the subscription's channel.