event

package
v0.20.0 Latest Latest
Warning

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

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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FireParallel added in v0.19.1

func FireParallel[T Event](mgr Manager, event T, after ...func(T))

FireParallel fires an event in a new goroutine and returns immediately. The event type is inferred from the argument of the handler. See Manager.FireParallel for more details.

func Subscribe added in v0.19.1

func Subscribe[T Event](mgr Manager, priority int, handler func(T)) (unsubscribe func())

Subscribe subscribes a handler to an event type with a priority. The event type is inferred from the argument of the handler. See Manager.Subscribe for more details.

Types

type Event

type Event any

Event is the event interface.

type HandlerFunc

type HandlerFunc func(e Event)

HandlerFunc is an event handler func.

type Manager

type Manager interface {
	// Subscribe subscribes a handler to an event type with a priority
	// and returns a func that can be run to unsubscribe the handler.
	//
	// HandlerFunc should return as soon as possible and start long-running tasks in parallel.
	// The Type can be any type, pointer to type or reflect.Type and the handler is only run for
	// the exact type subscribed for.
	//
	// HandlerFunc always gets the fired event of the same subscribed eventType or the same type as
	// represented by reflect.Type.
	Subscribe(eventType any, priority int, fn HandlerFunc) (unsubscribe func())
	// Fire fires an event in the calling goroutine and returns after all subscribers are complete handling it.
	// Any panic by a subscriber is caught so firing the event to the next subscriber can proceed.
	Fire(Event)
	// FireParallel fires an event in a new goroutine B and returns immediately.
	// It optionally runs handlers in goroutine B after all subscribers are done and passes
	// the potentially modified version of the fired event.
	// If an "after" handler returns false or panics no further handlers in the slice are run.
	FireParallel(event Event, after ...HandlerFunc)
	// Wait blocks until no event handlers are running.
	Wait()
	// HasSubscribers determines whether the given event has any subscribers.
	HasSubscribers(event Event) bool
}

Manager is an event manager to subscribe, fire events and decouple the event source and sink in a complex system.

var Nop Manager = &nopMgr{}

Nop is an event Manager that does nothing.

func New

func New(log logr.Logger) Manager

New returns a new event Manager optionally using a logger to log handler panics.

type Type

type Type reflect.Type

Type is an event type.

func TypeOf

func TypeOf(i any) (t Type)

TypeOf is a helper func to get the reflect.Type from i. If it is nil returns nil.

Jump to

Keyboard shortcuts

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