bus

package
v0.22.0 Latest Latest
Warning

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

Go to latest
Published: Oct 20, 2026 License: MIT Imports: 7 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package bus provides a command bus, through which all of orca's commands and queries are dispatched. It's the single place for behaviour that applies to every command, such as logging (middleware) and lifecycle hooks.

Handlers, middleware and hooks are expected to be registered while wiring up the application, before anything is dispatched, so the bus isn't safe for concurrent registration.

Index

Constants

This section is empty.

Variables

View Source
var ErrNoHandler = errors.New("no handler registered for message")

Functions

func LoggingMiddleware

func LoggingMiddleware(ctx context.Context, msg Named, next Next) (any, error)

LoggingMiddleware logs each dispatch at debug level.

Types

type Bus

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

func New

func New() *Bus

func (*Bus) AddHook

func (b *Bus) AddHook(hooks ...Hook)

AddHook adds hooks to the bus, which run in the order they're added.

func (*Bus) After

func (b *Bus) After[M Message[R], R any](fn func(ctx context.Context, msg M, res R, err error) error)

After adds a hook that runs after messages of type M are handled, with the handler's result and error.

func (*Bus) Before

func (b *Bus) Before[M Message[R], R any](fn func(ctx context.Context, msg M) error)

Before adds a hook that runs before messages of type M are handled.

func (*Bus) Dispatch

func (b *Bus) Dispatch[R any](ctx context.Context, msg Message[R]) (R, error)

Dispatch sends the message to its handler, through any middleware and hooks.

func (*Bus) Handle

func (b *Bus) Handle[M Message[R], R any](h HandlerFunc[M, R])

Handle registers the handler for messages of type M. Registering a second handler for the same type is a programming error, so panics.

func (*Bus) HandleVoid

func (b *Bus) HandleVoid[M Message[Void]](h func(ctx context.Context, msg M) error)

HandleVoid registers the handler for messages of type M, which don't return anything, so the handler only needs to return an error.

func (*Bus) HandledTypes

func (b *Bus) HandledTypes() []string

HandledTypes returns the type of every message that has a handler, e.g. "controller.UpCommand".

func (*Bus) Use

func (b *Bus) Use(mw ...Middleware)

Use adds middleware to the bus. Middleware runs in the order it's added, and wraps the hooks as well as the handler.

type Event

type Event struct {
	Phase   Phase
	Name    string
	Message any
	Result  any
	Err     error
}

Event describes a point in the lifecycle of a dispatch. Result and Err are only set for PhaseAfter.

type HandlerFunc

type HandlerFunc[M Message[R], R any] func(ctx context.Context, msg M) (R, error)

HandlerFunc handles a single type of message.

type Hook

type Hook func(ctx context.Context, ev Event) error

Hook is called for every message at each phase of its dispatch.

An error from a PhaseBefore hook stops the dispatch, so the handler and any later hooks don't run. PhaseAfter hooks always run, whether or not the handler failed, and their errors are joined with the handler's.

type HookFailedError

type HookFailedError struct {
	Phase Phase
	Name  string
	Err   error
}

func (HookFailedError) Error

func (e HookFailedError) Error() string

func (HookFailedError) Unwrap

func (e HookFailedError) Unwrap() error

type Message

type Message[R any] interface {
	Named
	// contains filtered or unexported methods
}

Message is a command or query that returns R when dispatched.

type Middleware

type Middleware func(ctx context.Context, msg Named, next Next) (any, error)

Middleware wraps the dispatch of every message. The message and result are untyped, as interfaces can't declare generic methods.

type Named

type Named interface {
	MessageName() string
}

Named is implemented by every message. The name is a stable identifier for the message (e.g. "up", "ws.init"), used for logging and matching hooks.

type Next

type Next func(ctx context.Context) (any, error)

Next calls the rest of the middleware chain, and eventually the handler.

type Phase

type Phase string
const (
	PhaseBefore Phase = "before"
	PhaseAfter  Phase = "after"
)

type Returns

type Returns[R any] struct{}

Returns is embedded in a message to declare the type it returns, e.g.

type UpCommand struct {
	bus.Returns[bus.Void]
	...
}

It lets the compiler check that the handler for a message and the callers dispatching it agree on the result type.

type Void

type Void = struct{}

Void is the result type for messages that don't return anything.

Jump to

Keyboard shortcuts

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