State Class

Represents a core data container in the reactive store, responsible for maintaining and broadcasting data updates to multiple subscribers. Like emitters, the state can create and manage reactive streams to broadcast changes efficiently. It is designed to simplify state propagation and synchronization across stores or features, ensuring consistency. The state can also integrate seamlessly with other reactive sources like emitters, states, and observables

@template T
The type of data managed and emitted by the state

API

class State<T> extends AbstractState<T> {
  readonly $: Observable<T>;
  initialize(): this;
  complete(): void;
  manage(...operators: OperatorFunction<T, T>[]): this;
  get(): T;
  set(value: T): this;
  update(updater: (state: T) => T): this;
  reset(): this;
  compareBy(comparison: Comparison): this;
  connect(source: DataSource<T>): this;
  useLazyEmission(): this;
  useLazyEmissionOnce(): this;
  select<I extends any[]>(...inputs: [...EmitterOrObservableTuple<I>, SpreadFn<I, T>]): this;
  zip<I extends any[]>(...inputs: [...EmitterOrObservableTuple<I>, SpreadFn<I, T>]): this;
  wait<I extends any[]>(...inputs: [...EmitterOrObservableTuple<I>, SpreadFn<I, T>]): this;
  receive(...inputs: EmitterOrObservable<T>[]): this;
  receive<I>(input: EmitterOrObservable<I>, reducer: (value: I, state: T) => T): this;
  transmit(...outputs: (EmitterOrSubject<T> | EmitterOrSubject<void>)[]): this;
  transmit<O>(output: AbstractState<O>, reducer: (value: T, state: O) => O): this;
  transmit<O>(output: EmitterOrSubject<O>, reducer: (value: T) => O): this;
  effect(...operators: OperatorFunction<any, any>[]): this;
  tap(observer: Partial<Observer<T>>): this;
  tap(next: (value: T) => void): this;
}

Example

import {state} from '@bitfiber/rx';
 
// Creates a state
const counter = state<number>(1);