event

package
v0.0.0-...-9c50d2a Latest Latest
Warning

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

Go to latest
Published: Apr 19, 2020 License: MIT Imports: 1 Imported by: 0

Documentation

Overview

Package event deals with domain events. I really like to make every mutation of an entity as an event dispatched once the entity has been persisted. This make it really easy to design extensible and reliable systems.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Data

type Data interface{}

Data represents event payload and can be anything.

type Dispatcher

type Dispatcher interface {
	// Use register one or many handlers on this dispatcher.
	Use(...Handler)
	// Dispatch one or more events. You should always prefer the DispatchFrom
	// methods but making it easy to dispatch events could be great too.
	Dispatch(...Event)
	// DispatchFrom dequeue events from given emitters and dispatch them
	// to all listeners.
	DispatchFrom(...Emitter)
	// Close should release every resources such as a connection to a remote
	// broker or that kind of stuff.
	Close()
}

Dispatcher represents a system capable of dispatching domain events from emitters.

func NewInProcessDispatcher

func NewInProcessDispatcher() Dispatcher

NewInProcessDispatcher constructs a new simple Dispatcher which will call handlers as soon as an event is published.

type Emitter

type Emitter interface {
	// Dequeue the first event of this emitter and returns it. The caller must
	// checks the bool which represents wether or not we have reach the end.
	Dequeue() (Event, bool)
}

Emitter represents an element which can store events.

type Event

type Event struct {
	// Subject retrieve the subject on which the Data has been applied.
	Subject Subject
	// Data retrieves the event payload as raised by the domain.
	Data Data
	// EmittedAt retrieves the time at which the event has been raised.
	EmittedAt time.Time
}

Event used to represents an event in a system.

type Handler

type Handler func(Event)

Handler which will be called by a dispatcher when events are dispatched.

type Log

type Log struct {
	// Current version of this log. It is exposed to make it easier to persist
	// it to a datastore but it should never be set manually.
	Version int `json:"version"`
	// contains filtered or unexported fields
}

Log should be embedded in any types that needs to raise and apply events. It implements the Emitter interface. See the test file to see how it should be used.

func (*Log) Dequeue

func (l *Log) Dequeue() (Event, bool)

Dequeue implementation for the Log.

func (*Log) OriginVersion

func (l *Log) OriginVersion() int

OriginVersion retrieves the original version on which the current, non dispatched events, have been applied. It makes it easy to deal with optimistic locking by looking for an element at this version when updating, if no one could be found, that means the element has been modified outside this transaction so we should abort to prevent data loss.

func (*Log) StoreEvent

func (l *Log) StoreEvent(subject Subject, payload Data, emittedAt time.Time)

StoreEvent stores an event in the log with given data. It will increments the inner Version of the log and append the changes to the inner list.

type Subject

type Subject interface{}

Subject represents the subject of an event which is the entity on which an event belongs.

Jump to

Keyboard shortcuts

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