Documentation ¶
Overview ¶
Package eventloop provides an event loop which is widely used by modules.
The event loop allows for flexible handling of events through the concept of observers and handlers. An observer is a function that is able to view an event before it is handled. Thus, there can be multiple observers for each event type. A handler is a function that processes the event. There can only be one handler for each event type.
Index ¶
- type EventHandler
- type EventLoop
- func (el *EventLoop) AddEvent(event interface{})
- func (el *EventLoop) AddTicker(interval time.Duration, callback func(tick time.Time) (event interface{})) int
- func (el *EventLoop) DelayUntil(eventType, event interface{})
- func (el *EventLoop) RegisterHandler(eventType interface{}, handler EventHandler)
- func (el *EventLoop) RegisterObserver(eventType interface{}, observer EventHandler)
- func (el *EventLoop) RemoveTicker(id int) bool
- func (el *EventLoop) Run(ctx context.Context)
- func (el *EventLoop) Tick() bool
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 relevant event handlers. It supports registering both observers and handlers based on the type of event that they accept. The difference between them is that there can be many observers per event type, but only one handler. Handlers and observers can either be executed asynchronously, immediately after AddEvent() is called, or synchronously, after the event has passed through the event queue. An asynchronous handler consumes events, which means that synchronous observers will not be notified of them.
func (*EventLoop) AddEvent ¶
func (el *EventLoop) AddEvent(event interface{})
AddEvent adds an event to the event queue.
func (*EventLoop) AddTicker ¶
func (el *EventLoop) AddTicker(interval time.Duration, callback func(tick time.Time) (event interface{})) 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) DelayUntil ¶
func (el *EventLoop) DelayUntil(eventType, event interface{})
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) RegisterHandler ¶
func (el *EventLoop) RegisterHandler(eventType interface{}, handler EventHandler)
RegisterHandler registers a handler for events with the same type as the 'eventType' argument. The handler is executed synchronously. There can be only one handler per event type.
func (*EventLoop) RegisterObserver ¶
func (el *EventLoop) RegisterObserver(eventType interface{}, observer EventHandler)
RegisterObserver registers an observer for events with the same type as the 'eventType' argument. The observer is executed synchronously before any registered handler.
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.