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 ¶
- Variables
- func LoggingMiddleware(ctx context.Context, msg Named, next Next) (any, error)
- type Bus
- func (b *Bus) AddHook(hooks ...Hook)
- func (b *Bus) After[M Message[R], R any](fn func(ctx context.Context, msg M, res R, err error) error)
- func (b *Bus) Before[M Message[R], R any](fn func(ctx context.Context, msg M) error)
- func (b *Bus) Dispatch[R any](ctx context.Context, msg Message[R]) (R, error)
- func (b *Bus) Handle[M Message[R], R any](h HandlerFunc[M, R])
- func (b *Bus) HandleVoid[M Message[Void]](h func(ctx context.Context, msg M) error)
- func (b *Bus) HandledTypes() []string
- func (b *Bus) Use(mw ...Middleware)
- type Event
- type HandlerFunc
- type Hook
- type HookFailedError
- type Message
- type Middleware
- type Named
- type Next
- type Phase
- type Returns
- type Void
Constants ¶
This section is empty.
Variables ¶
var ErrNoHandler = errors.New("no handler registered for message")
Functions ¶
Types ¶
type Bus ¶
type Bus struct {
// contains filtered or unexported fields
}
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) 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 ¶
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 ¶
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 ¶
Event describes a point in the lifecycle of a dispatch. Result and Err are only set for PhaseAfter.
type HandlerFunc ¶
HandlerFunc handles a single type of message.
type Hook ¶
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 ¶
func (HookFailedError) Error ¶
func (e HookFailedError) Error() string
func (HookFailedError) Unwrap ¶
func (e HookFailedError) Unwrap() error
type Middleware ¶
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.