event

package
v0.10.3 Latest Latest
Warning

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

Go to latest
Published: Jul 17, 2026 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package event provides domain event primitives for use inside one bounded context. Concrete dispatchers may be in-memory or backed by external middleware, but the package does not expose broker-specific concepts.

The package is intentionally scoped to domain events inside one bounded context. It is not an integration message bus, broker abstraction, transactional outbox, or reliable delivery mechanism across process restarts.

Dispatch errors report only dispatcher admission or delivery failures. They do not report handler success or failure. Handlers represent follow-up transactions and own their own error policy.

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrDispatcherClosed = errors.New("domain event dispatcher is closed")

ErrDispatcherClosed reports that a dispatcher cannot accept new events because it is closing or closed.

Functions

This section is empty.

Types

type CloseInterruptedContext

type CloseInterruptedContext struct {
	Error           error
	InFlightBatchID uint64
	PendingBatches  []PendingBatch
}

CloseInterruptedContext describes accepted work that was not confirmed as handled before Close was interrupted.

type Collection

type Collection interface {
	Add(Event) bool
	Drain() []Event
	Len() int
}

Collection stores domain events raised by an aggregate until the application layer drains them after persistence succeeds.

func NewCollection

func NewCollection() Collection

NewCollection creates an empty aggregate event collection.

Example
package main

import (
	"fmt"

	"github.com/go-jimu/components/ddd/event"
)

type exampleDomainEvent struct {
	kind event.Kind
}

func (e exampleDomainEvent) Kind() event.Kind {
	return e.kind
}

func main() {
	events := event.NewCollection()

	events.Add(exampleDomainEvent{kind: "order.paid"})
	events.Add(exampleDomainEvent{kind: "order.confirmed"})

	fmt.Println("pending", events.Len())

	drained := events.Drain()
	fmt.Println(drained[0].Kind(), drained[1].Kind(), events.Len())

}
Output:
pending 2
order.paid order.confirmed 0

type Dispatcher

type Dispatcher interface {
	Dispatch(Event) error
	DispatchAll([]Event) error
	Close(context.Context) error
}

Dispatcher accepts domain event batches for handling.

type Event

type Event interface {
	Kind() Kind
}

Event is a domain fact raised inside one bounded context.

type Handler

type Handler interface {
	Listening() []Kind
	Handle(context.Context, Event)
}

Handler reacts to a domain event as a follow-up transaction.

type InMemoryDispatcher added in v0.7.4

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

func NewDispatcher

func NewDispatcher(opts ...Option) *InMemoryDispatcher

NewDispatcher creates an in-process dispatcher with one background worker.

func (*InMemoryDispatcher) Close added in v0.7.4

func (d *InMemoryDispatcher) Close(ctx context.Context) error

func (*InMemoryDispatcher) Dispatch added in v0.7.4

func (d *InMemoryDispatcher) Dispatch(event Event) error

func (*InMemoryDispatcher) DispatchAll added in v0.7.4

func (d *InMemoryDispatcher) DispatchAll(events []Event) error

func (*InMemoryDispatcher) Subscribe added in v0.7.4

func (d *InMemoryDispatcher) Subscribe(handler Handler)

type Kind

type Kind string

Kind identifies the kind of a domain event inside one bounded context.

type Option

type Option func(*InMemoryDispatcher)

Option configures a Dispatcher during construction.

func WithBufferSize

func WithBufferSize(size int) Option

WithBufferSize sets the maximum number of queued event batches.

func WithCloseInterruptedHandler

func WithCloseInterruptedHandler(fn func(CloseInterruptedContext)) Option

WithCloseInterruptedHandler sets a hook for close interruptions that leave accepted work unconfirmed.

func WithContextFactory

func WithContextFactory(fn func(context.Context, Event) context.Context) Option

WithContextFactory sets a function to derive a new context for each event dispatch.

func WithDelayClose

func WithDelayClose(delay time.Duration) Option

WithDelayClose sets the delay before the dispatcher starts rejecting new events during shutdown.

func WithHandlerTimeout

func WithHandlerTimeout(timeout time.Duration) Option

WithHandlerTimeout sets the timeout for each handler invocation.

func WithLogger

func WithLogger(logger *slog.Logger) Option

WithLogger sets the logger for dispatcher lifecycle and runtime diagnostics.

func WithPanicHandler

func WithPanicHandler(fn func(PanicContext)) Option

WithPanicHandler sets a hook for recovered handler panics.

func WithUnhandledEventHandler

func WithUnhandledEventHandler(fn func(UnhandledContext)) Option

WithUnhandledEventHandler sets a hook for events with no registered handler.

type PanicContext

type PanicContext struct {
	BatchID uint64
	Event   Event
	Panic   any
	Stack   []byte
}

PanicContext describes a recovered handler panic.

type PendingBatch

type PendingBatch struct {
	BatchID uint64
	Events  []Event
}

PendingBatch describes an accepted event batch that was not started before Close was interrupted.

type Subscriber added in v0.7.4

type Subscriber interface {
	Subscribe(Handler)
}

Subscriber registers handlers for domain events.

type UnhandledContext

type UnhandledContext struct {
	BatchID uint64
	Event   Event
}

UnhandledContext describes an event that has no registered handler.

Jump to

Keyboard shortcuts

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