Help us shape the future of Go! Take the 2026 Go Developer Survey

cstate

package
v0.57.21 Latest Latest
Warning

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

Go to latest
Published: Oct 2, 2026 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CState

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

CState maintains an operation queue and a set of watchers. If any operation returns true for "dirty" the watchers are called.

Allows a single operation to execute at a time.

func NewCState

func NewCState[T any](obj T) *CState[T]

NewCState constructs a CState with an object.

func (*CState[T]) AddWatcher

func (c *CState[T]) AddWatcher(
	ctx context.Context,
	initial bool,
	cb func(ctx context.Context, state T),
) (func(), error)

AddWatcher adds a watcher callback function. The state will be locked while the watcher executes. Called if the state changes (dirty=true). If initial=true, calls with initial value immediately. Returns a function to remove the watcher. The ctx is used to call the watcher, if canceled, watcher is removed. Returns an error only if ctx is canceled.

func (*CState[T]) Apply

func (c *CState[T]) Apply(
	ctx context.Context,
	op func(ctx context.Context, v *CStateWriter[T]) (dirty bool, err error),
) (dirty bool, err error)

Apply queues & applies the given operation.

func (*CState[T]) Execute

func (c *CState[T]) Execute(ctx context.Context, errCh <-chan error) error

Execute executes the operation loop. errCh can be used to interrupt the Execute loop, and can be nil.

func (*CState[T]) Obj

func (c *CState[T]) Obj() T

Obj returns the controlled object.

func (*CState[T]) View

func (c *CState[T]) View(
	ctx context.Context,
	cb func(ctx context.Context, value T) error,
) (rerr error)

View calls a callback while the mutex is locked.

func (*CState[T]) Wait

func (c *CState[T]) Wait(
	ctx context.Context,
	cb func(ctx context.Context, val T) (bool, error),
) error

Wait waits for the callback to return true, nil before returning. Returns nil only if the callback returned true, nil.

func (*CState[T]) Wake

func (c *CState[T]) Wake(setDirty bool)

Wake wakes the execution loop.

type CStateWriter

type CStateWriter[T any] struct {
	*CState[T]
}

CStateWriter is passed to the CState callback with a SetObj function.

func (*CStateWriter[T]) SetObj

func (w *CStateWriter[T]) SetObj(obj T)

SetObj sets the object.

Jump to

Keyboard shortcuts

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