@nrgyjs/rxjs Package
Package Purpose
Integration of Nrgy.js with the RxJS library for working with reactive data streams.
General Information
The @nrgyjs/rxjs package provides a bridge between Nrgy atoms and RxJS Observables. This allows you to use powerful RxJS operators for processing data stored in atoms, and vice versa — to translate data from external streams into reactive atoms.
Package Installation
bash
npm install @nrgyjs/core @nrgyjs/rxjs rxjsConceptual Architecture
The package architecture is built on two opposing transformations:
- Atom -> Observable: A reaction to atom changes generates events in an RxJS stream.
- Observable -> Atom: A subscription to an RxJS stream updates the atom's value.
Functionality Documentation
Transformations
- observe(atom, options?): Creates an
Observablethat emits the current value of the atom and all subsequent changes. - fromObservable(observable, initialValue?): Creates an
Atomthat subscribes to theobservableand updates its value.
Usage Examples
typescript
import { atom } from '@nrgyjs/core';
import { observe, fromObservable } from '@nrgyjs/rxjs';
import { filter, interval } from 'rxjs';
// 1. Atom to Observable with filtering
const count = atom(0);
const evenCount$ = observe(count).pipe(filter(v => v % 2 === 0));
// 2. Observable to Atom
const time = fromObservable(interval(1000), 0);
count.set(1); // evenCount$ will ignore
count.set(2); // evenCount$ will emit 2