sukazavr / fp-ts-atom

State management solution combining fp-ts, RxJS and monocle-ts

Home Page:https://sukazavr.github.io/fp-ts-atom/modules/Atom.ts.html

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

fp-ts-atom

Test npm version npm downloads


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 Atom is a hot Observable
  • 😪 Lazy initial value evaluation
  • 🏃 Deferred source subscription
  • 🔎 Compatible with Lens from monocle-ts
  • 🌍 Natively extends fp-ts ecosystem
  • 🦺 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 console

Atom API Reference

ReadonlyAtom

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 console

ReadonlyAtom API Reference

Install

Uses fp-ts, rxjs and monocle-ts as a peer dependency.

yarn add fp-ts rxjs monocle-ts fp-ts-atom

or

npm install fp-ts rxjs monocle-ts fp-ts-atom

About

State management solution combining fp-ts, RxJS and monocle-ts

https://sukazavr.github.io/fp-ts-atom/modules/Atom.ts.html

License:MIT License


Languages

Language:TypeScript 99.8%Language:JavaScript 0.2%