event

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: May 19, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package events provides a lightweight, thread-safe, in-process event emitter for decoupling controllers from side-effects (emails, audit logs, etc.).

Index

Constants

This section is empty.

Variables

View Source
var DefaultEmitter = New()

DefaultEmitter is the package-level default emitter.

Functions

This section is empty.

Types

type BaseEvent

type BaseEvent struct {
	EventName string
	EventData any
}

BaseEvent is a simple implementation of Event.

func (BaseEvent) Data

func (e BaseEvent) Data() any

func (BaseEvent) Name

func (e BaseEvent) Name() string

type Emitter

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

Emitter is a thread-safe in-process event bus.

func New

func New() *Emitter

New creates a new Emitter with a default worker pool of 100.

func (*Emitter) Emit

func (e *Emitter) Emit(ctx context.Context, event Event)

Emit fires all listeners for the given event synchronously.

func (*Emitter) EmitAsync

func (e *Emitter) EmitAsync(ctx context.Context, event Event)

EmitAsync fires all listeners for the given event using a fixed worker pool.

func (*Emitter) EmitPayload

func (e *Emitter) EmitPayload(ctx context.Context, eventName string, data any)

EmitPayload fires an event with a simple string name and any data.

func (*Emitter) Off

func (e *Emitter) Off(eventName string, listener Listener)

Off removes exactly the specified handler.

func (*Emitter) On

func (e *Emitter) On(eventName string, listener Listener)

On registers a listener for an event name.

func (*Emitter) OnFunc

func (e *Emitter) OnFunc(eventName string, fn func(ctx context.Context, event Event) error)

OnFunc is a helper to register a function as a listener.

func (*Emitter) OnPayload

func (e *Emitter) OnPayload(eventName string, fn func(data any))

OnPayload registers a listener that receives the raw data.

func (*Emitter) Once

func (e *Emitter) Once(eventName string, listener Listener)

Once registers a listener that fires only once.

type Event

type Event interface {
	Name() string
	Data() any
}

Event is the interface that all events must implement.

type JobFailedEvent

type JobFailedEvent struct {
	ID      string
	JobType string
	Error   error
}

func (JobFailedEvent) Data

func (e JobFailedEvent) Data() any

func (JobFailedEvent) Name

func (e JobFailedEvent) Name() string

type JobProcessedEvent

type JobProcessedEvent struct {
	ID       string
	JobType  string
	Duration time.Duration
}

func (JobProcessedEvent) Data

func (e JobProcessedEvent) Data() any

func (JobProcessedEvent) Name

func (e JobProcessedEvent) Name() string

type JobProcessingEvent

type JobProcessingEvent struct {
	ID      string
	JobType string
}

func (JobProcessingEvent) Data

func (e JobProcessingEvent) Data() any

func (JobProcessingEvent) Name

func (e JobProcessingEvent) Name() string

type JobQueuedEvent

type JobQueuedEvent struct {
	ID      string
	JobType string
	Queue   string
}

Queue events

func (JobQueuedEvent) Data

func (e JobQueuedEvent) Data() any

func (JobQueuedEvent) Name

func (e JobQueuedEvent) Name() string

type Listener

type Listener interface {
	Handle(ctx context.Context, event Event) error
}

Listener is the interface for event consumers.

type ListenerFunc

type ListenerFunc func(ctx context.Context, event Event) error

ListenerFunc is a helper to use functions as listeners.

func (ListenerFunc) Handle

func (f ListenerFunc) Handle(ctx context.Context, event Event) error

Handle implements the Listener interface.

type MailSendingEvent

type MailSendingEvent struct {
	To      string
	Subject string
}

Mail events

func (MailSendingEvent) Data

func (e MailSendingEvent) Data() any

func (MailSendingEvent) Name

func (e MailSendingEvent) Name() string

type MailSentEvent

type MailSentEvent struct {
	To      string
	Subject string
}

func (MailSentEvent) Data

func (e MailSentEvent) Data() any

func (MailSentEvent) Name

func (e MailSentEvent) Name() string

type QueryExecutedEvent

type QueryExecutedEvent struct {
	SQL      string
	Args     []any
	Duration time.Duration
	Error    error
}

DB events

func (QueryExecutedEvent) Data

func (e QueryExecutedEvent) Data() any

func (QueryExecutedEvent) Name

func (e QueryExecutedEvent) Name() string

type RedisCommandExecutedEvent

type RedisCommandExecutedEvent struct {
	Command  string
	Args     []any
	Duration time.Duration
	Error    error
}

Redis events

func (RedisCommandExecutedEvent) Data

func (e RedisCommandExecutedEvent) Data() any

func (RedisCommandExecutedEvent) Name

type RequestFinishedEvent

type RequestFinishedEvent struct {
	Method   string
	Path     string
	Status   int
	Duration time.Duration
}

func (RequestFinishedEvent) Data

func (e RequestFinishedEvent) Data() any

func (RequestFinishedEvent) Name

func (e RequestFinishedEvent) Name() string

type RequestStartedEvent

type RequestStartedEvent struct {
	Method string
	Path   string
	IP     string
}

Request lifecycle events

func (RequestStartedEvent) Data

func (e RequestStartedEvent) Data() any

func (RequestStartedEvent) Name

func (e RequestStartedEvent) Name() string

type ServerStartedEvent

type ServerStartedEvent struct {
	Addr string
}

func (ServerStartedEvent) Data

func (e ServerStartedEvent) Data() any

func (ServerStartedEvent) Name

func (e ServerStartedEvent) Name() string

type ServerStartingEvent

type ServerStartingEvent struct {
	Addr string
}

Server lifecycle events

func (ServerStartingEvent) Data

func (e ServerStartingEvent) Data() any

func (ServerStartingEvent) Name

func (e ServerStartingEvent) Name() string

type ServerStoppedEvent

type ServerStoppedEvent struct{}

func (ServerStoppedEvent) Data

func (e ServerStoppedEvent) Data() any

func (ServerStoppedEvent) Name

func (e ServerStoppedEvent) Name() string

type ServerStoppingEvent

type ServerStoppingEvent struct {
	Signal string
}

func (ServerStoppingEvent) Data

func (e ServerStoppingEvent) Data() any

func (ServerStoppingEvent) Name

func (e ServerStoppingEvent) Name() string

Jump to

Keyboard shortcuts

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