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.
The event loop also allows observers/handlers to run at two different times:
1. When an event is added (concurrent with the event loop). 2. When an event has passed through the event queue.
The former allows for concurrent handling of events, as multiple goroutines can be adding events concurrently, which means that the observers or handlers can run concurrently. The latter makes it possible to write handlers that will run on the same goroutine, one at a time.
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) RegisterAsyncHandler(eventType interface{}, handler EventHandler)
- func (el *EventLoop) RegisterAsyncObserver(eventType interface{}, observer EventHandler)
- 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)
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.
The event may be processed or consumed by an async handler before it enters the queue. It is not safe to call this function from the the event loop goroutine. If you need to send add an event from a handler, use a goroutine:
go EventLoop.AddEvent(...)
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) RegisterAsyncHandler ¶
func (el *EventLoop) RegisterAsyncHandler(eventType interface{}, handler EventHandler)
RegisterAsyncHandler registers a handler for events with the same type as the 'eventType' argument. The handler is executed asynchronously, immediately after a new event arrives. The handler consumes the event, which means that it will not be observed by synchronous observers. There can be only one handler per event type.
func (*EventLoop) RegisterAsyncObserver ¶
func (el *EventLoop) RegisterAsyncObserver(eventType interface{}, observer EventHandler)
RegisterAsyncObserver registers an observer for events with the same type as the 'eventType' argument. The observer is executed asynchronously before any registered handler.
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.