Documentation
¶
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.