events

package
v0.26.1 Latest Latest
Warning

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

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

Documentation

Overview

Package events contains a manager for dispatching and receiving arbitrary events.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Event

type Event interface {
	proto.Message
	GetTime() *timestamppb.Timestamp
	GetMessage() string
}

An Event is any protobuf message that has a time and message.

type EventSink

type EventSink func(Event)

An EventSink receives events.

type EventSinkHandle

type EventSinkHandle string

An EventSinkHandle is a reference to a registered EventSink so that it can be unregistered.

type Handle added in v0.24.0

type Handle string

A Handle represents a listener.

type LastError

type LastError = events.LastError

LastError re-exports events.LastError.

type Listener added in v0.24.0

type Listener[T any] func(ctx context.Context, event T)

A Listener is a function that listens for events of type T.

type Manager

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

A Manager manages the dispatching of events to event sinks.

func New

func New() *Manager

New creates a new Manager.

func (*Manager) Dispatch

func (mgr *Manager) Dispatch(evt Event)

Dispatch dispatches an event to any registered event sinks.

func (*Manager) Register

func (mgr *Manager) Register(sink EventSink) (handle EventSinkHandle)

Register registers an event sink to receive events.

func (*Manager) Unregister

func (mgr *Manager) Unregister(sinkHandle EventSinkHandle)

Unregister unregisters an event sink so it no longer receives events.

type Target added in v0.24.0

type Target[T any] struct {
	// contains filtered or unexported fields
}

A Target is a target for events.

Listeners are added with AddListener with a function to be called when the event occurs. AddListener returns a Handle which can be used to remove a listener with RemoveListener.

Dispatch dispatches events to all the registered listeners.

Target is safe to use in its zero state.

Each listener is run in its own goroutine.

A slow listener will cause the next event dispatch to block. This is the opposite behavior from Manager.

Close will remove and cancel all listeners.

func (*Target[T]) AddListener added in v0.24.0

func (t *Target[T]) AddListener(listener Listener[T]) Handle

AddListener adds a listener to the target.

func (*Target[T]) Close added in v0.24.0

func (t *Target[T]) Close()

Close closes the event target. This can be called multiple times safely.

func (*Target[T]) Dispatch added in v0.24.0

func (t *Target[T]) Dispatch(ctx context.Context, evt T)

Dispatch dispatches an event to all listeners.

func (*Target[T]) RemoveListener added in v0.24.0

func (t *Target[T]) RemoveListener(handle Handle)

RemoveListener removes a listener from the target.

Jump to

Keyboard shortcuts

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