Documentation
¶
Index ¶
- func MustRemember[T any](c SupportState, key string, calc func() T) T
- func NotifyRead(source StateChangeNotifier)
- func Remember[T any](c SupportState, key string, calc func() T) (T, error)
- func RememberUnsafe[T any](c SupportState, key string, calc func() T) T
- func WithReadObserver(observer ReadObserver, block func())
- type DerivedState
- type EqualityPolicy
- type InvalidationNotifier
- type Memo
- type MemoTyped
- type MutableValue
- type ObserverManager
- type PersistentState
- type ReadObserver
- type StateChangeNotifier
- type StateOption
- type StateOptions
- type Subscription
- type SubscriptionManager
- type SupportState
- type TypedMutableValue
- type TypedValue
- type Value
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 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
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]
type MutableValue ¶
func NewMutableValue ¶ added in v0.1.71
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 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 TypedMutableValue ¶
type TypedMutableValue[T any] interface { TypedValue[T] Set(value T) Unwrap() MutableValue }
type TypedValue ¶
type TypedValue[T any] interface { Get() T Subscribe(callback func()) Subscription }
type Value ¶
type Value interface {
Get() any
Subscribe(callback func()) Subscription
}