eventemitter

package
v0.0.0-...-422e9ff Latest Latest
Warning

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

Go to latest
Published: May 12, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package eventemitter provides a flexible event bus implementation for handling bot events.

Index

Constants

This section is empty.

Variables

View Source
var ErrBreak = errors.New("break")

ErrBreak is a special error that can be returned by a listener to stop further event propagation.

Functions

This section is empty.

Types

type ErrorHandler

type ErrorHandler func(event string, err error)

ErrorHandler is called when a listener returns an error.

type EventEmitter

type EventEmitter interface {
	// AddListener registers a listener for the given event.
	AddListener(event string, listener Listener) UnsubscribeFunc
	// Once registers a listener that will be called only once.
	Once(event string, listener Listener) UnsubscribeFunc
	// Emit notifies all listeners of the given event with the provided payload.
	Emit(ctx context.Context, event string, payload any)
	// Use applies middleware to the given event.
	Use(event string, middleware ...Middleware)
	// ListenerCount returns the number of listeners for the given event.
	ListenerCount(event string) int
	// RemoveAllListeners removes all listeners for the given event.
	RemoveAllListeners(event string)
}

EventEmitter defines the interface for event management.

type Listener

type Listener interface {
	Handle(ctx context.Context, payload any) error
}

Listener is an interface that handles an event.

type ListenerFunc

type ListenerFunc func(ctx context.Context, payload any) error

ListenerFunc is an adapter to allow the use of ordinary functions as Listener.

func (ListenerFunc) Handle

func (f ListenerFunc) Handle(ctx context.Context, payload any) error

Handle calls f(ctx, payload).

type Middleware

type Middleware interface {
	Handle(next Listener) Listener
}

Middleware is an interface that wraps a listener.

type MiddlewareFunc

type MiddlewareFunc func(next Listener) Listener

MiddlewareFunc is an adapter to allow the use of ordinary functions as Middleware.

func (MiddlewareFunc) Handle

func (f MiddlewareFunc) Handle(next Listener) Listener

Handle calls f(next).

type OptOptionsSetter

type OptOptionsSetter func(o *Options)

func WithErrorHandler

func WithErrorHandler(opt ErrorHandler) OptOptionsSetter

func WithStopOnError

func WithStopOnError(opt bool) OptOptionsSetter

type Options

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

Options defines the configuration for an EventEmitter.

func NewOptions

func NewOptions(
	options ...OptOptionsSetter,
) Options

func (*Options) Validate

func (o *Options) Validate() error

type SyncEventEmitter

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

SyncEventEmitter is a concrete implementation of EventEmitter.

func NewSync

func NewSync(opts Options) (*SyncEventEmitter, error)

NewSync creates a new SyncEventEmitter.

func (*SyncEventEmitter) AddListener

func (e *SyncEventEmitter) AddListener(event string, listener Listener) UnsubscribeFunc

AddListener adds a listener for the given event.

func (*SyncEventEmitter) Emit

func (e *SyncEventEmitter) Emit(ctx context.Context, event string, payload any)

Emit notifies all listeners of the given event with the provided payload.

func (*SyncEventEmitter) ListenerCount

func (e *SyncEventEmitter) ListenerCount(event string) int

ListenerCount returns the number of listeners for the given event.

func (*SyncEventEmitter) Once

func (e *SyncEventEmitter) Once(event string, listener Listener) UnsubscribeFunc

Once registers a listener that will be called only once.

func (*SyncEventEmitter) RemoveAllListeners

func (e *SyncEventEmitter) RemoveAllListeners(event string)

RemoveAllListeners removes all listeners for the given event.

func (*SyncEventEmitter) Use

func (e *SyncEventEmitter) Use(event string, middleware ...Middleware)

Use applies middleware to the given event.

type TypedListener

type TypedListener[T any] func(ctx context.Context, payload *T) error

TypedListener is a generic listener function that accepts a specific payload type.

type UnsubscribeFunc

type UnsubscribeFunc func()

UnsubscribeFunc is a function that unregisters a listener.

func On

func On[E any](ee EventEmitter, event string, handler TypedListener[E]) UnsubscribeFunc

On registers a typed handler for a specific event.

Jump to

Keyboard shortcuts

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