state

package
v0.1.72 Latest Latest
Warning

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

Go to latest
Published: Jan 17, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MustRemember

func MustRemember[T any](c SupportState, key string, calc func() T) T

func NotifyRead added in v0.1.61

func NotifyRead(source StateChangeNotifier)

NotifyRead notifies the active read observer that a state object has been read. Source is typically the state object itself (e.g. *MutableValue).

func Remember

func Remember[T any](c SupportState, key string, calc func() T) (T, error)

func RememberUnsafe

func RememberUnsafe[T any](c SupportState, key string, calc func() T) T

func WithReadObserver added in v0.1.61

func WithReadObserver(observer ReadObserver, block func())

WithReadObserver executes the block with the given read observer. It restores the previous observer after the block finishes.

Types

type DerivedState added in v0.1.61

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

DerivedState computes a value from other state objects and caches the result. It uses push-based invalidation: when dependencies change, the derived state is marked invalid but not recalculated until Get() is called.

func DerivedStateOf added in v0.1.61

func DerivedStateOf[T any](calculation func() T) *DerivedState[T]

DerivedStateOf creates a new DerivedState with the given calculation function. Uses reflect.DeepEqual for value comparison by default.

func DerivedStateOfCustom added in v0.1.61

func DerivedStateOfCustom[T any](calculation func() T, compare func(T, T) bool) *DerivedState[T]

DerivedStateOfCustom creates a new DerivedState with a custom comparison function.

func DerivedStateWithPolicy added in v0.1.67

func DerivedStateWithPolicy[T any](calculation func() T, policy EqualityPolicy[T]) *DerivedState[T]

DerivedStateWithPolicy creates a new DerivedState with a custom EqualityPolicy.

func (*DerivedState[T]) Get added in v0.1.61

func (ds *DerivedState[T]) Get() T

Get returns the cached value, recalculating if invalid.

func (*DerivedState[T]) IsInvalid added in v0.1.67

func (ds *DerivedState[T]) IsInvalid() bool

IsInvalid returns true if the derived state needs recalculation. Primarily for testing and debugging.

func (*DerivedState[T]) Subscribe added in v0.1.67

func (ds *DerivedState[T]) Subscribe(callback func()) Subscription

Subscribe registers a callback to be invoked when this derived state's computed value actually changes (not just when it's invalidated). This is the user-facing subscription for value-change notifications.

func (*DerivedState[T]) SubscribeForInvalidation added in v0.1.67

func (ds *DerivedState[T]) SubscribeForInvalidation(callback func()) Subscription

SubscribeForInvalidation registers a callback to be invoked when this derived state is invalidated. Used internally for derived state chains. Implements InvalidationNotifier interface.

type EqualityPolicy added in v0.1.67

type EqualityPolicy[T any] interface {
	Equivalent(a, b T) bool
}

EqualityPolicy determines if two values should be considered equivalent. Used by DerivedState to skip unnecessary updates when the computed value hasn't meaningfully changed.

type InvalidationNotifier added in v0.1.67

type InvalidationNotifier interface {
	// SubscribeForInvalidation registers a callback to be invoked when this
	// state is invalidated (not when its value changes).
	SubscribeForInvalidation(callback func()) Subscription
}

InvalidationNotifier is an optional interface for derived states that support a separate subscription for invalidation events. This allows derived states to subscribe to invalidation events (for chain propagation) rather than value-change events (for user callbacks).

type Memo

type Memo = immap.ImmutableMap[any]

type MemoTyped

type MemoTyped[T any] = immap.ImmutableMap[T]

func EmptyMemo

func EmptyMemo[T any]() MemoTyped[T]

type MutableValue

type MutableValue interface {
	Value
	Set(value any)
}

func MutableValueTypedToUntyped added in v0.1.72

func MutableValueTypedToUntyped[T any](mv MutableValueTyped[T]) MutableValue

func NewMutableValue added in v0.1.71

func NewMutableValue(initial any, changeNotifier func(any), compare func(any, any) bool) MutableValue

type MutableValueTyped added in v0.1.72

type MutableValueTyped[T any] interface {
	ValueTyped[T]
	Set(value T)

	Unwrap() MutableValue
}

func MutableValueToTyped added in v0.1.72

func MutableValueToTyped[T any](mv MutableValue) (MutableValueTyped[T], error)

func NewMutableValueTyped added in v0.1.72

func NewMutableValueTyped[T any](initial T, changeNotifier func(T), compare func(T, T) bool) MutableValueTyped[T]

type MutableValueTypedWrapper added in v0.1.72

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

func (*MutableValueTypedWrapper[T]) Get added in v0.1.72

func (w *MutableValueTypedWrapper[T]) Get() T

func (*MutableValueTypedWrapper[T]) Set added in v0.1.72

func (w *MutableValueTypedWrapper[T]) Set(value T)

func (*MutableValueTypedWrapper[T]) Subscribe added in v0.1.72

func (w *MutableValueTypedWrapper[T]) Subscribe(callback func()) Subscription

func (*MutableValueTypedWrapper[T]) Unwrap added in v0.1.72

func (w *MutableValueTypedWrapper[T]) Unwrap() MutableValue

type MutableValueWrapper added in v0.1.72

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

func (*MutableValueWrapper[T]) Get added in v0.1.72

func (w *MutableValueWrapper[T]) Get() any

func (*MutableValueWrapper[T]) Set added in v0.1.72

func (w *MutableValueWrapper[T]) Set(value any)

func (*MutableValueWrapper[T]) Subscribe added in v0.1.72

func (w *MutableValueWrapper[T]) Subscribe(callback func()) Subscription

type ObserverManager added in v0.1.67

type ObserverManager interface {
	WithReadObserver(ReadObserver, func())
	NotifyRead(source StateChangeNotifier)
}

type PersistentState

type PersistentState interface {
	GetState(key string, initial func() any, options ...StateOption) MutableValue
	SetOnStateChange(callback func())
}

type ReadObserver added in v0.1.61

type ReadObserver func(source StateChangeNotifier)

type StateChangeNotifier added in v0.1.67

type StateChangeNotifier interface {
	// Subscribe registers a callback to be invoked when the state changes.
	// Returns a Subscription that can be used to stop receiving notifications.
	// The callback may be invoked from any goroutine - implementations must be thread-safe.
	Subscribe(callback func()) Subscription
}

StateChangeNotifier is implemented by state objects that can notify subscribers when their value changes. This enables push-based invalidation for derived states.

type StateOption added in v0.1.67

type StateOption func(*StateOptions)

func WithCompare added in v0.1.67

func WithCompare(compare func(any, any) bool) StateOption

type StateOptions added in v0.1.67

type StateOptions struct {
	Compare func(any, any) bool
}

type StateTypedOption added in v0.1.72

type StateTypedOption[T any] func(*StateTypedOptions[T])

func WithTypedCompare added in v0.1.72

func WithTypedCompare[T any](compare func(T, T) bool) StateTypedOption[T]

type StateTypedOptions added in v0.1.72

type StateTypedOptions[T any] struct {
	Compare func(T, T) bool
}

type Subscription added in v0.1.67

type Subscription interface {
	Unsubscribe()
}

Subscription represents an active change observation that can be cancelled. When no longer needed, call Unsubscribe() to stop receiving notifications.

func NewNoOpSubscription added in v0.1.71

func NewNoOpSubscription() Subscription

func NewSubscription added in v0.1.67

func NewSubscription(unsubscribe func()) Subscription

NewSubscription creates a Subscription with the given unsubscribe function.

type SubscriptionManager added in v0.1.67

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

SubscriptionManager manages a list of subscriber callbacks with thread-safe subscription and notification.

func NewSubscriptionManager added in v0.1.67

func NewSubscriptionManager() *SubscriptionManager

NewSubscriptionManager creates a new SubscriptionManager.

func (*SubscriptionManager) Clear added in v0.1.67

func (sm *SubscriptionManager) Clear()

Clear removes all subscribers.

func (*SubscriptionManager) Count added in v0.1.67

func (sm *SubscriptionManager) Count() int

Count returns the number of active subscribers.

func (*SubscriptionManager) NotifyAll added in v0.1.67

func (sm *SubscriptionManager) NotifyAll()

NotifyAll calls all registered subscriber callbacks. Callbacks are invoked with the read lock held to prevent concurrent modification.

func (*SubscriptionManager) Subscribe added in v0.1.67

func (sm *SubscriptionManager) Subscribe(callback func()) Subscription

Subscribe adds a callback and returns a Subscription to remove it.

type SupportState

type SupportState interface {
	Remember(key string, calc func() any) any                                  // transient state
	State(key string, initial func() any, options ...StateOption) MutableValue // persistent state
}

type TypedMutableValueInterface added in v0.1.72

type TypedMutableValueInterface[T any] = MutableValueTyped[T]

func MustState added in v0.1.72

func MustState[T any](c SupportState, key string, initial func() T, options ...StateTypedOption[T]) TypedMutableValueInterface[T]

func State added in v0.1.72

func State[T any](c SupportState, key string, initial func() T, options ...StateTypedOption[T]) (TypedMutableValueInterface[T], error)

func StateUnsafe added in v0.1.72

func StateUnsafe[T any](c SupportState, key string, initial func() T, options ...StateTypedOption[T]) TypedMutableValueInterface[T]

type Value

type Value interface {
	Get() any
	Subscribe(callback func()) Subscription
}

type ValueTyped added in v0.1.72

type ValueTyped[T any] interface {
	Get() T
	Subscribe(callback func()) Subscription
}

Jump to

Keyboard shortcuts

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