eventbus

package
v2.3.8 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 1, 2025 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package eventbus implements a simple event bus.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Event

type Event[T any] struct {
	Topic   string
	Payload T
}

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

func NewEventBus[T any]() *EventBus[T]

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

func (eb *EventBus[T]) ClearListenersByTopic(topic string)

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

func (eb *EventBus[T]) GetAllListenersCount() int

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

func (eb *EventBus[T]) GetEvents() []string

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

func (eb *EventBus[T]) GetListenersCount(topic string) int

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

func (eb *EventBus[T]) Publish(event Event[T])

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

func (eb *EventBus[T]) SetErrorHandler(handler func(topic string, err error))

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

func (eb *EventBus[T]) Unsubscribe(topic string, listener func(eventData T))

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL