command: github.com/txgruppi/command Index | Files

package command

import "github.com/txgruppi/command"

Index

Package Files

dispatcher.go errors.go handler.go paralleldispatcher.go serialdispatcher.go

type Dispatcher Uses

type Dispatcher interface {
    // Dispatch dispatches the given command to all known handlers.
    //
    // If no handler returns `true` for `CanHandle` call it will return a
    // `NoHandlerFoundError`.
    Dispatch(cmd interface{}) error

    // DispatchOptional works the same way as `Dispatch` but it will not return an
    // error if all handler's `CanHandle` call returns `false`.
    DispatchOptional(cmd interface{}) error

    // AppendHandlers append the given handler to the list of known handlers.
    //
    // This is useful if you want to add more handler after creating a dispatcher.
    AppendHandlers(handlers ...Handler)
}

Dispatcher defines the interface for a command dispatcher.

func NewParallelDispatcher Uses

func NewParallelDispatcher(handlers []Handler) Dispatcher

NewParallelDispatcher creates a new PrallelDispatcher with the given handlers

func NewSerialDispatcher Uses

func NewSerialDispatcher(handlers []Handler) Dispatcher

NewSerialDispatcher creates a new PrallelDispatcher with the given handlers

type Handler Uses

type Handler interface {
    // CanHandle should return `true` whenever the given command can be handled
    // by this Handler, otherwise it should return `false`
    CanHandle(cmd interface{}) bool

    // Handle does all the *work* realted to the given command.
    //
    // It will only be called if CanHandle returns `true` for the given command.
    Handle(cmd interface{}, dispatcher Dispatcher) error
}

Handler defines the interface for a command handler.

type NoHandlerFoundError Uses

type NoHandlerFoundError struct {
    Command interface{}
}

NoHandlerFoundError is returned when no handler returns `true` for the `CanHandle` call.

func (*NoHandlerFoundError) Error Uses

func (e *NoHandlerFoundError) Error() string

type ParallelDispatcher Uses

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

ParallelDispatcher is a command dispatcher wich will run all handlers in parallel and wait all handlers to finish before returning.

All errors returned by the handlers will be grouped in a `errorgroup.ErrorGroup`.

This dispatcher is *thread safe*.

func (*ParallelDispatcher) AppendHandlers Uses

func (d *ParallelDispatcher) AppendHandlers(handlers ...Handler)

AppendHandlers implements `Dispatcher.AppendHandlers`

func (*ParallelDispatcher) Dispatch Uses

func (d *ParallelDispatcher) Dispatch(cmd interface{}) (err error)

Dispatch implements `Dispatcher.Dispatch`

func (*ParallelDispatcher) DispatchOptional Uses

func (d *ParallelDispatcher) DispatchOptional(cmd interface{}) (err error)

DispatchOptional implements `Dispatcher.DispatchOptional`

type SerialDispatcher Uses

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

SerialDispatcher is a command dispatcher wich will run all handlers in parallel and wait all handlers to finish before returning.

If any handler returns an error the dispatcher will stop execution and will return that error.

This dispatcher is *thread safe*.

func (*SerialDispatcher) AppendHandlers Uses

func (d *SerialDispatcher) AppendHandlers(handlers ...Handler)

AppendHandlers implements `Dispatcher.AppendHandlers`

func (*SerialDispatcher) Dispatch Uses

func (d *SerialDispatcher) Dispatch(cmd interface{}) (err error)

Dispatch implements `Dispatcher.Dispatch`

func (*SerialDispatcher) DispatchOptional Uses

func (d *SerialDispatcher) DispatchOptional(cmd interface{}) (err error)

DispatchOptional implements `Dispatcher.DispatchOptional`

Package command imports 4 packages (graph). Updated 2017-06-09. Refresh now. Tools for package owners. This is an inactive package (no imports and no commits in at least two years).