event

package
v1.3.2 Latest Latest
Warning

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

Go to latest
Published: Sep 12, 2024 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrListenerNil listener is nil
	ErrListenerNil = errors.New("listener is nil")

	// ErrEventNil event is nil
	ErrEventNil = errors.New("event is nil")

	// ErrBusClosed bus was closed
	ErrBusClosed = errors.New("bus was closed")

	// ErrEventTypeInvalid event type is invalid
	ErrEventTypeInvalid = errors.New("event type is invalid")

	// ErrListenerIncomparable listener is incomparable
	ErrListenerIncomparable = errors.New("listener is incomparable")
)

Functions

This section is empty.

Types

type Bus

type Bus interface {
	// On adds a Listener to the Event bus.
	On(e Event, lis Listener) error

	// Prepend adds the Listener to the beginning of the listeners.
	Prepend(e Event, lis Listener) error

	// Once adds a one-time Listener to the Event bus.
	Once(e Event, lis Listener) error

	// PrependOnce adds a one-time Listener to the beginning of the listeners.
	PrependOnce(e Event, lis Listener) error

	// Emit synchronously calls each of the listeners registered for the specified Event,
	// in the order they were registered.
	Emit(e Event) error

	// AsyncEmit asynchronously calls each of the listeners registered for the specified Event.
	AsyncEmit(e Event) <-chan error

	// Off removes the specified Listener from the listeners.
	Off(e Event, lis Listener) error

	// OffAll removes all listeners for the specified Event.
	OffAll(e Event) error

	// Close bus gracefully.
	Close(ctx context.Context) error
}

func NewBus

func NewBus(opts ...Option) Bus

type ErrListener

type ErrListener struct {
	EventType reflect.Type
}

func (ErrListener) Error

func (e ErrListener) Error() string

type Event

type Event interface {

	// When return the time of the event.
	When() time.Time

	// ID return the id of the event.
	ID() any

	// Body return the body of the event.
	Body() any

	// Type return the body's reflect.Type of the event.
	Type() reflect.Type

	// WithContext returns a shallow copy of e with its context changed to ctx.
	// The provided ctx must be non-nil.
	WithContext(ctx context.Context) Event

	// Context returns the context of the event. To change the context, use WithContext.
	Context() context.Context
}

Event is an interface with a specific type that is associated to a specific Listener.

func NewEvent

func NewEvent(body any, id any) Event

type Listener

type Listener interface {
	// Handle handles Event logic.
	Handle(event Event) error
}

Listener is Event listener interface.

type Option

type Option func(*option)

func MaxBackoff

func MaxBackoff(backoff int) Option

func Pool

func Pool(pool gopher.Gopher) Option

Jump to

Keyboard shortcuts

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