eventloop

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Dec 6, 2021 License: MIT Imports: 4 Imported by: 0

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

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type EventHandler

type EventHandler func(event interface{})

EventHandler processes an event.

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 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 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

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() bool

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

Jump to

Keyboard shortcuts

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