mediator

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Dec 31, 2021 License: MIT Imports: 4 Imported by: 0

README

mediator

Simple mediator implementation for Go

Go Report Card Coverage Go

Example


type MessageHandler struct{}

type Message struct {
    Foo string
}

type Response struct {
    Result string
}

func (h MessageHandler) Handle(ctx context.Context, msg *Message) (Response, error) {
    return Response{ msg.Foo + " bar "}, nil
}


m, err := mediator.New(mediator.WithHandler(&MessageHandler{})) 

r, err := m.Send(context.Background(), &Message{ "foo" })
// ...

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrHandlerNotFound = errors.New("message handler not found")
)

Errors

Functions

This section is empty.

Types

type Mediator

type Mediator interface {
	// Send delegates a message to the appropriate handler
	Send(ctx context.Context, msg interface{}) (res interface{}, err error)
}

Mediator is a simple implementation of the mediator pattern

func New

func New(options ...Option) (Mediator, error)

New creates a new mediator with the options set

type Option

type Option func(*mediator) error

Option is a function that modifies the mediator with certain options

func WithHandler

func WithHandler(h interface{}) Option

WithHandler adds a message handler to the mediator a handler must have an Handle method with two arguments and two return values

func WithHandlerFunc

func WithHandlerFunc(f interface{}) Option

WithHandlerFunc adds a handler func which takes two arguments, a context and a message.

Jump to

Keyboard shortcuts

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