eventloop

package
v2.5.3 Latest Latest
Warning

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

Go to latest
Published: Sep 27, 2024 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Handler

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

Handler represents a handler for processing events.

func NewReadHandler

func NewReadHandler(ch reflect.Value, h HandlerFunc) Handler

NewReadHandler creates a new Handler instance with the given channel and handler function. The channel is used to receive events, and the handler function is called to handle each event.

type HandlerFunc

type HandlerFunc func(value reflect.Value, closed bool)

HandlerFunc is a function type that represents a handler for events in the event loop. It takes two parameters: `value` of type `reflect.Value` which represents the event value, and `closed` of type `bool` which indicates whether the event loop is closed.

type Loop

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

Loop represents an event loop that handles events using registered handlers.

func New

func New() *Loop

New creates a new event loop.

func (*Loop) Add

func (el *Loop) Add(h ...Handler)

Add adds one or more event handlers to the event loop. The event handlers will be executed when the event loop is triggered. It is safe to call this method concurrently from multiple goroutines. The order in which the event handlers are added is preserved. The event loop will wake up after the handlers are added.

func (*Loop) Remove

func (el *Loop) Remove(h ...Handler)

Remove removes the specified handlers from the event loop. It takes one or more handlers as arguments and removes them by their channels. The channels of the handlers are collected into a slice and passed to the RemoveByChannels method.

func (*Loop) RemoveByChannels

func (el *Loop) RemoveByChannels(ch ...reflect.Value)

RemoveByChannels removes the handlers associated with the specified channels from the event loop. The channels are sorted based on their memory addresses before removing the handlers. After removing the handlers, the event loop is woken up to process any pending events.

func (*Loop) Run

func (el *Loop) Run(stop <-chan struct{})

Run starts the event loop and runs until the stop channel is closed. It continuously listens for events on the stop channel and the wake channel. When an event is received on the stop channel, the event loop stops and returns. When an event is received on the wake channel, the event loop continues to the next iteration. The event loop executes the registered handlers based on the received events. If an event handler returns an error, the event loop removes the handler and calls it with a nil value and the error set to true. If an event handler panics, the event loop removes the handler and panics with a descriptive error message.

func (*Loop) Wake

func (el *Loop) Wake()

Wake wakes up the event loop by sending a signal through the wake channel. If the wake channel is already full, the signal is dropped.

Jump to

Keyboard shortcuts

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