eventbus

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Mar 28, 2025 License: MIT Imports: 6 Imported by: 0

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

View Source
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) Publish

func (b *Bus) Publish(event string, data any)

Publish an event.

func (*Bus) Subscribe

func (b *Bus) Subscribe(event string, handler Subscriber)

Subscribe to an event.

func (*Bus) Wait

func (b *Bus) Wait(ctx context.Context) error

Wait for the event bus to finish processing all events.

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

func FromContext(ctx context.Context) EventBus

FromContext retrieves the event bus from a context.

func NewBus

func NewBus(ctx context.Context) EventBus

NewBus returns a new EventBus. ctx is passed to subscribers when they are executed.

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) Name

func (p *EventBusPlugin) Name() string

From prefab.Plugin.

func (*EventBusPlugin) ServerOptions

func (p *EventBusPlugin) ServerOptions() []prefab.ServerOption

From prefab.OptionProvider.

func (*EventBusPlugin) Shutdown

func (p *EventBusPlugin) Shutdown(ctx context.Context) error

From prefab.ShutdownPlugin.

type Subscriber

type Subscriber func(context.Context, any) error

Function type for event subscribers.

Jump to

Keyboard shortcuts

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