fp-ts-atom v3.0.0
State management solution combining fp-ts, RxJS and monocle-ts
fp-ts-atom aims to befriend RxJS and monocle-ts and provide a convenient way to manage state in a reactive way.
Inspired by @grammarly/focal
Features
- โจ๏ธ Any
Atomis a hotObservable - ๐ช Lazy initial value evaluation
- ๐ Deferred source subscription
- ๐ Compatible with
Lensfrommonocle-ts - ๐ Natively extends
fp-tsecosystem - ๐ฆบ Type-safe operations
- ๐งช Covered by tests
Atom
General usage of the Atom is to store an application state. Atom has a get and set methods that allow to read and write the state. Apart from that, Atom is a hot Observable which means that you can listen to changes of the state.
Atom has fp-ts instances: Pointed, FromIO.
import { of } from 'fp-ts-atom/Atom';
const state$ = of(0);
state$.get(); // 0
state$.set(3);
state$.get(); // 3
state$.subscribe(console.log); // "3" in console
state$.set(2); // "2" in consoleReadonlyAtom
ReadonlyAtom is useful when you want to protect the state from direct changes. You can derive ReadonlyAtom from Atom using toReadonlyAtom method from Atom module or any other method from ReadonlyAtom module.
ReadonlyAtom has fp-ts instances: Pointed, FromIO, Functor, Apply, Applicative.
import { of } from 'fp-ts-atom/Atom';
import { map } from 'fp-ts-atom/ReadonlyAtom';
const state$ = of({ a: 0 });
const number$ = pipe(state$, map(s => s.a));
number$.get(); // 0
state$.set({ a: 3 });
number$.get(); // 3
number$.subscribe(console.log); // "3" in console
state$.set({ a: 2 }); // "2" in consoleInstall
Uses fp-ts, rxjs and monocle-ts as a peer dependency.
yarn add fp-ts rxjs monocle-ts fp-ts-atomor
npm install fp-ts rxjs monocle-ts fp-ts-atom