statemanager

package
v0.18.0-dev Latest Latest
Warning

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

Go to latest
Published: May 27, 2025 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Manager

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

Manager is a generic wrapper around a state object T which is serialized to the storage as JSON. It provides ways to safely mutate the state, backed by file locks.

func New

func New[T any](initialState T, p string) (*Manager[T], error)

New initializes a state manager with the provided state and overwrites existing state.

func NewFromDisk

func NewFromDisk[T any](defaultState T, path string) (*Manager[T], error)

NewFromDisk initializes a state manager with the state that is exists on disk, if nothing is found on the disk it uses the provided default.

func (*Manager[T]) Commit

func (m *Manager[T]) Commit() error

Commit acquires an exclusive lock, then atomically writes the current state to the file.

func (*Manager[T]) Load

func (m *Manager[T]) Load() (*T, error)

Load acquires a shared lock, then reads and decodes the state from the file.

func (*Manager[T]) ModifyState

func (m *Manager[T]) ModifyState(cb func(*T) error) error

ModifyState acquires an exclusive lock, loads the current state. It then calls the callback function on the state to modify it before writing back to disk.

Jump to

Keyboard shortcuts

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