Getting started

Introduction

atemno is a fine-grained reactive state library. Atoms and computeds describe values, a domain holds them, and only what something reads ever reruns.

What it is

atemno splits state into two halves.

  • Descriptions. An atom describes a value you can write. A computed describes a value derived from other values. Creating either allocates nothing and holds nothing.
  • A domain. A ReactiveDomain holds the actual values. It creates a node the first time it sees a description, tracks what every computed reads, and decides what has to rerun when something changes.
TypeScript
import { atom, computed, ReactiveDomain } from 'atemno';

const count = atom('count', 1);
const double = computed<number>('double', $ => $.get(count) * 2);

const domain = new ReactiveDomain({});

domain.get(double); // 2
domain.set(count, 5);
domain.get(double); // 10

Why split them

Because a description is cheap and state is not. The same atom holds a different value in every domain, so there is no global store to share by accident.

  • On a server, give every request its own domain. Nothing leaks between users.
  • In tests, give every test its own domain. There is nothing to reset afterwards.
  • In an app, nest domains to give a subtree isolated state.

What it does for you

  • Computeds are lazy. Writing to an atom marks what depends on it stale. A computed reruns when something reads it, or straight away if something observes it. See how updates propagate.
  • Dependencies are collected per run. A computed depends on what it read last time, so a branch it did not take is not a dependency. See dynamic dependencies.
  • Equal values stop early. A computed that returns an equal result ends the update there.
  • Unused nodes are released. A node goes away once nothing tracks it. See node lifetime, which also covers the sharpest edge in the library.
  • The core is framework agnostic. React ships as a binding. Anything else is one subscription.

Tip

The explorer draws a live domain as it runs. It is the quickest way to see what "lazy" means here.

Where to go next

Start with the quick start, then read the mental model. If you know Jotai or Recoil, the comparison covers what is different.