@novyk/sx-state

Simple wrapper for BehaviorSubject with Immer integration.

Usage no npm install needed!

<script type="module">
  import novykSxState from 'https://cdn.skypack.dev/@novyk/sx-state';
</script>

README

Angular state management with RxJS + Immer

Simple wrapper for BehaviorSubject with Immer integration.

StateValue automatically clones and freezes all states.

Installation

npm i @novyk/sx-state immer

Usage

Create SxState

export class AppComponent {
  readonly data = new SxState<SomeValueInterface>(initialData);
}

Get value

this.data.value

Observe value

this.data.valueChanges

Set value

this.data.value = newData;

Produce new value using Immer

Works properly only with objects and arrays in the state.

More info about Immer: https://immerjs.github.io/immer/

this.data.produce(draft => {
  draft.entry = newEntry;
});

Reset value to initial

this.data.reset();

Subscribe to another Observable

someObervable.subscribe(this.data.observer);

StateValue options

  • noClone — do not clone all passed values
  • noFreeze — do not freeze all passed values
new SxState<SomeValueInterface>(initialData, {
  noClone: true,
  noFreeze: true,
});