Documentation ¶
Overview ¶
Package mob is a simple mediator / observer library. It supports in-process requests / events processing.
Index ¶
- Variables
- func AddInterceptor(interceptor Interceptor)
- func AddInterceptorTo(m *Mob, interceptor Interceptor)
- func Notify[T any](ctx context.Context, event T) error
- func RegisterEventHandler[T any](hn EventHandler[T], opts ...Option) error
- func RegisterEventHandlerTo[T any](m *Mob, ehn EventHandler[T], opts ...Option) error
- func RegisterRequestHandler[T any, U any](hn RequestHandler[T, U], opts ...Option) error
- func RegisterRequestHandlerTo[T any, U any](m *Mob, rhn RequestHandler[T, U], opts ...Option) error
- func Send[T any, U any](ctx context.Context, req T) (U, error)
- type AggregateHandlerError
- type EventHandler
- type EventHandlerFunc
- type EventNotifier
- type Interceptor
- type Mob
- type Option
- type RequestHandler
- type RequestHandlerFunc
- type RequestSender
- type SendInvoker
Constants ¶
This section is empty.
Variables ¶
var ( // ErrHandlerNotFound indicates that a requested handler is not registered. ErrHandlerNotFound = errors.New("mob: handler not found") // ErrInvalidHandler indicates that a given handler is not valid. ErrInvalidHandler = errors.New("mob: invalid handler") // ErrDuplicateHandler indicates that a handler for a given request / response pair is already registered. // It applies only to request handlers. ErrDuplicateHandler = errors.New("mob: duplicate handler") // ErrUnmarshal indicates that a request or a response type is malformed and cannot be // unmarshal to a given type. // It happens when a request or a response type is modified in the request processing pipeline. ErrUnmarshal = errors.New("mob: failed to unmarshal") )
Functions ¶
func AddInterceptor ¶ added in v1.3.0
func AddInterceptor(interceptor Interceptor)
AddInterceptorTo adds an Interceptor to the global Mob instance. Interceptors are invoked in order they're added to the chain.
func AddInterceptorTo ¶ added in v1.3.0
func AddInterceptorTo(m *Mob, interceptor Interceptor)
AddInterceptorTo adds an Interceptor to the given Mob instance. Interceptors are invoked in order they're added to the chain.
func Notify ¶
Notify dispatches a given event and execute all handlers registered with a dispatched event's type. Handlers are executed concurrently and errors are collected, if any, they're returned to the client.
If there is no appropriate handler in the global Mob instance, ErrHandlerNotFound is returned.
func RegisterEventHandler ¶
func RegisterEventHandler[T any](hn EventHandler[T], opts ...Option) error
RegisterEventHandler adds a given event handler to the global Mob instance. Returns nil if the handler added successfully, an error otherwise.
Multiple event handlers can be registered for a single event's type.
func RegisterEventHandlerTo ¶ added in v1.1.0
func RegisterEventHandlerTo[T any](m *Mob, ehn EventHandler[T], opts ...Option) error
RegisterEventHandlerTo adds a given event handler to the given Mob instance. Returns nil if the handler added successfully, an error otherwise.
Multiple event handlers can be registered for a single event's type.
func RegisterRequestHandler ¶
func RegisterRequestHandler[T any, U any](hn RequestHandler[T, U], opts ...Option) error
RegisterRequestHandler adds a given request handler to the global Mob instance. Returns nil if the handler added successfully, an error otherwise.
An only one handler for a given request-response pair can be registered. If support for multiple handlers for the same request-response pairs is needed within the Mob global instance, introduce type aliasing to avoid handlers' collision.
func RegisterRequestHandlerTo ¶ added in v1.1.0
RegisterRequestHandlerTo adds a given request handler to the given Mob instance. Returns nil if the handler added successfully, an error otherwise.
An only one handler for a given request-response pair can be registered. If support for multiple handlers for the same request-response pairs is needed within the same Mob instance, introduce type aliasing to avoid handlers' collision.
Types ¶
type AggregateHandlerError ¶
type AggregateHandlerError []error
An AggregateHandlerError is a type alias for a slice of handler errors. It applies only to event handlers.
func (AggregateHandlerError) Error ¶
func (e AggregateHandlerError) Error() string
func (AggregateHandlerError) Is ¶
func (e AggregateHandlerError) Is(target error) bool
type EventHandler ¶
EventHandler provides an interface for an event handler.
type EventHandlerFunc ¶ added in v1.2.0
EventHandlerFunc type is an adapter to allow the use of ordinary functions as event handlers.
type EventNotifier ¶ added in v1.1.0
type EventNotifier[T any] interface { // Notify dispatches a given event and execute all handlers registered with a dispatched event's type. // Handlers are executed concurrently and errors are collected, if any, they're returned to the client. // // If there is no appropriate handler in the notifier's Mob instance, ErrHandlerNotFound is returned. Notify(ctx context.Context, event T) error }
EventNotifier is the interface that wraps the mob's Notify method.
func NewEventNotifier ¶ added in v1.1.0
func NewEventNotifier[T any](m *Mob) EventNotifier[T]
NewEventNotifier returns an event notifier which uses a given Mob instance.
type Interceptor ¶ added in v1.3.0
type Interceptor func(ctx context.Context, req interface{}, invoker SendInvoker) (interface{}, error)
Interceptor intercepts an invocation of a Send method.
type Mob ¶ added in v1.1.0
type Mob struct {
// contains filtered or unexported fields
}
A Mob is a request / event handlers registry.
type Option ¶ added in v1.2.0
type Option interface {
// contains filtered or unexported methods
}
Option configures a handler during the registration process.
type RequestHandler ¶
RequestHandler provides an interface for a request handler.
type RequestHandlerFunc ¶ added in v1.2.0
RequestHandlerFunc type is an adapter to allow the use of ordinary functions as request handlers.
type RequestSender ¶ added in v1.1.0
type RequestSender[T any, U any] interface { // Send sends a given request T to an appropriate handler and returns a response U. // // If the appropriate handler does not exist in the sender's Mob instance, ErrHandlerNotFound is returned. Send(ctx context.Context, req T) (U, error) }
RequestSender is the interface that wraps the mob's Send method.
func NewRequestSender ¶ added in v1.1.0
func NewRequestSender[T any, U any](m *Mob) RequestSender[T, U]
NewRequestSender returns a request sender which uses a given Mob instance.
type SendInvoker ¶ added in v1.3.0
SendInvoker is a function called by an Interceptor to invoke the next Interceptor in the chain or the underlying invoker.