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 InvalidationNotifier
- type Memo
- type MemoTyped
- type MutableValue
- type MutableValueTyped
- func MustState[T any](c SupportState, key string, initial func() T, options ...StateTypedOption[T]) MutableValueTyped[T]
- func MutableStateOf[T any](value T) MutableValueTyped[T]
- func MutableStateWithPolicy[T any](value T, policy MutationPolicy[T]) MutableValueTyped[T]
- func MutableValueToTyped[T any](mv MutableValue) (MutableValueTyped[T], error)
- func NewMutableState[T any](initial T, opts ...MutableValueTypedOption[T]) MutableValueTyped[T]
- func State[T any](c SupportState, key string, initial func() T, options ...StateTypedOption[T]) (MutableValueTyped[T], error)
- func StateUnsafe[T any](c SupportState, key string, initial func() T, options ...StateTypedOption[T]) MutableValueTyped[T]
- type MutableValueTypedOption
- type MutableValueTypedWrapper
- func (w *MutableValueTypedWrapper[T]) CompareAndSet(expect, update T) bool
- func (w *MutableValueTypedWrapper[T]) Get() T
- func (w *MutableValueTypedWrapper[T]) GetAndUpdate(f func(T) T) T
- func (w *MutableValueTypedWrapper[T]) Set(value T)
- func (w *MutableValueTypedWrapper[T]) Subscribe(callback func()) Subscription
- func (w *MutableValueTypedWrapper[T]) Unwrap() MutableValue
- func (w *MutableValueTypedWrapper[T]) Update(f func(T) T)
- func (w *MutableValueTypedWrapper[T]) UpdateAndGet(f func(T) T) T
- type MutableValueWrapper
- func (w *MutableValueWrapper[T]) CompareAndSet(expect, update any) bool
- func (w *MutableValueWrapper[T]) Get() any
- func (w *MutableValueWrapper[T]) GetAndUpdate(f func(any) any) any
- func (w *MutableValueWrapper[T]) Set(value any)
- func (w *MutableValueWrapper[T]) Subscribe(callback func()) Subscription
- func (w *MutableValueWrapper[T]) Update(f func(any) any)
- func (w *MutableValueWrapper[T]) UpdateAndGet(f func(any) any) any
- type MutationPolicy
- type ObserverManager
- type PersistentState
- type ReadObserver
- type StateChangeNotifier
- type StateOption
- type StateOptions
- type StateTypedOption
- type StateTypedOptions
- type Subscription
- type SubscriptionManager
- type SupportState
- type Value
- type ValueTyped
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 MutationPolicy[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.
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.
Subscribe ensures the DerivedState is active (initialized and listening to dependencies).
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 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 ¶
type MutableValue interface {
Value
Set(value any)
CompareAndSet(expect, update any) bool
Update(func(any) any)
UpdateAndGet(func(any) any) any
GetAndUpdate(func(any) any) any
}
func MutableValueTypedToUntyped ¶ added in v0.1.72
func MutableValueTypedToUntyped[T any](mv MutableValueTyped[T]) MutableValue
func NewMutableValue ¶ added in v0.1.71
type MutableValueTyped ¶ added in v0.1.72
type MutableValueTyped[T any] interface { ValueTyped[T] Set(value T) CompareAndSet(expect, update T) bool Update(func(T) T) UpdateAndGet(func(T) T) T GetAndUpdate(func(T) T) T Unwrap() MutableValue }
func MustState ¶ added in v0.1.72
func MustState[T any](c SupportState, key string, initial func() T, options ...StateTypedOption[T]) MutableValueTyped[T]
func MutableStateOf ¶ added in v0.1.73
func MutableStateOf[T any](value T) MutableValueTyped[T]
MutableStateOf creates a new MutableValueTyped with the given initial value. Uses StructuralEqualityPolicy by default. This matches Kotlin's mutableStateOf function.
func MutableStateWithPolicy ¶ added in v0.1.73
func MutableStateWithPolicy[T any](value T, policy MutationPolicy[T]) MutableValueTyped[T]
MutableStateWithPolicy creates a new MutableValueTyped with a custom policy. This matches Kotlin's mutableStateOf(value, policy) overload.
func MutableValueToTyped ¶ added in v0.1.72
func MutableValueToTyped[T any](mv MutableValue) (MutableValueTyped[T], error)
func NewMutableState ¶ added in v0.1.73
func NewMutableState[T any](initial T, opts ...MutableValueTypedOption[T]) MutableValueTyped[T]
NewMutableState creates a new typed mutable state with optional configuration. This is the Kotlin-aligned API using MutationPolicy.
func State ¶ added in v0.1.72
func State[T any](c SupportState, key string, initial func() T, options ...StateTypedOption[T]) (MutableValueTyped[T], error)
func StateUnsafe ¶ added in v0.1.72
func StateUnsafe[T any](c SupportState, key string, initial func() T, options ...StateTypedOption[T]) MutableValueTyped[T]
type MutableValueTypedOption ¶ added in v0.1.73
type MutableValueTypedOption[T any] func(*mutableValueTypedConfig[T])
MutableValueTypedOption configures a mutableValueTyped
func WithChangeNotifier ¶ added in v0.1.73
func WithChangeNotifier[T any](notifier func(T)) MutableValueTypedOption[T]
WithChangeNotifier sets a callback to be invoked when the value changes.
func WithPolicy ¶ added in v0.1.73
func WithPolicy[T any](policy MutationPolicy[T]) MutableValueTypedOption[T]
WithPolicy sets a custom MutationPolicy for change detection. If not set, StructuralEqualityPolicy is used by default.
type MutableValueTypedWrapper ¶ added in v0.1.72
type MutableValueTypedWrapper[T any] struct { // contains filtered or unexported fields }
func (*MutableValueTypedWrapper[T]) CompareAndSet ¶ added in v0.1.73
func (w *MutableValueTypedWrapper[T]) CompareAndSet(expect, update T) bool
func (*MutableValueTypedWrapper[T]) Get ¶ added in v0.1.72
func (w *MutableValueTypedWrapper[T]) Get() T
func (*MutableValueTypedWrapper[T]) GetAndUpdate ¶ added in v0.1.73
func (w *MutableValueTypedWrapper[T]) GetAndUpdate(f func(T) T) 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
func (*MutableValueTypedWrapper[T]) Update ¶ added in v0.1.73
func (w *MutableValueTypedWrapper[T]) Update(f func(T) T)
func (*MutableValueTypedWrapper[T]) UpdateAndGet ¶ added in v0.1.73
func (w *MutableValueTypedWrapper[T]) UpdateAndGet(f func(T) T) T
type MutableValueWrapper ¶ added in v0.1.72
type MutableValueWrapper[T any] struct { // contains filtered or unexported fields }
func (*MutableValueWrapper[T]) CompareAndSet ¶ added in v0.1.73
func (w *MutableValueWrapper[T]) CompareAndSet(expect, update any) bool
func (*MutableValueWrapper[T]) Get ¶ added in v0.1.72
func (w *MutableValueWrapper[T]) Get() any
func (*MutableValueWrapper[T]) GetAndUpdate ¶ added in v0.1.73
func (w *MutableValueWrapper[T]) GetAndUpdate(f func(any) any) 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
func (*MutableValueWrapper[T]) Update ¶ added in v0.1.73
func (w *MutableValueWrapper[T]) Update(f func(any) any)
func (*MutableValueWrapper[T]) UpdateAndGet ¶ added in v0.1.73
func (w *MutableValueWrapper[T]) UpdateAndGet(f func(any) any) any
type MutationPolicy ¶ added in v0.1.73
type MutationPolicy[T any] interface { // Equivalent returns true if a and b should be considered the same value. // If Equivalent returns true, setting a value will not trigger notifications. Equivalent(a, b T) bool // Merge resolves conflicts between concurrent modifications. // Returns (merged, true) if the conflict was resolved, or (zero, false) if unresolvable. // Most policies return (zero, false) to indicate no merge support. Merge(previous, current, applied T) (T, bool) }
MutationPolicy controls change detection and conflict resolution. This matches Kotlin's SnapshotMutationPolicy interface from androidx.compose.runtime.
func NeverEqualPolicy ¶ added in v0.1.73
func NeverEqualPolicy[T any]() MutationPolicy[T]
NeverEqualPolicy returns a policy that always treats values as different. Setting any value will always trigger notifications, even if the value is the same.
func NewMutationPolicy ¶ added in v0.1.73
func NewMutationPolicy[T any](equivalent func(a, b T) bool, merge func(previous, current, applied T) (T, bool)) MutationPolicy[T]
NewMutationPolicy creates a MutationPolicy from an equivalence function. The merge function is optional (pass nil to disable merging).
func ReferentialEqualityPolicy ¶ added in v0.1.73
func ReferentialEqualityPolicy[T comparable]() MutationPolicy[T]
ReferentialEqualityPolicy returns a policy that compares values using Go's == operator. This is suitable for primitive types and types where identity comparison is sufficient.
func StructuralEqualityPolicy ¶ added in v0.1.73
func StructuralEqualityPolicy[T any]() MutationPolicy[T]
StructuralEqualityPolicy returns a policy that compares values using reflect.DeepEqual. This is the default policy and is suitable for complex structs and slices.
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 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 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 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 }