eventloop

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Oct 27, 2025 License: MIT Imports: 7 Imported by: 0

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

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type EventHandler

type EventHandler func(event any)

EventHandler processes an event.

type EventLoop

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

EventLoop accepts events of any type and executes registered event handlers.

func New

func New(bufferSize uint) *EventLoop

New returns a new event loop with the requested buffer size.

func (*EventLoop) AddEvent

func (el *EventLoop) AddEvent(event any)

AddEvent adds an event to the event queue.

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

func (el *EventLoop) Context() context.Context

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

func (el *EventLoop) DelayUntil(eventType, event any)

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 (el *EventLoop) InitModule(mods *modules.Core)

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

func (el *EventLoop) RemoveTicker(id int) bool

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

func (el *EventLoop) Run(ctx context.Context)

Run runs the event loop. A context object can be provided to stop the event loop.

func (*EventLoop) Tick added in v0.4.0

func (el *EventLoop) Tick(ctx context.Context) bool

Tick processes a single event. Returns true if an event was handled.

func (*EventLoop) UnregisterHandler added in v0.5.0

func (el *EventLoop) UnregisterHandler(eventType any, id int)

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.

Jump to

Keyboard shortcuts

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