Documentation
¶
Overview ¶
Package eventbus implements a simple event bus.
Index ¶
- type Event
- type EventBus
- func (eb *EventBus[T]) ClearListeners()
- func (eb *EventBus[T]) ClearListenersByTopic(topic string)
- func (eb *EventBus[T]) GetAllListenersCount() int
- func (eb *EventBus[T]) GetEvents() []string
- func (eb *EventBus[T]) GetListenersCount(topic string) int
- func (eb *EventBus[T]) Publish(event Event[T])
- func (eb *EventBus[T]) SetErrorHandler(handler func(topic string, err error))
- func (eb *EventBus[T]) Subscribe(topic string, listener func(eventData T), async bool, priority int, ...)
- func (eb *EventBus[T]) Unsubscribe(topic string, listener func(eventData T))
- type EventListener
Examples ¶
- EventBus
- EventBus.ClearListeners
- EventBus.ClearListenersByTopic
- EventBus.GetAllListenersCount
- EventBus.GetEvents
- EventBus.GetListenersCount
- EventBus.Publish
- EventBus.SetErrorHandler
- EventBus.Subscribe
- EventBus.Subscribe (Async)
- EventBus.Subscribe (WithFilter)
- EventBus.Subscribe (WithPriority)
- EventBus.Unsubscribe
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Event ¶
Event is the struct that is passed to the event listener, now it directly uses the generic Payload type.
type EventBus ¶
type EventBus[T any] struct { // contains filtered or unexported fields }
EventBus is the struct that holds the listeners and the error handler.
Example ¶
eb := NewEventBus[int]()
receivedData := 0
listener := func(eventData int) {
receivedData = eventData
}
eb.Subscribe("event1", listener, false, 0, nil)
eb.Publish(Event[int]{Topic: "event1", Payload: 1})
fmt.Println(receivedData)
Output: 1
func NewEventBus ¶
NewEventBus creates a new EventBus. Play: https://go.dev/play/p/gHbOPV_NUOJ
func (*EventBus[T]) ClearListeners ¶
func (eb *EventBus[T]) ClearListeners()
ClearListeners clears all the listeners. Play: https://go.dev/play/p/KBfBYlKPgqD
Example ¶
eb := NewEventBus[int]()
receivedData := 0
listener := func(eventData int) {
receivedData = eventData
}
eb.Subscribe("event1", listener, false, 0, nil)
eb.Subscribe("event2", listener, false, 0, nil)
eb.ClearListeners()
eb.Publish(Event[int]{Topic: "event1", Payload: 1})
eb.Publish(Event[int]{Topic: "event2", Payload: 2})
fmt.Println(receivedData)
Output: 0
func (*EventBus[T]) ClearListenersByTopic ¶
ClearListenersByTopic clears all the listeners by topic. Play: https://go.dev/play/p/gvMljmJOZmU
Example ¶
eb := NewEventBus[int]()
receivedData := 0
listener := func(eventData int) {
receivedData = eventData
}
eb.Subscribe("event1", listener, false, 0, nil)
eb.Subscribe("event2", listener, false, 0, nil)
eb.ClearListenersByTopic("event1")
eb.Publish(Event[int]{Topic: "event1", Payload: 1})
eb.Publish(Event[int]{Topic: "event2", Payload: 2})
fmt.Println(receivedData)
Output: 2
func (*EventBus[T]) GetAllListenersCount ¶
GetAllListenersCount returns the total number of listeners. Play: https://go.dev/play/p/PUlr0xcpEOz
Example ¶
eb := NewEventBus[int]()
eb.Subscribe("event1", func(eventData int) {}, false, 0, nil)
eb.Subscribe("event2", func(eventData int) {}, false, 0, nil)
count := eb.GetAllListenersCount()
fmt.Println(count)
Output: 2
func (*EventBus[T]) GetEvents ¶
GetEvents returns all the events topics. Play: https://go.dev/play/p/etgjjcOtAjX
Example ¶
eb := NewEventBus[int]()
eb.Subscribe("event1", func(eventData int) {}, false, 0, nil)
eb.Subscribe("event2", func(eventData int) {}, false, 0, nil)
events := eb.GetEvents()
sort.Strings(events)
for _, event := range events {
fmt.Println(event)
}
Output: event1 event2
func (*EventBus[T]) GetListenersCount ¶
GetListenersCount returns the number of listeners for a specific event topic. Play: https://go.dev/play/p/8VPJsMQgStM
Example ¶
eb := NewEventBus[int]()
eb.Subscribe("event1", func(eventData int) {}, false, 0, nil)
eb.Subscribe("event2", func(eventData int) {}, false, 0, nil)
count := eb.GetListenersCount("event1")
fmt.Println(count)
Output: 1
func (*EventBus[T]) Publish ¶
Publish publishes an event with a specific event topic and data payload. Play: https://go.dev/play/p/gHTtVexFSH9
Example ¶
eb := NewEventBus[int]()
eb.Subscribe("event1", func(eventData int) {
fmt.Println(eventData)
}, false, 0, nil)
eb.Publish(Event[int]{Topic: "event1", Payload: 1})
Output: 1
func (*EventBus[T]) SetErrorHandler ¶
SetErrorHandler sets the error handler function. Play: https://go.dev/play/p/gmB0gnFe5mc
Example ¶
eb := NewEventBus[int]()
eb.SetErrorHandler(func(topic string, err error) {
fmt.Println(topic, err)
})
eb.Subscribe("event1", func(eventData int) {
panic("error")
}, false, 0, nil)
eb.Publish(Event[int]{Topic: "event1", Payload: 1})
Output: event1 error
func (*EventBus[T]) Subscribe ¶
func (eb *EventBus[T]) Subscribe(topic string, listener func(eventData T), async bool, priority int, filter func(eventData T) bool)
Subscribe subscribes to an event with a specific event topic and listener function. Play: https://go.dev/play/p/EYGf_8cHei-
Example ¶
eb := NewEventBus[string]()
eb.Subscribe("event1", func(eventData string) {
fmt.Println(eventData)
}, false, 0, nil)
eb.Publish(Event[string]{Topic: "event1", Payload: "hello"})
Output: hello
Example (Async) ¶
eb := NewEventBus[int]()
var wg sync.WaitGroup
wg.Add(1)
eb.Subscribe("event1", func(eventData int) {
time.Sleep(100 * time.Millisecond)
fmt.Println(eventData)
wg.Done()
}, true, 1, nil)
eb.Publish(Event[int]{Topic: "event1", Payload: 1})
wg.Wait()
Output: 1
Example (WithFilter) ¶
eb := NewEventBus[int]()
receivedData := 0
listener := func(eventData int) {
receivedData = eventData
}
filter := func(eventData int) bool {
return eventData == 1
}
eb.Subscribe("event1", listener, false, 0, filter)
eb.Publish(Event[int]{Topic: "event1", Payload: 1})
eb.Publish(Event[int]{Topic: "event1", Payload: 2})
fmt.Println(receivedData)
Output: 1
Example (WithPriority) ¶
eb := NewEventBus[int]()
eb.Subscribe("event1", func(eventData int) {
fmt.Println(eventData)
}, false, 0, nil)
eb.Subscribe("event1", func(eventData int) {
fmt.Println(eventData)
}, false, 1, nil)
eb.Publish(Event[int]{Topic: "event1", Payload: 1})
Output: 1 1
func (*EventBus[T]) Unsubscribe ¶
Unsubscribe unsubscribes from an event with a specific event topic and listener function. Play: https://go.dev/play/p/Tmh7Ttfvprf
Example ¶
eb := NewEventBus[int]()
receivedData := 0
listener := func(eventData int) {
receivedData = eventData
}
eb.Subscribe("event1", listener, false, 0, nil)
eb.Unsubscribe("event1", listener)
eb.Publish(Event[int]{Topic: "event1", Payload: 1})
fmt.Println(receivedData)
Output: 0
type EventListener ¶
type EventListener[T any] struct { // contains filtered or unexported fields }
EventListener is the struct that holds the listener function and its priority.