history

package
v0.1.26 Latest Latest
Warning

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

Go to latest
Published: Oct 1, 2024 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// SubjectHasError is returned when the subject has an error.
	SubjectHasError error

	// SubjectIsDone is returned when the subject is done.
	SubjectIsDone error

	// HistoryEnded is returned when the history has ended.
	HistoryEnded error
)

Functions

func Execute

func Execute[E any, S Subjecter[E]](init_fn func() S) iter.Seq[S]

Execute returns a sequence of subjects that can be reached by applying events from the history to the subjects, where the history is updated by adding events to the history, and the subjects are updated by applying events to the subjects.

The function takes a function that returns a subject as an argument. This function is called once for each subject in the sequence.

The sequence is infinite, and will continue to generate subjects until the subject is done due to exhaustion of the history, or until the subject is done due to an error.

The function will panic if an error occurs while executing the sequence.

Types

type History

type History[E any] struct {
	// contains filtered or unexported fields
}

History is a stack of events that can be replayed.

func NewHistory

func NewHistory[E any]() *History[E]

NewHistory creates a new history.

Returns:

  • *History: The new history. Never returns nil.

func (*History[E]) AddEvent

func (h *History[E]) AddEvent(event E)

AddEvent adds an event to the history.

Parameters:

  • event: The event to add to the history.

func (*History[E]) AdvanceOne

func (h *History[E]) AdvanceOne(subject Subjecter[E]) (bool, error)

AdvanceOne advances the history by one event and applies the event to the subject.

Parameters:

  • history: The history to advance.
  • subject: The subject to apply the event to.

Returns:

  • bool: True if the subject is done, false otherwise.
  • error: If the subject is nil, or if an error occurs while applying the event.

Errors:

  • SubjectHasError: If the subject is done due to an error.
  • SubjectIsDone: If the subject is done due to internal success.
  • HistoryEnded: If the history has ended.

func (*History[E]) Align

func (h *History[E]) Align(subject Subjecter[E]) error

Align applies events from the history to the subject until the subject is at the end of the history.

Parameters:

  • history: The history to apply events from. Assumed not nil.
  • subject: The subject to apply events to. Assumed not nil.

Returns:

  • error: If the subject is nil, or if an error occurs while applying events.

Errors:

  • SubjectHasError: If the subject is done due to an error.
  • SubjectIsDone: If the subject is done due to internal success.

func (History[E]) Copy

func (h History[E]) Copy() *History[E]

Copy creates a copy of the history.

Returns:

  • *History: The copy of the history. Never returns nil.

func (*History[E]) Event

func (h *History[E]) Event() iter.Seq[E]

Event returns a sequence of events in the history that have not been applied to the subject yet.

Returns:

  • iter.Seq[E]: The sequence of events.

func (*History[E]) IsNil

func (h *History[E]) IsNil() bool

IsNil checks if the history is nil.

Returns:

  • bool: True if the history is nil, false otherwise.

func (*History[E]) Restart

func (h *History[E]) Restart()

Restart resets the history without removing any events.

type Pair

type Pair[E any] struct {
	// contains filtered or unexported fields
}

Pair is a pair of a subject and a history.

func NewPair

func NewPair[E any](history *History[E], subject Subjecter[E]) (*Pair[E], error)

NewPair creates a new pair from the given history and subject.

If the given history is nil, a new one is created.

Parameters:

  • history: The history for the pair. If nil, a new one is created.
  • subject: The subject for the pair. Must not be nil.

Returns:

  • *Pair[E]: The new pair.
  • error: If the subject is nil.

func (*Pair[E]) IsNil

func (p *Pair[E]) IsNil() bool

IsNil implements the errors.Pointer interface.

type Subjecter

type Subjecter[E any] interface {
	// IsNil checks if the subject is nil.
	//
	// Returns:
	//   - bool: True if the subject is nil, false otherwise.
	IsNil() bool

	// HasError checks if the subject has an error.
	//
	// Returns:
	//   - bool: True if the subject has an error, false otherwise.
	HasError() bool

	// ApplyEvent applies an event to the subject.
	//
	// Parameters:
	//   - event: The event to apply. Assumed not nil.
	//
	// Returns:
	//   - bool: True if the subject is done, false otherwise.
	ApplyEvent(event E) bool

	// NextEvents returns the next events in the subject.
	//
	// Returns:
	//   - []E: The next events in the subject.
	NextEvents() []E
}

Subjecter is an interface for a subject.

Jump to

Keyboard shortcuts

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