Documentation
¶
Overview ¶
Package eventemitter provides a flexible event bus implementation for handling bot events.
Index ¶
- Variables
- type ErrorHandler
- type EventEmitter
- type Listener
- type ListenerFunc
- type Middleware
- type MiddlewareFunc
- type OptOptionsSetter
- type Options
- type SyncEventEmitter
- func (e *SyncEventEmitter) AddListener(event string, listener Listener) UnsubscribeFunc
- func (e *SyncEventEmitter) Emit(ctx context.Context, event string, payload any)
- func (e *SyncEventEmitter) ListenerCount(event string) int
- func (e *SyncEventEmitter) Once(event string, listener Listener) UnsubscribeFunc
- func (e *SyncEventEmitter) RemoveAllListeners(event string)
- func (e *SyncEventEmitter) Use(event string, middleware ...Middleware)
- type TypedListener
- type UnsubscribeFunc
Constants ¶
This section is empty.
Variables ¶
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 ¶
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 ListenerFunc ¶
ListenerFunc is an adapter to allow the use of ordinary functions as Listener.
type Middleware ¶
Middleware is an interface that wraps a listener.
type MiddlewareFunc ¶
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
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 ¶
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.