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-countedeach 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.

Quick facts
Kind
class
Implements
IStateManager
Members
12
Package
@rs-x/core

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
propertypublicreadonly
readonly changed: Observable<IStateChange>

Type

Observable<IStateChange>

readonly contextChanged
propertypublicreadonly
readonly contextChanged: Observable<IContextChanged>

Type

Observable<IContextChanged>

readonly endChangeCycle
propertypublicreadonly
readonly endChangeCycle: Observable<void>

Type

Observable<void>

readonly startChangeCycle
propertypublicreadonly
readonly startChangeCycle: Observable<void>

Type

Observable<void>

clear
methodpublic
clear(): void

Parameters

No parameters.

Returns

void

getState
methodpublic
getState<T>(
  context: unknown,
  index: unknown
): T

Parameters

Name
Type
Required
context
unknown
required
index
unknown
required

Returns

T

isWatched
methodpublic
isWatched(
  context: unknown,
  index: unknown,
  indexWatchRule?: IIndexWatchRule
): boolean

Parameters

Name
Type
Required
context
unknown
required
index
unknown
required
indexWatchRule?
IIndexWatchRule
optional

Returns

boolean

releaseState
methodpublic
releaseState(
  context: unknown,
  index: unknown,
  indexWatchRule?: IIndexWatchRule
): void

Parameters

Name
Type
Required
context
unknown
required
index
unknown
required
indexWatchRule?
IIndexWatchRule
optional

Returns

void

setState
methodpublic
setState<T>(
  context: unknown,
  index: unknown,
  value: T,
  ownerId?: unknown
): void

Parameters

Name
Type
Required
context
unknown
required
index
unknown
required
value
T
required
ownerId?
unknown
optional

Returns

void

subscribeStateEvents
methodpublic
subscribeStateEvents(
  context: unknown,
  index: unknown,
  listener: IStateEventListener
): () => void

Parameters

Name
Type
Required
context
unknown
required
index
unknown
required
listener
IStateEventListener
required

Returns

() => void

toString
methodpublic
toString(): string

Parameters

No parameters.

Returns

string

watchState
methodpublic
watchState(
  context: unknown,
  index: unknown,
  options?: IStateOptions
): unknown

Parameters

Name
Type
Required
context
unknown
required
index
unknown
required
options?
IStateOptions
optional

Returns

unknown