event

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Nov 12, 2022 License: MIT Imports: 4 Imported by: 8

README

event

Go event library to subscribe and fire events used to decouple the event source and sink in complex or ad-don based systems.

// Custom event type
type MyEvent struct {
    Message string
}

// Create a new event manager.
mgr := New()

// Subscribe to the event.
Subscribe(mgr, 0, func(e *MyEvent) {
    // Handle the event.
    fmt.Println("handler A received event:", e.Message)
})

// Subscribe to the event higher priority.
Subscribe(mgr, 1, func(e *MyEvent) {
    // Handle the event.
    fmt.Println("handler B received event:", e.Message)
    e.Message = "hello gophers"
})

unsubscribe := Subscribe(mgr, 0, func(e *MyEvent) {
    // Handle the event.
    fmt.Println("handler C received event:", e.Message)
})
// Unsubscribe from the event if you want to or ignore and never call it.
unsubscribe()

// Fire the event.
mgr.Fire(&MyEvent{Message: "hello world"})

Used by

Documentation

Overview

Example
// Custom event type
type MyEvent struct {
	Message string
}

// Create a new event manager.
mgr := New()

// Subscribe to the event.
Subscribe(mgr, 0, func(e *MyEvent) {
	// Handle the event.
	fmt.Println("handler A received event:", e.Message)
})

// Subscribe to the event higher priority.
Subscribe(mgr, 1, func(e *MyEvent) {
	// Handle the event.
	fmt.Println("handler B received event:", e.Message)
	e.Message = "hello gophers"
})

unsubscribe := Subscribe(mgr, 0, func(e *MyEvent) {
	// Handle the event.
	fmt.Println("handler C received event:", e.Message)
})
// Unsubscribe from the event if you want to or ignore and never call it.
unsubscribe()

// Fire the event.
mgr.Fire(&MyEvent{Message: "hello world"})
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func FireParallel

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

FireParallel fires an event in a new goroutine and returns immediately. The subscribers are called in order of priority and the event value is passed to the next subscriber.

It optionally runs handlers in the goroutine after all subscribers are done. If an after handler panics no further handlers in the slice are run.

func FireParallelChan

func FireParallelChan[T Event](mgr Manager, event T) (resultChan <-chan T)

FireParallelChan fires an event in a new goroutine and returns a result channel immediately. The subscribers are called in order of priority and the event value is passed to the next subscriber.

func Subscribe

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.

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 Event, 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 and returns immediately.
	// The subscribers are called in order of priority and the event value is passed to the next subscriber.
	//
	// It optionally runs handlers in the goroutine after all subscribers are done.
	// If an after handler panics no further handlers in the slice are run.
	FireParallel(event Event, after ...HandlerFunc)

	// Wait blocks until no event handlers are running for the specified events.
	// If no events are specified it waits for all events.
	Wait(events ...Event)

	// HasSubscriber determines whether all given events have at least one subscriber.
	// If no events are specified it returns true if there are any subscribers for any event.
	//
	// It is useful to check whether an event is subscribed for before firing it when
	// the event value is expensive to create.
	HasSubscriber(events ...Event) bool
	// UnsubscribeAll unsubscribes all subscribers of the given events
	// and returns the number of subscribers unsubscribed.
	UnsubscribeAll(events ...Event) int
}

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(opts ...ManagerOption) Manager

New returns a new event Manager.

type ManagerOption

type ManagerOption func(*manager)

ManagerOption is a Manager option for New.

func WithLogger

func WithLogger(log logr.Logger) ManagerOption

WithLogger returns a ManagerOption that sets the logger. Default is logr.Discard().

func WithRecoverPanic

func WithRecoverPanic(enabled bool) ManagerOption

WithRecoverPanic returns a ManagerOption that enables/disables panic recovery. Default is true.

type Type

type Type reflect.Type

Type is an event type.

Jump to

Keyboard shortcuts

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