signals

package
v1.28.0 Latest Latest
Warning

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

Go to latest
Published: Sep 12, 2026 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Package signals provides a synchronous and asynchronous event bus for the Nucleus framework. It implements a pattern similar to Django signals, allowing decoupled components to react to model lifecycle events (pre/post save, delete, etc.).

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrRedisURLRequired = errors.New("signals: redis url is required")
	ErrSignalRequired   = errors.New("signals: signal is required")
	ErrNilHandler       = errors.New("signals: handler is required")
	ErrNilBus           = errors.New("signals: bus is nil")
	ErrNilRelay         = errors.New("signals: relay is nil")
)

Functions

This section is empty.

Types

type Bus

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

Bus manages signal handlers and dispatches events.

func NewBus

func NewBus(logger *slog.Logger) *Bus

NewBus creates a new signal bus. The logger is used for async error reporting.

func (*Bus) Clear

func (b *Bus) Clear(signals ...Signal)

Clear removes all handlers for the given signal, or all handlers if no signal is specified. Useful for testing.

func (*Bus) Emit

func (b *Bus) Emit(event Event) error

Emit dispatches an event synchronously. All registered handlers for the event's signal are called in order. If any handler returns an error, execution stops and the error is returned.

func (*Bus) EmitAsync

func (b *Bus) EmitAsync(event Event)

EmitAsync dispatches an event asynchronously. Each handler runs in its own goroutine. Errors are logged but do not propagate.

func (*Bus) On

func (b *Bus) On(signal Signal, handler Handler)

On registers a handler for the given signal. Handlers are called in registration order when the signal is emitted.

type Event

type Event struct {
	Signal    Signal
	ModelName string
	Payload   any
	Ctx       context.Context
}

Event carries the data associated with a signal emission.

type Handler

type Handler func(Event) error

Handler is a function that processes an event. Returning an error from a synchronous handler aborts the operation that triggered the signal.

type RedisRelay

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

func NewRedisRelay

func NewRedisRelay(cfg RedisRelayConfig, logger *slog.Logger) (*RedisRelay, error)

func (*RedisRelay) Channel

func (r *RedisRelay) Channel(signal Signal) string

func (*RedisRelay) Close

func (r *RedisRelay) Close() error

func (*RedisRelay) ForwardToBus

func (r *RedisRelay) ForwardToBus(ctx context.Context, signal Signal, bus *Bus) error

func (*RedisRelay) Publish

func (r *RedisRelay) Publish(ctx context.Context, event Event) error

func (*RedisRelay) Subscribe

func (r *RedisRelay) Subscribe(ctx context.Context, signal Signal, handler Handler) error

type RedisRelayConfig

type RedisRelayConfig struct {
	RedisURL      string
	ChannelPrefix string
}

type Signal

type Signal string

Signal identifies the type of event being emitted.

const (
	PreCreate  Signal = "pre_create"
	PostCreate Signal = "post_create"
	PreSave    Signal = "pre_save"
	PostSave   Signal = "post_save"
	PreDelete  Signal = "pre_delete"
	PostDelete Signal = "post_delete"
	PreUpdate  Signal = "pre_update"
	PostUpdate Signal = "post_update"
)

Built-in signals for model lifecycle events.

Jump to

Keyboard shortcuts

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