state

package
v0.2024.5 Latest Latest
Warning

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

Go to latest
Published: Jun 5, 2024 License: GPL-3.0 Imports: 3 Imported by: 0

Documentation

Overview

Package state deals with tracking, notification, and management of state and state changes.

Index

Constants

This section is empty.

Variables

View Source
var ErrTerminalState = errors.New("terminal state")

ErrTerminalState is returned from Stateful.WaitForStateChange if the currentState is a terminal state.

Functions

func IsTerminal

func IsTerminal(state any) bool

IsTerminal returns whether state implements Terminal and returns true for Terminal.IsTerminal.

Types

type Manager

type Manager[T comparable] struct {
	// contains filtered or unexported fields
}

Manager keeps track of changes to state.

func NewManager

func NewManager[T comparable](initialState T) *Manager[T]

NewManager returns a new Manager with an initial state.

func (*Manager[T]) CurrentState

func (sm *Manager[T]) CurrentState() T

CurrentState returns the current state.

func (*Manager[T]) GetNotifyChan

func (sm *Manager[T]) GetNotifyChan() <-chan struct{}

GetNotifyChan returns a chan that can be used to be notified when the state changes.

func (*Manager[T]) Update

func (sm *Manager[T]) Update(state T) (old T)

Update updates the state if the current state isn't terminal. If there's a change it notifies goroutines waiting on state change to happen.

func (*Manager[S]) WaitForStateChange

func (sm *Manager[S]) WaitForStateChange(ctx context.Context, sourceState S) error

WaitForStateChange implements Stateful.WaitForStateChange.

type Stateful

type Stateful[S comparable] interface {
	// WaitForStateChange waits until the state changes from sourceState or ctx expires.
	// An error will be returned if ctx expires or the state will never change.
	WaitForStateChange(ctx context.Context, sourceState S) error
	// CurrentState returns the current state.
	CurrentState() S
}

Stateful describes types that have a single state.

type Terminal

type Terminal interface {
	IsTerminal() bool
}

Terminal allows a state to indicate that it is a terminal state or not.

Jump to

Keyboard shortcuts

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