Table of Contents

Class Store<TState>

Namespace
Velvet
Assembly
Velvet.Docs.dll

State container: immutable state plus collocated actions in a single class, with synchronous subscribers notified on every change.

public abstract class Store<TState> : IStoreWriter<TState>

Type Parameters

TState

Immutable state record type.

Inheritance
object
Store<TState>
Implements
IStoreWriter<TState>

Remarks

Equivalent to a Zustand store for users migrating from Zustand.

Threading: a store is single-threaded. All reads, mutations (SetState(Func<TState, TState>) / Mutate(Func<TState, TState>)), and subscription changes must occur on the Unity main thread (PlayerLoop). The store performs no internal locking; it assumes a single-threaded model.

Re-entrancy: listeners passed to Subscribe(Action<TState>, bool) / Select<T>(Func<TState, T>, Action<T, T>, IEqualityComparer<T>?, bool) must not synchronously mutate the store. Notification is synchronous and not serialized, so a re-entrant mutation interleaves delivery and can reorder the (current, previous) pairs seen by other subscribers. Defer such updates (e.g. via a scheduler / next frame) instead of mutating during notification.

Constructors

Store(TState)

Retains initial as InitialState and seeds the live state with it.

Properties

CancellationToken

CancellationToken for async operations.

Current

Snapshot of the current state.

InitialState

The state value the store was constructed with. Useful for distinguishing "untouched" from "user-modified" without separately tracking a dirty flag, and for diffing against the persisted/seed value.

Logger

Logger. null suppresses logs.

Methods

Dispose()

Releases resources in the order: CTS cancel → state notifier.

Mutate(Func<TState, TState>)

Updates state unconditionally. Used for cases like Reset where notification must always fire.

OnDispose()

Subclass-specific dispose logic (overridable). The state notifier is already disposed at this point and must not be accessed. Subscriptions a subclass opened via Subscribe(Action<TState>, bool) / Select<T>(Func<TState, T>, Action<T, T>, IEqualityComparer<T>?, bool) are owned by the subclass and should be disposed here.

Reset()

Resets state to the initial value. Non-virtual template method that short-circuits when the store has been disposed; concrete stores implement ResetCore().

ResetCore()

Concrete reset logic. Called by Reset() only when the store is not disposed.

Select<T>(Func<TState, T>, Action<T, T>, IEqualityComparer<T>?, bool)

Subscribes to a selected slice with a (currentSlice, previousSlice) signature. The listener fires only when the selected slice changes under the given comparer.

SetState(Func<TState, TState>)

Updates state (equality-checked).

Subscribe(Action<TState, TState>, bool)

Subscribes to state changes with a (current, previous) signature.

Subscribe(Action<TState>, bool)

Subscribes to state changes.