statemanager

package
v0.36.0 Latest Latest
Warning

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

Go to latest
Published: Jan 16, 2025 License: BSD-3-Clause Imports: 16 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetDefaultStatePath

func GetDefaultStatePath() string

GetDefaultStatePath returns the path to the state file based on the operating system It returns an empty string if the path cannot be determined.

Types

type CleanableState added in v0.34.0

type CleanableState interface {
	State
	Cleanup() error
}

CleanableState interface extends State with cleanup capability

type Manager

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

Manager handles the persistence and management of various states

func New

func New(filePath string) *Manager

New creates a new Manager instance

func (*Manager) CleanupStateByName added in v0.34.0

func (m *Manager) CleanupStateByName(name string) error

CleanupStateByName loads and cleans up a specific state by name if it implements CleanableState. Returns an error if the state doesn't exist, isn't registered, or cleanup fails.

func (*Manager) DeleteAllStates added in v0.34.0

func (m *Manager) DeleteAllStates() (int, error)

DeleteAllStates removes all states.

func (*Manager) DeleteState

func (m *Manager) DeleteState(state State) error

DeleteState removes the state from the manager and marks it as dirty for the next save. Pass an uninitialized state to delete it.

func (*Manager) DeleteStateByName added in v0.34.0

func (m *Manager) DeleteStateByName(stateName string) error

DeleteStateByName handles deletion of states without cleanup. It doesn't require the state to be registered.

func (*Manager) GetSavedStateNames added in v0.34.0

func (m *Manager) GetSavedStateNames() ([]string, error)

GetSavedStateNames returns all state names that are currently saved in the state file.

func (*Manager) GetState

func (m *Manager) GetState(state State) State

GetState returns the state for the given type

func (*Manager) LoadState added in v0.34.0

func (m *Manager) LoadState(state State) error

LoadState loads a specific state from the state file

func (*Manager) PerformCleanup

func (m *Manager) PerformCleanup() error

PerformCleanup retrieves all states from the state file and calls Cleanup on registered states that support it. Unregistered states are preserved in their original state.

func (*Manager) PersistState

func (m *Manager) PersistState(ctx context.Context) error

PersistState persists the states that have been updated since the last save.

func (*Manager) RegisterState

func (m *Manager) RegisterState(state State)

RegisterState registers a state with the manager but doesn't attempt to persist it. Pass an uninitialized state to register it.

func (*Manager) Start

func (m *Manager) Start()

Start starts the state manager periodic save routine

func (*Manager) Stop

func (m *Manager) Stop(ctx context.Context) error

func (*Manager) UpdateState

func (m *Manager) UpdateState(state State) error

UpdateState updates the state in the manager and marks it as dirty for the next save. The state will be replaced with the new one.

type RawState added in v0.34.0

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

RawState wraps raw JSON data for unregistered states

func (*RawState) MarshalJSON added in v0.34.0

func (r *RawState) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler to preserve the original JSON

func (*RawState) Name added in v0.34.0

func (r *RawState) Name() string

type State

type State interface {
	Name() string
}

State interface defines the methods that all state types must implement

Jump to

Keyboard shortcuts

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