Help us shape the future of Go! Take the 2026 Go Developer Survey

events

package
v0.13.0 Latest Latest
Warning

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

Go to latest
Published: Oct 16, 2026 License: MIT Imports: 7 Imported by: 1

Documentation

Overview

Package events dispatches typed events to listeners registered by type. Listeners are plain functions; registration is explicit code (no discovery):

type UserRegistered struct{ User models.User }

bus.Register(events.On(listeners.SendWelcome)) // func(ctx, UserRegistered) error
err := bus.Dispatch(ctx, UserRegistered{User: u})

Async listeners run on a bounded goroutine pool that Shutdown drains; a queue-backed mode arrives with the queue (BETA).

Index

Constants

This section is empty.

Variables

View Source
var ErrClosed = errors.New("events: bus is shut down")

ErrClosed is returned by Dispatch after Shutdown.

Functions

func Dispatched

func Dispatched[E any](f *Fake) []E

Dispatched returns the recorded events of type E.

func Listen

func Listen[E any](b *Bus, fn func(ctx context.Context, e E) error)

Listen registers a synchronous listener for E.

func ListenAsync

func ListenAsync[E any](b *Bus, fn func(ctx context.Context, e E) error)

ListenAsync registers an async listener for E.

Types

type Bus

type Bus struct {
	// contains filtered or unexported fields
}

Bus holds listeners and runs them.

func New

func New(log *slog.Logger, workers int) *Bus

New returns a bus running at most workers async listeners at once (0 = 16).

func (*Bus) Dispatch

func (b *Bus) Dispatch(ctx context.Context, event any) error

Dispatch runs the synchronous listeners of event's type in registration order (stopping at the first error) and schedules the async ones. Events without listeners are fine.

func (*Bus) Register

func (b *Bus) Register(regs ...Registration)

Register adds listeners in order.

func (*Bus) Shutdown

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

Shutdown stops accepting events and waits for async listeners until ctx is done.

type Dispatcher

type Dispatcher interface {
	Dispatch(ctx context.Context, event any) error
}

Dispatcher dispatches events (Bus and Fake implement it).

type Fake

type Fake struct {
	// contains filtered or unexported fields
}

Fake records dispatched events instead of running listeners (tests).

func NewFake

func NewFake() *Fake

NewFake returns an empty Fake.

func (*Fake) Dispatch

func (f *Fake) Dispatch(_ context.Context, event any) error

Dispatch implements Dispatcher.

type Registration

type Registration struct {
	// contains filtered or unexported fields
}

Registration binds a listener to its event type; create it with On or OnAsync.

func On

func On[E any](fn func(ctx context.Context, e E) error) Registration

On registers fn for events of type E, run synchronously by Dispatch (its error is returned to the caller).

func OnAsync

func OnAsync[E any](fn func(ctx context.Context, e E) error) Registration

OnAsync registers fn for events of type E, run in the background; its error is logged.

Jump to

Keyboard shortcuts

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