state

package
v0.1.116 Latest Latest
Warning

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

Go to latest
Published: Apr 17, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

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 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]

func EmptyMemo

func EmptyMemo[T any]() MemoTyped[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

func NewMutableValue(initial any, changeNotifier func(any), compare func(any, any) bool, options ...MutableValueOption) MutableValue

type MutableValueOption added in v0.1.92

type MutableValueOption func(*MutableValueOptions)

func MutableValueWithOnForgotten added in v0.1.92

func MutableValueWithOnForgotten(onForgotten func()) MutableValueOption

type MutableValueOptions added in v0.1.92

type MutableValueOptions struct {
	OnForgotten func()
}

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 MustRemember

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

func MustState deprecated added in v0.1.72

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

Deprecated: use MustState instead

func MutableValueToTyped added in v0.1.72

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

func Remember

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

func RememberUnsafe

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

func State deprecated added in v0.1.72

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

Deprecated: use Remember instead

func StateUnsafe deprecated added in v0.1.72

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

Deprecated: use RememberUnsafe instead

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 {
	StateChangeNotifier
	lifecycle.FrameLifecycleAware
	GetState(key string, initial func() any, options ...StateOption) MutableValue
	// Deprecated: use Subscribe instead
	SetOnStateChange(callback func())
}

type ReadObserver added in v0.1.61

type ReadObserver func(source StateChangeNotifier)

type SlotStore added in v0.1.91

type SlotStore = immap.ImmutableMap[any]

type SlotStoreTyped added in v0.1.91

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

func EmptySlotStore added in v0.1.91

func EmptySlotStore[T any]() SlotStoreTyped[T]

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

func WithOnForgotten added in v0.1.92

func WithOnForgotten(onForgotten func()) StateOption

type StateOptions added in v0.1.67

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

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]

func WithTypedOnForgotten added in v0.1.92

func WithTypedOnForgotten[T any](onForgotten func()) StateTypedOption[T]

type StateTypedOptions added in v0.1.72

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

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, initial func() any, options ...StateOption) MutableValue // persistent state

	// deprecate this one to get closer to jetpack compose
	// alias for Remember
	// Deprecated: use Remember instead
	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
}

Jump to

Keyboard shortcuts

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