Documentation ¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
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 ¶
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 ¶
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
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 ¶
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 ¶
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 ¶
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.