statemachine

package
v0.427.1 Latest Latest
Warning

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

Go to latest
Published: Jan 16, 2025 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Handle

type Handle[Q any, R any, E any] interface {
	// Publish updates the state machine with a new event.
	Publish(ctx context.Context, msg E) error

	// Query retrieves the current state of the state machine.
	Query(ctx context.Context, query Q) (R, error)

	// StateIter returns an iterator of state based on a query.
	//
	// The current state is returned as the first element of the iterator,
	// followed by a stream of states for each change.
	//
	// The iterator is finished when the context is cancelled.
	StateIter(ctx context.Context, query Q) (iter.Seq[R], error)
}

Handle to update and query a state machine.

Handle abstracts away the access to a statemachine from its implementation. For example, the state machine can be running remotely, and the handle would be a client to the remote system. One statemachine can have multiple handles attached to it.

func NewLocalHandle

func NewLocalHandle[Q any, R any, E any](sm Listenable[Q, R, E]) Handle[Q, R, E]

NewLocalHandle creates a handle to a local Listenable StateMachine.

type Listenable

type Listenable[Q any, R any, E any] interface {
	StateMachine[Q, R, E]

	// Subscribe returns a channel that emits an event when the state of the state machine changes.
	Subscribe(ctx context.Context) (<-chan struct{}, error)
}

Listenable adds change notification capabilities to a StateMachine.

type Marshallable

type Marshallable encoding.BinaryMarshaler

Marshallable is a type that can be marshalled to a binary representation.

type SingleQueryHandle

type SingleQueryHandle[Q any, R any, E any] struct {
	// contains filtered or unexported fields
}

SingleQueryHandle is a handle to a state machine that only supports a single query.

This is a convenience interface to avoid repeating the same constant query.

func NewSingleQueryHandle

func NewSingleQueryHandle[Q any, R any, E any](underlying Handle[Q, R, E], query Q) *SingleQueryHandle[Q, R, E]

func (*SingleQueryHandle[Q, R, E]) Publish

func (h *SingleQueryHandle[Q, R, E]) Publish(ctx context.Context, msg E) error

Publish updates the state machine with a new event.

func (*SingleQueryHandle[Q, R, E]) StateIter

func (h *SingleQueryHandle[Q, R, E]) StateIter(ctx context.Context) (iter.Seq[R], error)

StateIter returns a stream of state machine based on a query. The current state is returned as the first element of the iterator, followed by a stream of states for each change.

The iterator is finished when the context is cancelled.

func (*SingleQueryHandle[Q, R, E]) View

func (h *SingleQueryHandle[Q, R, E]) View(ctx context.Context) (R, error)

View retrieves the current state of the state machine.

type Snapshotting

type Snapshotting[Q any, R any, E any] interface {
	StateMachine[Q, R, E]

	// Save the state of the state machine to a snapshot.
	Save(writer io.Writer) error
	// Recover the state of the state machine from a snapshot.
	Recover(reader io.Reader) error
	// Close the state machine.
	Close() error
}

Snapshotting adds snapshotting capabilities to a StateMachine.

type StateMachine

type StateMachine[Q any, R any, E any] interface {
	// Query the state of the state machine.
	Lookup(key Q) (R, error)
	// Publish an event to the state machine.
	Publish(msg E) error
}

StateMachine updates an underlying state based on events. It can be queried for the current state.

Q is the query type. R is the query response type. E is the event type.

type Unmarshallable

type Unmarshallable[T any] interface {
	*T
	encoding.BinaryUnmarshaler
}

Unmarshallable is a type that can be unmarshalled from a binary representation.

Jump to

Keyboard shortcuts

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