handler

package
v0.4.2 Latest Latest
Warning

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

Go to latest
Published: Sep 15, 2023 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrRunning = errors.New("event handler is already running")

ErrRunning is the error returned when trying to run an event handler that is already running.

Functions

This section is empty.

Types

type Handler

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

Handler is responsible for managing the registration and execution of event handlers in response to incoming events. It maintains a map of registered event handlers and listens to an event bus for events that match registered handler names. When such an event occurs, the corresponding handler function is invoked. Handler also provides support for querying stored events from an event store, and it can run concurrently, with its execution state being controlled through a provided context.

func New

func New(bus event.Bus, opts ...Option) *Handler

New creates a new Handler with the provided event bus and applies the given options. The bus parameter is used to subscribe to events and distribute them to registered event handlers. Options can be used to configure the Handler, for example to specify an event store where past events are queried from on startup. The returned Handler is not running and must be started with the Run method.

func (*Handler) Context

func (h *Handler) Context() context.Context

Context returns the context of the Handler. This context is set when the Run method is called and is used to control the lifecycle of the Handler. It's protected by a read-write lock, ensuring safe concurrent access.

func (*Handler) EventHandler added in v0.2.12

func (h *Handler) EventHandler(name string) (func(event.Event), bool)

EventHandler retrieves the event handling function associated with the provided name. The function returned is responsible for processing an event of that name. If no handler is found for the given name, a nil function and false are returned.

func (*Handler) RegisterEventHandler

func (h *Handler) RegisterEventHandler(name string, fn func(event.Event))

RegisterEventHandler registers an event handler function for the given event name. The event handler function, which takes an event.Event as a parameter, will be called whenever an event with the registered name occurs. The function is thread-safe and can be called concurrently.

func (*Handler) Run

func (h *Handler) Run(ctx context.Context) (<-chan error, error)

Run starts the event handler with the provided context. It subscribes to all registered events on the event bus and begins handling incoming events. If the event handler has an event store, it also handles all stored events. If the handler is already running, it returns an error. The function returns a channel for errors that may occur during event handling and an error if there was an issue starting the handler.

func (*Handler) Running

func (h *Handler) Running() bool

Running checks if the Handler is currently running. It returns true if the Handler has been started and not yet stopped, otherwise it returns false. The context of the Handler is used to determine its state. If the context is not nil, it indicates that the Handler is running.

type Option

type Option func(*Handler)

Option is a function that modifies the configuration of a Handler. It provides a way to set optional parameters when creating a new Handler. This approach is used to avoid constructor cluttering when there are many configurations possible for a Handler.

func WithStore

func WithStore(store event.Store) Option

WithStore is a function that returns an Option which, when applied to a Handler, sets the event.Store of that Handler. This is used to configure where the Handler stores events.

Jump to

Keyboard shortcuts

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