mediator

package
v0.7.1 Latest Latest
Warning

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

Go to latest
Published: Sep 24, 2025 License: Apache-2.0 Imports: 7 Imported by: 5

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrMediatorClosed   = errors.New("mediator is closed")
	ErrNoHandlerMatched = errors.New("no matching event handler found")
)

Functions

func Dispatch

func Dispatch(ev Event)

func SetDefault

func SetDefault(m Mediator)

func Subscribe

func Subscribe(hdl EventHandler)

Types

type Event

type Event interface {
	Kind() EventKind
}

Event 事件接口.

type EventCollection

type EventCollection interface {
	Add(Event)
	Raise(Mediator)
	AsyncRaise(Mediator)
}

func NewEventCollection

func NewEventCollection() EventCollection

type EventHandler

type EventHandler interface {
	Listening() []EventKind
	Handle(context.Context, Event)
}

type EventKind

type EventKind string

EventKind 事件类型描述.

type InMemMediator

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

InMemMediator is a simple in-memory mediator implementation.

func (*InMemMediator) Dispatch

func (m *InMemMediator) Dispatch(ev Event) error

Dispatch dispatches an event to the mediator.

func (*InMemMediator) GracefulShutdown added in v0.6.3

func (m *InMemMediator) GracefulShutdown(ctx context.Context) error

GracefulShutdown waits for all the events to be processed and then closes the mediator.

func (*InMemMediator) Subscribe

func (m *InMemMediator) Subscribe(hdl EventHandler)

Subscribe registers an event handler to the mediator.

func (*InMemMediator) WithGenContext added in v0.5.11

func (m *InMemMediator) WithGenContext(fn func(ctx context.Context, ev Event) context.Context)

WithGenContext present a function to generate a new context for each handler.

func (*InMemMediator) WithLogger added in v0.6.1

func (m *InMemMediator) WithLogger(logger *slog.Logger)

func (*InMemMediator) WithOrphanEventHandler

func (m *InMemMediator) WithOrphanEventHandler(fn func(Event) error)

WithOrphanEventHandler present a function to handle the event when no handler is found.

func (*InMemMediator) WithTimeout added in v0.5.11

func (m *InMemMediator) WithTimeout(timeout time.Duration)

WithTimeout present a timeout for each handler.

type Mediator

type Mediator interface {
	Dispatch(Event) error
	Subscribe(EventHandler)
}

Mediator is the interface that wraps the methods of a mediator.

func Default

func Default() Mediator

func NewInMemMediator

func NewInMemMediator(opt Options) Mediator

type Options

type Options struct {
	Timeout    string `json:"timeout" yaml:"timeout" toml:"timeout"`
	Concurrent int    `json:"concurrent" yaml:"concurrent" toml:"concurrent"`
}

Options is the options for the mediator.

Jump to

Keyboard shortcuts

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