API Reference
StateManagerclass
Central reactive registry that bridges your model with the rs-x expression runtime. When you call watchState(context, index), StateManager wraps the value at context[index] in an observer/proxy, begins tracking mutations, and emits on changed whenever the value meaningfully changes. Watches are reference-counted — each watchState call must be paired with a corresponding releaseState call to avoid memory leaks.
When to use
The rsx() expression runtime calls watchState and releaseState automatically for every leaf node it observes, so most application code never touches StateManager directly. You need to call it yourself in two cases: (1) **computed / derived properties** — store derived values with setState and expose them via a getter using getState, so the expression runtime can observe them as if they were plain model fields; (2) **custom data type integration** — call watchState directly when wiring a custom observer/proxy stack into the reactive graph.
Import
import { StateManager } from '@rs-x/state-manager';Example
import { InjectionContainer } from '@rs-x/core';
import {
RsXStateManagerModule,
RsXStateManagerInjectionTokens,
watchIndexRecursiveRule,
type IStateChange,
type IStateManager,
} from '@rs-x/state-manager';
await InjectionContainer.load(RsXStateManagerModule);
const stateManager = InjectionContainer.get<IStateManager>(
RsXStateManagerInjectionTokens.IStateManager,
);
// ── 1. Watch a plain property ────────────────────────────────
const model = { x: { y: 10 } };
// watchState returns the current value and starts observing.
const current = stateManager.watchState(model, 'x');
console.log(current); // { y: 10 }
Members
12 members in this class.
readonly changed: Observable<IStateChange>Type
Observable<IStateChange>
readonly contextChanged: Observable<IContextChanged>Type
Observable<IContextChanged>
readonly endChangeCycle: Observable<void>Type
Observable<void>
readonly startChangeCycle: Observable<void>Type
Observable<void>
clear(): voidParameters
No parameters.
Returns
void
getState<T>(
context: unknown,
index: unknown
): TParameters
- Name
- Type
- Required
- context
- unknown
- required
- index
- unknown
- required
Returns
T
isWatched(
context: unknown,
index: unknown,
indexWatchRule?: IIndexWatchRule
): booleanParameters
- Name
- Type
- Required
- context
- unknown
- required
- index
- unknown
- required
Returns
boolean
releaseState(
context: unknown,
index: unknown,
indexWatchRule?: IIndexWatchRule
): voidParameters
- Name
- Type
- Required
- context
- unknown
- required
- index
- unknown
- required
Returns
void
setState<T>(
context: unknown,
index: unknown,
value: T,
ownerId?: unknown
): voidParameters
- Name
- Type
- Required
- context
- unknown
- required
- index
- unknown
- required
- value
- T
- required
- ownerId?
- unknown
- optional
Returns
void
subscribeStateEvents(
context: unknown,
index: unknown,
listener: IStateEventListener
): () => voidParameters
- Name
- Type
- Required
- context
- unknown
- required
- index
- unknown
- required
Returns
() => void
toString(): stringParameters
No parameters.
Returns
string
watchState(
context: unknown,
index: unknown,
options?: IStateOptions
): unknownParameters
- Name
- Type
- Required
- context
- unknown
- required
- index
- unknown
- required
Returns
unknown