Documentation
¶
Overview ¶
Package eventbus provides a simple publish/subscribe event bus. Plugins and components can optionally use this to communicate with each other.
Index ¶
Constants ¶
const (
// Constant name for identifying the eventbus plugin.
PluginName = "eventbus"
)
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Bus ¶
type Bus struct {
// contains filtered or unexported fields
}
Implementation of EventBus which uses a simple map to store subscribers.
func (*Bus) Shutdown ¶ added in v0.3.0
Shutdown closes the job channel and waits for all workers to finish. This should be called before Wait() to ensure graceful shutdown.
func (*Bus) Subscribe ¶
func (b *Bus) Subscribe(event string, handler Subscriber)
Subscribe to an event.
type BusOption ¶ added in v0.3.0
type BusOption func(*Bus)
BusOption configures the event bus.
func WithWorkerPool ¶ added in v0.3.0
WithWorkerPool sets the number of worker goroutines for processing events. Default is 100 workers. Set to 0 to use unbounded goroutines (legacy behavior).
type EventBus ¶
type EventBus interface {
// Subscribe to an event. The handler will be called when the event is
// published. Depending on the implementation errors may be logged or retried.
// Subscribers should assume that they may be called multiple times
// concurrently.
Subscribe(event string, subscriber Subscriber)
// Publish an event. The event will be sent to all subscribers.
Publish(event string, data any)
// Wait for the event bus to finish processing all events. You should ensure
// that publishers are also stopped as the event bus won't reject new events.
Wait(ctx context.Context) error
}
EventBus provides a simple publish/subscribe interface for publishing and subscribing to events.
func FromContext ¶
FromContext retrieves the event bus from a context.
type EventBusPlugin ¶
type EventBusPlugin struct {
EventBus
}
EventBusPlugin provides access to an event bus for plugins and components to communicate with each other.
func Plugin ¶
func Plugin(eb EventBus) *EventBusPlugin
Plugin registers an eventbus with a Prefab server for use by other plugins to use. The bus can be retrieved from the request context using the FromContext function.
func (*EventBusPlugin) ServerOptions ¶
func (p *EventBusPlugin) ServerOptions() []prefab.ServerOption
From prefab.OptionProvider.