Documentation
¶
Overview ¶
Package eventloop provides an event loop which is widely used by modules. The use of the event loop enables many of the modules to run synchronously, thus removing the need for thread safety. This simplifies the implementation of modules and reduces the risks of race conditions.
The event loop can accept events of any type. It uses reflection to determine what handler function to execute based on the type of an event.
Index ¶
- type EventHandler
- type EventLoop
- func (el *EventLoop) AddEvent(event any)
- func (el *EventLoop) AddTicker(interval time.Duration, callback func(tick time.Time) (event any)) int
- func (el *EventLoop) Context() context.Context
- func (el *EventLoop) DelayUntil(eventType, event any)
- func (el *EventLoop) InitModule(mods *modules.Core)
- func (el *EventLoop) RegisterHandler(eventType any, handler EventHandler, opts ...HandlerOption) int
- func (el *EventLoop) RemoveTicker(id int) bool
- func (el *EventLoop) Run(ctx context.Context)
- func (el *EventLoop) Tick(ctx context.Context) bool
- func (el *EventLoop) UnregisterHandler(eventType any, id int)
- type HandlerOption
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type EventLoop ¶
type EventLoop struct {
// contains filtered or unexported fields
}
EventLoop accepts events of any type and executes registered event handlers.
func (*EventLoop) AddTicker ¶
func (el *EventLoop) AddTicker(interval time.Duration, callback func(tick time.Time) (event any)) int
AddTicker adds a ticker with the specified interval and returns the ticker id. The ticker will send the specified event on the event loop at regular intervals. The returned ticker id can be used to remove the ticker with RemoveTicker. The ticker will not be started before the event loop is running.
func (*EventLoop) Context ¶ added in v0.5.0
Context returns the context associated with the event loop. Usually, this context will be the one passed to Run. However, if Tick is used instead of Run, Context will return the last context that was passed to Tick. If neither Run nor Tick have been called, Context returns context.Background.
func (*EventLoop) DelayUntil ¶
DelayUntil allows us to delay handling of an event until after another event has happened. The eventType parameter decides the type of event to wait for, and it should be the zero value of that event type. The event parameter is the event that will be delayed.
func (*EventLoop) InitModule ¶ added in v0.5.0
func (*EventLoop) RegisterHandler ¶
func (el *EventLoop) RegisterHandler(eventType any, handler EventHandler, opts ...HandlerOption) int
RegisterHandler registers the given event handler for the given event type with the given handler options, if any. If no handler options are provided, the default handler options will be used.
func (*EventLoop) RemoveTicker ¶
RemoveTicker removes the ticker with the specified id. If the ticker was removed, RemoveTicker will return true. If the ticker does not exist, false will be returned instead.
func (*EventLoop) Run ¶
Run runs the event loop. A context object can be provided to stop the event loop.
func (*EventLoop) Tick ¶ added in v0.4.0
Tick processes a single event. Returns true if an event was handled.
func (*EventLoop) UnregisterHandler ¶ added in v0.5.0
UnregisterHandler unregisters the handler for the given event type with the given id.
type HandlerOption ¶ added in v0.5.0
type HandlerOption func(*handlerOpts)
HandlerOption sets configuration options for event handlers.
func Prioritize ¶ added in v0.5.0
func Prioritize() HandlerOption
Prioritize instructs the event loop to run the handler before handlers that do not have priority. It should only be used if you must look at an event before other handlers get to look at it.
func UnsafeRunInAddEvent ¶ added in v0.5.0
func UnsafeRunInAddEvent() HandlerOption
UnsafeRunInAddEvent instructs the eventloop to run the handler as a part of AddEvent. Handlers that use this option can process events before they are added to the event queue. Because AddEvent could be running outside the event loop, it is unsafe. Only thread-safe modules can be used safely from a handler using this option.