Reactivity — From Atoms to Mastery
A source-grounded tour of Effect 4's reactive state layer — the same primitives that power TanStack-Query-style data fetching, SSR hydration, and fine-grained UI state, but built natively on Effects, Streams, Layers, scopes, and typed errors.
The reactivity APIs are tagged @stability unstable, so pin compatible package versions and re-audit before upgrading.
For a compact module-by-module API reference, use the Reactivity & Atom handbook topic. This deep dive repeats only the details needed to connect those modules into an application-level mental model.
Choose a path: start with the mental model, jump to read/write invalidation, focus on SSR hydration or React bindings, or go straight to the complete Todos feature.
Orientation & mental model
Before any API: hold two worlds in your head. You already live in the first one.
- ① Effect world — Pure descriptions:
Effect,Stream,Layer. Nothing runs until executed. You write these already. - ② Atom graph — A lazy dependency graph of reactive values. An
Atomdescribes how to produce/refresh one cell of state. - ③ Registry — The live runtime.
AtomRegistryruns atom reads, memoises values, tracks deps, runs Effects, and disposes the unused. - ④ View — Framework bindings subscribe to atoms in a registry: React hooks, Solid accessors, and Vue composables. SSR state crosses the boundary through Hydration.
Reactive state libraries you know — Jotai, Recoil, Zustand, TanStack Query — solve "derive values, cache async data, re-render on change." Effect 4's core reactivity modules put those graph and data-fetching primitives beside Effect and Stream, while satellite packages bind them to React, Solid, and Vue. Nodes can therefore inherit structured concurrency, interruption, typed errors, retries, and Layer-provided dependencies inside the state graph.
The cast of characters
| Module | What it is | Lives in |
|---|---|---|
Reactivity | Key-based pub/sub invalidation service. The wiring that says "this write affects these reads." | effect/reactivity |
Atom | A description of one reactive value — static, derived, Effect-backed, or Stream-backed. | effect/reactivity |
AtomRegistry | The runtime store that holds atom state, deps, subscriptions, and lifecycle. | effect/reactivity |
AsyncResult | Initial | Success | Failure (+ waiting) — the shape every async atom yields. | effect/reactivity |
AtomRef | Standalone synchronous observable cells (no registry). Fine-grained local mutable state. | effect/reactivity |
Hydration | dehydrate/hydrate serializable atoms for SSR. | effect/reactivity |
AtomHttpApi | Typed HttpApi clients exposed as query and mutation atoms. | effect/reactivity |
AtomRpc | Typed unary and streaming RPC clients exposed as atoms. | effect/reactivity |
AtomReact | React hooks, provider, Suspense, and hydration boundary. | @effect/atom-react |
Imports you'll use everywhere
import surface
Self-contained source · Type-checked
// These six foundational namespaces are under the core package. The same
// barrel also exports AtomHttpApi and AtomRpc integrations.
import {
Atom,
AtomRef,
AtomRegistry,
AsyncResult,
Reactivity,
Hydration,
} from "effect/reactivity"
// The React bindings ("AtomReact") are a sibling package:
import {
RegistryProvider,
useAtom,
useAtomValue,
useAtomSet,
useAtomSuspense,
HydrationBoundary,
} from "@effect/atom-react"Caution: These modules are tagged
@stability unstableand can change between releases. This guide describes the audited target above.
Takeaway: An Atom is a value, not a hook. Define atoms once at module scope. They are inert descriptions; a Registry brings them to life. This separation is what makes them testable, server-renderable, and shareable across frameworks.
Reactivity — the foundation
Start at the bottom. Reactivity is a tiny service that does one thing: connect writes to dependent reads by key. It stores no values itself — it just remembers "who cares about key K" and fires them when K is invalidated.
Everything key-reactive in this layer bottoms out here: atoms that auto-refresh on a mutation, SQL queries that refetch after an insert, and typed-client invalidation. Ordinary writable/derived atom dependencies are tracked separately by AtomRegistry's graph.
The service surface
Reactivity.ts
Self-contained source · Type-checked
import type { Effect, Queue, Scope, Stream } from "effect"
// Reactivity is a Context.Service. Its methods (paraphrased):
interface ReactivityService {
// imperative register/fire (no Effect, no scope):
registerUnsafe(keys: Keys, handler: () => void): () => void // returns unregister
invalidateUnsafe(keys: Keys): void
// effectful equivalents:
invalidate(keys: Keys): Effect.Effect<void>
mutation<A, E, R>(keys: Keys, effect: Effect.Effect<A, E, R>): Effect.Effect<A, E, R> // invalidate AFTER success
query<A, E, R>(keys: Keys, effect: Effect.Effect<A, E, R>): Effect.Effect<Queue.Dequeue<A, E>, never, R | Scope.Scope>
stream<A, E, R>(keys: Keys, effect: Effect.Effect<A, E, R>): Stream.Stream<A, E, Exclude<R, Scope.Scope>>
withBatch<A, E, R>(effect: Effect.Effect<A, E, R>): Effect.Effect<A, E, R> // coalesce invalidations
}
// Keys are either a flat array, or a record of "table -> ids":
type Keys = ReadonlyArray<unknown> | Readonly<Record<string, ReadonlyArray<unknown>>>
// ["todos"] -> coarse, whole-collection key
// { todos: [id1, id2] } -> fires the "todos" key AND "todos:id1", "todos:id2"The whole idea in one program
register + invalidate
Self-contained source · Type-checked
import { Effect } from "effect"
import { Reactivity } from "effect/reactivity"
const program = Effect.gen(function* () {
const reactivity = yield* Reactivity.Reactivity
// someone reading the "users" collection registers interest:
const unregister = reactivity.registerUnsafe(["users"], () => {
console.log("users changed — re-run my read")
})
// ...elsewhere, a write happens and announces it:
reactivity.invalidateUnsafe(["users"]) // -> logs "users changed — re-run my read"
unregister()
})
program.pipe(Effect.provide(Reactivity.layer), Effect.runFork)Mutations and queries
The effectful combinators are the ergonomic surface. mutation wraps a write so its keys are invalidated only on success. query turns a read into a live Queue that re-emits whenever its keys change. The registration is torn down automatically when the surrounding Scope closes.
mutation + query (pipeable accessors)
Fragment · Type-checked with fixture
import { Effect, Queue } from "effect"
import { Reactivity } from "effect/reactivity"
// a write that invalidates ["todos"] after it succeeds:
const addTodo = (text: string) =>
Reactivity.mutation(insertTodo(text), ["todos"])
// Effect<Todo, E, Reactivity | DbR>
// a live read keyed on ["todos"]:
const watchTodos = Effect.gen(function* () {
const queue = yield* Reactivity.query(listTodos, ["todos"])
while (true) {
const todos = yield* Queue.take(queue) // initial value, then every refresh
console.log("todos now:", todos.length)
}
}) // requires Reactivity + Scope
// run a batch of writes, fire each invalidation once at the end:
const importMany = Reactivity.Reactivity.pipe(
Effect.flatMap((r) => r.withBatch(Effect.all([addTodo("a"), addTodo("b")])))
)Source note:
Reactivitystringifies string, number, bigint, and boolean keys and hashes other values withHash.hash. The record form{ todos: [id] }expands to both the table keytodosand eachtodos:idkey. Use that expansion for mutations; register detail reads directly on`todos:${id}`when they must avoid unrelated row invalidations. Query reruns are serialized: while one run is active, repeated invalidations coalesce into at most one pending rerun. A key list that names the same key twice is safe — the handler is stored once per key and scope cleanup tolerates the repeat.
Source note:
Reactivity.Reactivityis the Context key and the service's branded interface ([Reactivity.TypeId]); there is no separateReactivity.Servicetype to import. TheReactivityServiceinterface above is a reading aid, not an export. Obtain an instance fromReactivity.layerorReactivity.makerather than writing the object by hand. The same convention applies toLanguageModel,EmbeddingModel, andChat.
Takeaway: You rarely call
Reactivitydirectly in app code. It reappears below as the engine behindAtom.withReactivityandreactivityKeys— but understanding "keys connect writes to reads, no value caching" makes the rest click.
Atom — reactive values
An Atom<A> is a description of one reactive value. Three flavours to start: a writable state cell, a derived computation, and (next chapters) Effect/Stream-backed atoms.
Atom.ts — the basics
Self-contained source · Type-checked
import { Atom } from "effect/reactivity"
// 1. A writable state cell — like useState, but defined ONCE at module scope:
const count = Atom.make(0) // Atom.Writable<number>
// 2. A derived atom. The `get` function reads other atoms and, by reading them,
// declares them as dependencies. Re-runs automatically when they change:
const double = Atom.make((get) => get(count) * 2) // Atom<number>
// 3. Derived atoms compose — depend on as many atoms as you like:
const summary = Atom.make((get) => `count=${get(count)} double=${get(double)}`)That's the entire surface for pure state: Atom.make(value) for a source of truth, Atom.make((get) => ...) for anything derived. The dependency graph is implicit — reading an atom inside a read function subscribes to it. No dependency arrays.
The get context is richer than it looks
The get passed to a read function is an AtomContext. Beyond get(atom) it can read without subscribing, await async atoms, manage its own lifecycle, and even write:
AtomContext powers
Self-contained source · Type-checked
import { Effect, Option } from "effect"
import { Atom } from "effect/reactivity"
const a = Atom.make(1)
const b = Atom.make(2)
const c = Atom.make(3)
const other = Atom.make(4)
const asyncAtom = Atom.make(Effect.succeed(5))
const optionAtom = Atom.make(Option.some(6))
const action = Atom.fn((input: number) => Effect.succeed(input))
const derived = Atom.make((get) => {
get(a) // read + subscribe (re-run when `a` changes)
get.once(b) // read WITHOUT subscribing (snapshot, no re-run)
get.self<number>() // Option of this atom's previous value
get.setSelf(123) // push a new value for THIS atom (async sources use this)
get.addFinalizer(() => {}) // cleanup when this atom is disposed/refreshed
get.mount(other) // keep `other` alive for this atom's lifetime
get.subscribe(c, (v) => {/* side-effect on change */})
get.refresh(other) // request recomputation of another atom
get.refreshSelf() // request recomputation of this atom
const awaited = get.result(asyncAtom) // Effect that awaits success
const snapshot = get.resultOnce(asyncAtom) // same, without subscribing
const some = get.some(optionAtom) // Effect that awaits Some
const someSnapshot = get.someOnce(optionAtom)
const changes = get.stream(other) // Stream of registry values
const successes = get.streamResult(asyncAtom)
const actionResult = get.setResult(action, 7) // Effect that writes and awaits success when run
void [awaited, snapshot, some, someSnapshot, changes, successes, actionResult]
get.registry // the active AtomRegistry
return get(a) + get.once(b)
})Lifecycle flags (preview)
Every atom carries lazy and keepAlive metadata, tunable with copy-combinators. We'll use them properly once the registry is in play:
combinators (copy-on-write)
Self-contained source · Type-checked
import { Atom } from "effect/reactivity"
const a = Atom.make(0).pipe(Atom.keepAlive) // never auto-disposed
const b = Atom.make(0).pipe(Atom.setIdleTTL("30 seconds")) // dispose 30s after last use
const c = Atom.make(0).pipe(Atom.withLabel("myCounter")) // debug label
const d = Atom.make((get) => get(a)).pipe(Atom.map((n) => n + 1)) // map a value
const e = Atom.make({ x: 0, y: 0 }).pipe(
Atom.withEquality<{ x: number; y: number }>(
(left, right) => left.x === right.x && left.y === right.y,
)
)Source note:
Atom.makeis heavily overloaded. With a plain value it returnsWritable<A>; with a function it returns a derivedAtom<A>; with anEffectorStreamit returnsAtom<AsyncResult<A,E>>(chapters 4–5).autoDisposeundoeskeepAlive,setLazycontrols eager rebuilding while mounted, andwithEqualityreplaces the defaultObject.iscomparison so comparator-equal values do not notify listeners or dependents.
Caution: An atom by itself does nothing —
doubleabove has never computed anything. It has no value until a registry reads it. That's next.
AtomRegistry — the runtime
The registry is where atoms come alive. It stores each atom's current value in a node, wires up the dependency graph, runs Effects, notifies subscribers, and garbage-collects atoms nobody is using.
AtomRegistry.ts — read / write / subscribe
Self-contained source · Type-checked
import { Atom, AtomRegistry } from "effect/reactivity"
const count = Atom.make(0)
const double = Atom.make((get) => get(count) * 2)
const registry = AtomRegistry.make()
registry.get(double) // 0 — computed on demand
registry.set(count, 5)
registry.get(double) // 10 — recomputed because `count` changed
// subscribe returns an unsubscribe function; immediate delivery is opt-in:
const cancel = registry.subscribe(
double,
(v) => console.log("double =", v),
{ immediate: true },
)
registry.set(count, 10) // logs "double = 20"
cancel()
// other writes:
registry.update(count, (n) => n + 1)
registry.modify(count, (n) => [`was ${n}`, n + 1]) // [returnValue, nextValue]
registry.refresh(double) // force recomputationWithout immediate, subscribe still builds the atom, so a derived or effectful atom that nothing has read yet starts computing and the listener hears its next change. get.subscribe, get.stream with withoutInitialValue, and React's useAtomSubscribe behave the same way.
Lifecycle & garbage collection
This is the part that surprises people. Atoms are lazy and auto-disposed by default. If nothing subscribes to (or mounts) an atom, the registry drops its node after an idle tick — and the next read rebuilds it from its definition.
GC behaviour (from the test suite)
Self-contained source · Type-checked
import { Atom, AtomRegistry } from "effect/reactivity"
const counter = Atom.make(0)
const r = AtomRegistry.make()
r.set(counter, 1)
r.get(counter) // 1
await new Promise((res) => setTimeout(res)) // one idle tick passes
r.get(counter) // 0 ← node was disposed, rebuilt from initial!
// Keep state across idle periods when you need persistence:
const kept = Atom.make(0).pipe(Atom.keepAlive)
// ...or hold it explicitly for a scope:
const release = r.mount(kept) // mounted => never idle-disposed
// release() laterSource note: Auto-dispose keeps memory bounded automatically: a per-route data atom evaporates when you navigate away. Reach for
keepAlive/setIdleTTL/mountonly for state that must outlive its subscribers (a session, a cache you want warm).
The Node
Each atom maps to a Node exposing value(), Set-backed parents, children, and listeners, plus currentState() → "uninitialized" | "stale" | "valid" | "removed". You rarely touch nodes directly, but Hydration walks them. Dependency tracking remains intact when a node is invalidated during a batched rebuild.
Registry as a Layer + the service accessors
The registry is also a Context.Service, so you can read/write atoms from inside ordinary Effects. Atom.get, Atom.set, Atom.refresh, Atom.getResult etc. all require the AtomRegistry service:
registry as a service
Self-contained source · Type-checked
import { Effect } from "effect"
import { Atom, AtomRegistry } from "effect/reactivity"
const count = Atom.make(0)
const program = Effect.gen(function* () {
yield* Atom.set(count, 42)
return yield* Atom.get(count) // requires AtomRegistry
})
program.pipe(
Effect.provide(AtomRegistry.layer), // a fresh registry, disposed with the scope
Effect.runPromise,
)Source note:
AtomRegistry.make({ initialValues, scheduleTask, timeoutResolution, defaultIdleTTL })tunes scheduling and GC.initialValuespre-seeds atoms — the hook React'sRegistryProviderand test setups use to inject values/layers.
Takeaway: One registry = one isolated world of state. Make one per server request (SSR), one per React tree (the provider), one per test. Atoms are shared definitions; registries are the instances.
AsyncResult — async state
The moment an atom is backed by an Effect or Stream, its value is an AsyncResult<A, E>. This is the data type that makes loading/error/stale UI states first-class.
AsyncResult.ts — the shape
Self-contained source · Type-checked
import type { AsyncResult } from "effect/reactivity"
// The exact exported union has three variants, and every one carries `waiting`:
type State<A, E = never> = AsyncResult.AsyncResult<A, E>
// AsyncResult.Initial<A, E> — never asked yet
// AsyncResult.Success<A, E> — { value, timestamp, waiting }
// AsyncResult.Failure<A, E> — { cause, previousSuccess, waiting }
// `waiting` is orthogonal to the variant: a Success can be `waiting: true`
// (you have data AND a refresh is in-flight) — that's stale-while-revalidate baked in.This two-axis design (variant × waiting) is the whole trick. A refresh doesn't throw away your data — it flips waiting to true on the existing Success, so your UI can show data and a spinner without bespoke state machines.
Constructors & guards
constructing / inspecting
Self-contained source · Type-checked
import { AsyncResult } from "effect/reactivity"
const r = AsyncResult.success<number, Error>(42)
const fallback = 0
AsyncResult.initial<number, Error>() // Initial
AsyncResult.success(42) // Success(42)
AsyncResult.fail(new Error("boom")) // Failure(typed error)
AsyncResult.waiting(AsyncResult.success(42)) // Success(42), waiting: true
AsyncResult.isInitial(r); AsyncResult.isSuccess(r); AsyncResult.isFailure(r)
AsyncResult.isWaiting(r) // true for any waiting variant
AsyncResult.value(r) // Option<A>
AsyncResult.getOrElse(r, () => fallback)
AsyncResult.map(r, (a) => a + 1) // map success channel
AsyncResult.flatMap(r, (a) => AsyncResult.success(a * 2))The is* guards above narrow. AsyncResult.isInterrupted (a failure whose cause is only interruption) returns a plain boolean and does not; check isFailure first when you need the Failure type.
Rendering: match, matchWithWaiting, and the fluent builder
Three idioms, increasing in power. match is the exhaustive 3-way switch. matchWithWaiting collapses Initial + waiting into one "loading" branch and splits failures into typed errors vs. defects — usually what a UI wants:
matchers
Self-contained source · Type-checked
import { Data } from "effect"
import { AsyncResult } from "effect/reactivity"
class LoadError extends Data.TaggedError("LoadError")<{}> {}
const result: AsyncResult.AsyncResult<ReadonlyArray<number>, LoadError> =
AsyncResult.success([])
// exhaustive 3-way
const label = AsyncResult.match(result, {
onInitial: () => "idle",
onFailure: (f) => `failed`,
onSuccess: (s) => `value: ${s.value}`,
})
// UI-shaped: loading vs typed error vs defect vs success
const ui = AsyncResult.matchWithWaiting(result, {
onWaiting: () => "Loading…",
onError: (e) => `Error: ${e.message}`, // your typed E
onDefect: (d) => `Unexpected: ${d}`, // squashed non-error cause
onSuccess: (s) => `Loaded ${s.value.length} rows`,
})The builder is a type-tracked fluent renderer: the compiler only exposes .exhaustive() once every possible case is handled, and it has tag-aware helpers like .onErrorTag:
AsyncResult.builder
Fragment · Type-checked
import { Data } from "effect"
import { AsyncResult } from "effect/reactivity"
class NotFoundError extends Data.TaggedError("NotFoundError")<{ id: string }> {}
class PermissionError extends Data.TaggedError("PermissionError")<{}> {}
declare const result: AsyncResult.AsyncResult<ReadonlyArray<number>, NotFoundError | PermissionError>
const rendered = AsyncResult.builder(result)
.onInitial(() => "—")
.onWaiting(() => "Loading…")
.onErrorTag("NotFoundError", (e) => `Missing ${e.id}`) // narrow by _tag
.onError((e) => `Error: ${e}`)
.onDefect((d) => `Crash: ${d}`)
.onSuccess((value) => `Got ${value.length}`)
.orElse(() => "unreachable") // or .exhaustive() once all cases are coveredCombining several results
When the first non-success input is a failure, AsyncResult.all rebuilds its previousSuccess in the combined tuple or record shape, using every input's current or previous value. If any input has no value, there is no combined previous success. This matters when rendering stale combined screens after one request fails: the stale value must still contain all fields, not only the failing request's value.
AsyncResult.all
Self-contained source · Type-checked
import { AsyncResult } from "effect/reactivity"
const userResult = AsyncResult.success({ id: 1 })
const settingsResult = AsyncResult.success({ theme: "dark" })
const prefsResult = AsyncResult.success({ compact: true })
// First non-success short-circuits; if all succeed you get a Success of the tuple/record,
// marked `waiting` if any input was waiting:
const combined = AsyncResult.all([userResult, settingsResult] as const)
const named = AsyncResult.all({ user: userResult, prefs: prefsResult })Takeaway:
AsyncResult≈ a typedRemoteDatawith a stale flag. You won't usually construct it by hand — atoms produce it — but masteringmatchWithWaiting/builderis what makes your render code clean.
Effectful atoms & Runtime
Now the payoff for being Effect-native. Back an atom with an Effect and it manages the fiber, scope, interruption, and result for you. Back it with a Layer via the default Atom.runtime factory and your atoms can use real services.
Effect- and Stream-backed atoms
Atom.make(effect)
Self-contained source · Type-checked
import { Data, Effect } from "effect"
import { Atom } from "effect/reactivity"
interface User { readonly id: number }
class FetchError extends Data.TaggedError("FetchError")<{ cause: unknown }> {}
const fetchUser = (url: string) => Effect.tryPromise({
try: async () => await fetch(url).then((response) => response.json()) as User,
catch: (cause) => new FetchError({ cause }),
})
const anonymousUser: User = { id: 0 }
const userId = Atom.make(1)
// value type is AsyncResult<User, E>. The registry forks the effect in a scope,
// sets Initial(waiting) -> Success | Failure, and interrupts it on dispose/refresh.
const me = Atom.make(fetchUser("/api/me"))
// Seed an initial value to skip the Initial state:
const meSeeded = Atom.make(fetchUser("/api/me"), { initialValue: anonymousUser })
// Depend on other atoms inside the effect — re-fetches when `userId` changes:
const profile = Atom.make((get) =>
fetchUser(`/api/users/${get(userId)}`)
)Source note: On
refresh, an effect atom keeps its previousSuccessvalue but setswaiting: trueuntil the new run resolves — automatic stale-while-revalidate. AStreamsource updates the atom on every emission (last value wins), failing withNoSuchElementErrorif the stream ends empty.
Atom.runtime — atoms that use services
Most real data access needs dependencies (an API client, a DB, config). Atom.runtime(layer) builds a runtime and hands you .atom, .fn, .pull, and .subscriptionRef constructors whose effects can require that layer's services. The default factory's Layer.MemoMap is itself an atom: derived runtimes share services inside one AtomRegistry, while separate registries remain isolated for SSR and tests. Use Atom.context({ memoMap }) only when you intentionally need custom or cross-registry memoization. If Layers are new, first read Services, Context & Layers.
service + runtime (mirrors the test idiom)
Self-contained source · Type-checked
import { Clock, Context, Effect, Layer } from "effect"
import { Atom } from "effect/reactivity"
interface Todo { id: number; text: string; done: boolean }
// An Effect 4 service:
class Api extends Context.Service<Api, {
readonly list: Effect.Effect<ReadonlyArray<Todo>>
readonly add: (text: string) => Effect.Effect<Todo>
readonly getById: (id: number) => Effect.Effect<Todo>
readonly toggle: (id: number) => Effect.Effect<Todo>
readonly prices: Effect.Effect<ReadonlyArray<number>>
}>()("app/Api") {}
const ApiLive = Layer.effect(
Api,
Effect.gen(function* () {
return Api.of({
list: Effect.succeed([]),
add: Effect.fn(function*(text: string) {
const id = yield* Clock.currentTimeMillis
return { id, text, done: false }
}),
getById: (id) => Effect.succeed({ id, text: `Todo ${id}`, done: false }),
toggle: (id) => Effect.succeed({ id, text: `Todo ${id}`, done: true }),
prices: Effect.succeed([]),
})
}),
)
// Build the runtime atom ONCE from the layer:
const runtime = Atom.runtime(ApiLive)
// Derive an effectful atom that needs the Api service:
const todos = runtime.atom(Api.use((api) => api.list))
// ^ Atom<AsyncResult<ReadonlyArray<Todo>, never>>The following short snippets continue to use this Api, runtime, and a registry from the same feature module; they focus on one constructor at a time.
Atom.fn — action / mutation atoms
A fn atom is a writable atom whose write value is the function argument. Writing an arg kicks off the Effect; reading the atom gives you the live AsyncResult. This is your "mutation" primitive — form submits, button actions, etc.
Atom.fn
Fragment · Type-checked with fixture
import { Effect } from "effect"
import { Atom } from "effect/reactivity"
const searchEffect = (query: string) => Effect.succeed(query)
const addTodo = runtime.fn((text: string) => Api.use((api) => api.add(text)))
// ^ AtomResultFn<string, Todo, never> (a Writable<AsyncResult<Todo>, string | Reset | Interrupt>)
// drive it from a registry (or from the React hooks covered below):
registry.set(addTodo, "Buy milk") // runs the effect; async work exposes waiting, then Success
registry.get(addTodo) // AsyncResult<Todo>
registry.set(addTodo, Atom.Reset) // back to Initial
registry.set(addTodo, Atom.Interrupt) // interrupt the in-flight fiber
// Atom.fn options: initialValue, concurrent (don't cancel previous run)
// runtime.fn additionally supports static reactivityKeys.
const search = Atom.fn((q: string) => searchEffect(q), { concurrent: true })Asynchronous executions expose a waiting state before completion. A synchronous effect can complete directly as Success without an observable waiting state.
There's also Atom.fnSync for synchronous functions (returns Option<A> before the first call, or a seeded value).
Atom.pull — streaming & pagination
Atom.pull
Fragment · Type-checked
import { Atom, AtomRegistry } from "effect/reactivity"
// A writable atom over a Stream. It pulls the first chunk, and each write pulls
// the next — accumulating items by default. Great for "load more" / infinite scroll.
declare const pagedStream: import("effect").Stream.Stream<Item>
interface Item { readonly id: number }
const feed = Atom.pull(pagedStream) // Writable<PullResult<Item>, void>
const registry = AtomRegistry.make()
registry.get(feed) // AsyncResult<{ items: NonEmptyArray<Item>; done: boolean }>
registry.set(feed, undefined) // pull the next pageThe exact success type uses a non-empty batch: { readonly done: boolean; readonly items: Array.NonEmptyArray<Item> }. The top-level constructor pulls once on first read and accumulates later chunks unless you pass { disableAccumulation: true }.
Atom.family — parameterised atoms
Atom.family
Fragment · Type-checked with fixture
import { Atom } from "effect/reactivity"
// Memoised factory: the SAME atom instance for the same arg (WeakRef-GC'd).
// Essential — never call runtime.atom(...) inline in render with a new arg each time.
const todoById = Atom.family((id: number) =>
runtime.atom(Api.use((api) => api.getById(id)))
)
registry.get(todoById(1)) // stable atom for id=1The combinator toolbox
Effect 4 ships the data-fetching niceties you'd otherwise pull in a library for. All are pipeable atom→atom transforms:
| Combinator | Purpose |
|---|---|
map / mapResult | Transform the value (or the success channel of an AsyncResult atom). |
swr({ staleTime, revalidateOnFocus, focusSignal }) | Stale-while-revalidate; focus refresh requires an explicit signal. |
debounce(duration) | Publish source changes only after they settle. |
withRefresh(duration) | Auto-refresh on an interval. |
optimistic / optimisticFn | Optimistic updates with automatic rollback on failure. |
withFallback(fallbackAtom) | Show a fallback result (marked waiting) while the primary is still Initial. If the primary is writable, writes and refreshes go to the primary. |
kvs({ runtime, key, schema, defaultValue }) | Persist an atom to a KeyValueStore (e.g. localStorage). |
searchParam(name, { schema }) | Two-way bind an atom to a URL query parameter. |
refreshOnWindowFocus | Refresh whenever the tab regains focus. |
combinators in practice
Fragment · Type-checked with fixture
import { Schema } from "effect"
import { Atom } from "effect/reactivity"
const prices = runtime.atom(Api.use((a) => a.prices)).pipe(
Atom.swr({
staleTime: "10 seconds",
revalidateOnFocus: true,
focusSignal: Atom.windowFocusSignal,
}),
Atom.withLabel("prices"),
)
const page = Atom.searchParam("page", { schema: Schema.FiniteFromString }) // Option<number>, ?page=3
const draft = Atom.make("").pipe(Atom.serializable({ key: "draft", schema: Schema.String }))Atom.windowFocusSignal and refreshOnWindowFocus are safe in non-browser environments — when window is undefined they simply never fire. Supply a custom focus signal only when you want different focus semantics, not to avoid crashes. With revalidateOnFocus: true, freshness still respects staleTime; use "always" to force a refresh on every signal.
Atom.serializable only adds hydration metadata; it does not persist to browser storage. For real local persistence, create a runtime from @effect/platform-browser's BrowserKeyValueStore.layerLocalStorage and pass it to Atom.kvs with a schema, key, and default value.
Takeaway: Think in three roles: queries (
runtime.atom(effect), read-only async state), mutations (runtime.fn(arg => effect), write-triggered), and derivations (Atom.make((get) => …)/map). Everything else is a combinator on top.
AtomRef — local mutable state
A different tool for a different job. AtomRef is a standalone, synchronous, observable cell — no registry involved. Reach for it for high-frequency, fine-grained local state: form fields, cursor positions, editor buffers.
AtomRef.ts
Self-contained source · Type-checked
import { AtomRef } from "effect/reactivity"
const form = AtomRef.make({ name: "", email: "" })
const unsub = form.subscribe((v) => console.log("form:", v))
form.update((f) => ({ ...f, name: "Ada" })) // notifies subscribers
form.set({ name: "Ada", email: "a@x.dev" })
form.value // current value, read synchronouslyProperty lenses & derived refs
The superpower is prop: a typed lens into one field that is itself a fully reactive AtomRef. Subscribers to a prop ref only fire when that field changes (equality-gated) — fine-grained updates without re-rendering the whole form.
prop lenses, map, collection
Self-contained source · Type-checked
import { AtomRef } from "effect/reactivity"
const form = AtomRef.make({ name: "", email: "" })
const name = form.prop("name") // AtomRef<string> backed by form.name
name.set("Grace") // updates form.name AND notifies both refs
name.value // "Grace"
const upper = name.map((n) => n.toUpperCase()) // ReadonlyRef<string>, derived
// reactive collections of item refs:
const todos = AtomRef.collection([{ text: "a" }, { text: "b" }])
todos.push({ text: "c" })
todos.toArray() // raw values
todos.subscribe((refs) => {/* fires on insert/remove/item change */})Source note:
setis a no-op when the new value isEqual.equalsto the current one — so derived/prop refs don't churn. Equality & hashing are based on the contained value.
Atom | AtomRef | |
|---|---|---|
| Runtime | Needs an AtomRegistry | Standalone, none |
| Model | Lazy dependency graph, async-capable | Eager synchronous cell |
| Async | Effects/Streams → AsyncResult | Synchronous values only |
| Best for | App/query/derived state, data fetching | Fast local UI state, form fields, lenses |
Takeaway: Use
Atomfor the reactive graph; drop toAtomRefwhen you want imperative, allocation-light, per-field reactivity that never touches the registry. In React they pair viauseAtomRef/useAtomRefPropValue.
Reactivity ⇄ Atom
Now we close the loop from the foundation above. The reason Reactivity exists is so a mutation can invalidate keys and any query atom watching those keys refetches — automatically, with zero manual wiring.
Two halves: withReactivity on reads, reactivityKeys on writes
auto-invalidation loop
Fragment · Type-checked with fixture
import { Atom } from "effect/reactivity"
// READ side: a query atom that refreshes whenever ["todos"] is invalidated.
const todos = runtime.atom(Api.use((a) => a.list)).pipe(
Atom.withReactivity(["todos"])
)
// WRITE side: a mutation that invalidates ["todos"] on success.
const addTodo = runtime.fn(
(text: string) => Api.use((a) => a.add(text)),
{ reactivityKeys: ["todos"] }
)
// Wire-up complete. Now:
registry.set(addTodo, "Walk dog")
// -> the effect succeeds
// -> ["todos"] is invalidated through the Reactivity service
// -> the `todos` atom refreshes itself (stale-while-revalidate)
// No refetch() call, no cache keys to juggle.Under the hood, withReactivity(keys) registers a handler that calls get.refresh(self) when the keys fire; reactivityKeys wraps the mutation's effect in Reactivity.mutation(effect, keys) so the keys invalidate after success. Both share the runtime's single Reactivity service.
Collection plus row-level invalidation
record keys
Fragment · Type-checked with fixture
import { Atom, Reactivity } from "effect/reactivity"
// A per-item query listens only to its exact row key:
const todoById = Atom.family((id: number) =>
runtime.atom(Api.use((a) => a.getById(id))).pipe(
Atom.withReactivity([`todos:${id}`])
)
)
// The record form invalidates BOTH "todos" and "todos:<id>". A list listening
// on ["todos"] and the matching detail both refresh; unrelated details do not.
const toggle = runtime.fn(
(id: number) => Reactivity.mutation(
Api.use((a) => a.toggle(id)),
{ todos: [id] },
),
)Do not register a detail query with { todos: [id] }: record keys always include the shared todos key, so every detail registered that way would refresh for every row mutation (and the matching detail handler can be reached through both expanded keys).
Caution: The runtime
.fnoptionreactivityKeysis static. For keys computed from an argument, wrap the effect withReactivity.mutationas above. Reads useAtom.withReactivity(keys)with the default factory orruntime.factory.withReactivity(keys)with a custom factory; runtime.atomhas noreactivityKeysoption.
Takeaway: This is the Effect-native answer to query invalidation: declare which keys a read depends on and which keys a write affects; the runtime does the bookkeeping. It composes with
swr,optimistic, and families.
Hydration — SSR
Server-render with real atom values, ship them in the HTML payload, and rehydrate on the client so the first paint is correct and interactive without a flash of loading. The contract is small: mark atoms serializable, then dehydrate on the server and hydrate on the client.
1. Mark atoms serializable
Atom.serializable
Self-contained source · Type-checked
import { Schema } from "effect"
import { Atom } from "effect/reactivity"
// Attach a stable key + a Schema codec. Only serializable atoms are dehydrated.
const username = Atom.make("").pipe(
Atom.serializable({ key: "username", schema: Schema.String })
)
// The schema must encode/decode the atom's VALUE type (here: string).
// For async atoms, you provide a codec for their AsyncResult value.2. Dehydrate on the server, hydrate on the client
Hydration.ts
Fragment · Type-checked with fixture
import { Hydration } from "effect/reactivity"
// --- server: after rendering into a per-request registry ---
const snapshot = Hydration.dehydrate(serverRegistry) // Initial results are ignored
// serialize `snapshot` into the HTML (e.g. window.__ATOMS__ = ...)
// --- client: preload BEFORE the atoms are first read ---
Hydration.hydrate(clientRegistry, window.__ATOMS__)
// encoded values are loaded by key via registry.setSerializable(...)Source note:
encodeInitialAs: "ignore"is the default;"value-only"emits the encodedInitialvalue."promise"attaches a liveresultPromise, so it is only suitable for a streaming transport that preserves promises—not JSON embedded in HTML.Hydration.toValuesnarrows dehydrated entries for transports that need the concrete record shape.hydrateapplies values by key and notifies existing subscribers immediately. It does not consultdehydratedAt, but it does protectAsyncResultatoms already in the registry: an incomingInitialdoes not replace a non-Initialresult, and an olderSuccess(by its timestamp) does not replace a newer one. Plain values and failures are always applied, so packet ordering otherwise remains the caller's responsibility. A value that fails schema decoding is skipped and the atom computes on the client; a seeded effect or stream atom still runs on mount unless it was markedAtom.serializable({ key, schema, skipInitialRun: true }).dehydratetolerates a bad codec: an atom whose current value fails to encode with aSchemaErroris skipped and the rest of the registry is still emitted, so one mis-declared schema costs a client refetch rather than the whole SSR payload.
For an async atom, the serialization schema must cover the whole AsyncResult, not only its success value. Construct it with AsyncResult.Schema({ success: UserSchema, error: UserErrorSchema }).
For the codec model behind this boundary, use the handbook's concise Schema topic, then Effect's release-matched comprehensive Schema guide for the long-form treatment.
withReactivity preserves the underlying initial-value target, so a preloaded serializable atom still refreshes the correct source atom.
Deterministic server reads
Some atoms must read differently on the server (no window, no Date.now() drift). Override their server-side read:
withServerValue
Self-contained source · Type-checked
import { Atom } from "effect/reactivity"
const startedAt = Atom.make(() => Date.now()).pipe(
Atom.withServerValue(() => 0) // stable on the server -> no hydration mismatch
)
// For async atoms: Atom.withServerValueInitial keeps them Initial(waiting) on the server.
// Read with Atom.getServerValue(atom, registry) when rendering server-side.Takeaway: SSR = three verbs:
serializable(opt in + give a codec),dehydrate(snapshot a registry),hydrate(preload another). The React layer wraps step 3 in a component (next).
AtomReact — React bindings
The @effect/atom-react package connects atoms to components: a registry provider, a family of hooks built on useSyncExternalStore (SSR-safe), Suspense integration for AsyncResult, and a hydration boundary. This guide demonstrates React; the concise Reactivity & Atom topic lists the Solid and Vue equivalents.
Provider + the two everyday hooks
counter.tsx
Self-contained source · Type-checked
import { RegistryProvider, useAtom, useAtomValue } from "@effect/atom-react"
import { Atom } from "effect/reactivity"
const count = Atom.make(0)
const double = Atom.make((get) => get(count) * 2)
function Counter() {
const [n, setN] = useAtom(count) // value + setter (setter takes value or updater)
const d = useAtomValue(double) // read-only subscription
return <button onClick={() => setN((x) => x + 1)}>{n} (×2 = {d})</button>
}
export function App() {
return (
<RegistryProvider> {/* one registry for this subtree */}
<Counter />
</RegistryProvider>
)
}The hook family
| Hook | Use |
|---|---|
useAtomValue(atom, selector?) | Subscribe + read. Optional selector maps before subscribing. |
useAtom(atom, { mode? }) | [value, setter] for a writable atom. |
useAtomSet(atom, { mode? }) | Setter only (no re-render on value). mode: "promise" | "promiseExit" for mutations. |
useAtomInitialValues(values) | Seed atom values in the current registry during first render. |
useAtomMount(atom) | Keep an atom alive for the component's lifetime. |
useAtomRefresh(atom) | Returns a refresh() callback. |
useAtomSuspense(atom, opts) | Read an AsyncResult atom via Suspense (throws a promise while loading). |
useAtomSubscribe(atom, f) | Run a side-effect on change without rendering the value. |
useAtomRef(ref) | Subscribe to a standalone AtomRef. |
useAtomRefProp(ref, prop) | Memoize and return a writable property ref. |
useAtomRefPropValue(ref, prop) | Subscribe directly to one property value. |
Async data with Suspense + mutations
todos.tsx
Fragment · Type-checked with fixture
import { Suspense } from "react"
import { useAtomSuspense, useAtomSet, useAtomValue } from "@effect/atom-react"
import { AsyncResult, Atom } from "effect/reactivity"
function TodoList() {
// suspends until the atom leaves Initial; throws the squashed cause/error on failure
const result = useAtomSuspense(todos) // result: AsyncResult.Success<Todo[]>
return <ul>{result.value.map((t) => <li key={t.id}>{t.text}</li>)}</ul>
}
function AddTodo() {
// "promise" mode -> setter returns a Promise that resolves on success
const add = useAtomSet(addTodo, { mode: "promise" })
return <button onClick={() => add("New task")}>Add</button>
}
// Read just a slice with a stable selector (no re-render unless it changes):
const todoCount = (r: Atom.Type<typeof todos>) =>
AsyncResult.isSuccess(r) ? r.value.length : 0
function TodoCount() {
const n = useAtomValue(todos, todoCount)
return <span>{n} todos</span>
}
function Screen() {
return (
<Suspense fallback={<p>Loading…</p>}>
<TodoList /><AddTodo /><TodoCount />
</Suspense>
)
}Subtree-scoped atoms & SSR hydration
ScopedAtom + HydrationBoundary
Fragment · Type-checked with fixture
import { HydrationBoundary, useAtomValue } from "@effect/atom-react"
import * as ScopedAtom from "@effect/atom-react/ScopedAtom"
import { Atom } from "effect/reactivity"
// Each <Provider> instance owns its OWN atom — different subtrees don't share state:
const Counter = ScopedAtom.make(() => Atom.make(0))
function View() {
const atom = Counter.use()
return <span>{useAtomValue(atom)}</span>
}
// Render with: <Counter.Provider><View /></Counter.Provider>
// SSR: feed dehydrated state into the nearest registry before children read.
function Page({ dehydrated }: { dehydrated: any }) {
return (
<HydrationBoundary state={dehydrated}>
<TodoList />
</HydrationBoundary>
)
}Source note: Hooks use
useSyncExternalStorewith a server snapshot (Atom.getServerValue).RegistryProvidercreates one registry per subtree and lets you opt intodefaultIdleTTL; the fallbackRegistryContextuses 400ms. Provider disposal waits 500ms after unmount and is canceled if it remounts. Suspense promise caches are isolated per registry.HydrationBoundaryhydrates brand-new atoms during render, but defers values for atoms already present until after commit — so a transition cannot update the current UI early.
Takeaway: Define atoms at module scope;
useAtomValue/useAtomfor sync state,useAtomSuspense+ a Suspense boundary for queries,useAtomSet({mode:"promise"})for mutations. Suspense always waits forInitial; a waiting state alone does not suspend unlesssuspendOnWaiting: true. A waitingSuccessis returned, while aFailureis returned only withincludeFailure: trueand otherwise throwsCause.squash. Pair withRegistryProvider(client) andHydrationBoundary(SSR).
Selectors are compared with the mapped atom's default Object.is equality. Keep selector functions stable (module scope or useCallback) when identity churn matters, because an inline selector produces a fresh mapped atom whenever the function identity changes.
Typed HttpApi and RPC atoms
AtomHttpApi and AtomRpc connect Effect's typed clients to the same registry, runtime, reactivity, retention, and hydration machinery. They are the high-level path when your data source already has an HttpApi or RPC contract.
Fragment · Type-checked with fixture
import { AtomHttpApi, AtomRpc } from "effect/reactivity"
// Given an HttpApi contract `Api` and an HTTP client layer `HttpClientLive`:
const Http = AtomHttpApi.Service()("app/Http", {
api: Api,
httpClient: HttpClientLive,
})
const user = Http.query("users", "getUser", {
params: { id: 1 },
reactivityKeys: { users: [1] },
timeToLive: "30 seconds",
serializationKey: "1",
})
const saveUser = Http.mutation("users", "saveUser")
// Given an RpcGroup `Rpcs` and client protocol layer `RpcProtocol`:
const RpcClient = AtomRpc.Service()("app/Rpc", {
group: Rpcs,
protocol: RpcProtocol,
})
const profile = RpcClient.query("getProfile", { id: 1 }, {
reactivityKeys: { profiles: [1] },
timeToLive: "30 seconds",
serializationKey: "1",
})
const saveProfile = RpcClient.mutation("saveProfile")Both services expose the underlying typed client and an atom .runtime. Their query options can add static reactivity keys, an idle TTL (or infinite keep-alive), and a stable hydration serialization key. Mutations accept reactivity keys with each write request. A streaming RPC query becomes a writable pull atom, so writing undefined requests the next chunk. The query builders preserve serialization and idle-retention metadata when reactivity wrapping is applied; separately, Atom.withReactivity preserves the hydration initial-value target.
What the typed atoms promise about results and errors
The generated atoms are thin: they call the typed client and hand you its result. Three rules keep rendering code honest.
| Question | AtomHttpApi | AtomRpc |
|---|---|---|
| What is the success type? | Exactly the generated client's type for that endpoint and responseMode — decoded value, [value, response] tuple, raw response, header-wrapped value, or a Stream for SSE and binary-stream endpoints. | The RPC's success type; a streaming RPC becomes a pull atom. |
| What is in the typed error? | Endpoint errors plus middleware errors (server-declared and client-side). | RPC error, RpcClientError, and both error kinds of every middleware on the RPC. |
| Where do transport and decode failures go? | For the request: defects (render with onDefect). For a returned Stream: the stream's error channel — HttpClientError, Schema.SchemaError, Sse.Retry, Sse.SseError alongside the declared stream error. | In the typed error as RpcClientError. |
| How long is an unused query kept? | Omitted timeToLive → the registry's defaultIdleTTL; 0 → no idle retention; finite → that long; infinite → kept alive. | Same. |
Two consequences for UI code. A component that reads an SSE query gets Success as soon as the stream exists; the interesting failures happen later, inside the stream, so the consumer (Stream.catch, or an atom built from that stream) must handle them. And a session-expiry middleware on an RPC shows up in the atom's error union, so an exhaustive AsyncResult.builder(...).onErrorTag(...) chain stops compiling until you add the case, which is the point.
AtomHttpApi also accepts per-call sseOptions (native SSE decode options; they are part of the query's family key) and dispatches endpoints of a topLevel group on the client root while you still address them by group identifier.
Mastery — capstone
One small Todos feature ties the central primitives together: a service-backed runtime, a query atom with auto-invalidation, URL state, a serializable shared draft atom, Suspense rendering, and a registry-level test. This is the shape of real Effect 4 reactive code.
The atoms (define once, framework-agnostic)
todos/atoms.ts
Self-contained source · Type-checked
import { Clock, Context, Effect, Layer, Schema } from "effect"
import { AsyncResult, Atom } from "effect/reactivity"
interface Todo { id: number; text: string; done: boolean }
export class TodoApi extends Context.Service<TodoApi, {
readonly list: Effect.Effect<ReadonlyArray<Todo>>
readonly add: (text: string) => Effect.Effect<Todo>
readonly toggle: (id: number) => Effect.Effect<Todo>
}>()("app/TodoApi") {}
const TodoApiLive = Layer.effect(TodoApi, Effect.gen(function* () {
// ...real HTTP client built from a Layer dependency...
return TodoApi.of({
list: Effect.succeed([]),
add: Effect.fn(function*(text: string) {
const id = yield* Clock.currentTimeMillis
return { id, text, done: false }
}),
toggle: (id: number) => Effect.succeed({ id, text: "", done: true }),
})
}))
// 1) one runtime for the whole feature (services shared within each registry)
export const runtime = Atom.runtime(TodoApiLive)
// 2) QUERY: auto-refreshes when ["todos"] is invalidated; stays fresh for 15s
export const todosAtom = runtime.atom(TodoApi.use((a) => a.list)).pipe(
Atom.withReactivity(["todos"]),
Atom.swr({ staleTime: "15 seconds" }),
Atom.withLabel("todos"),
)
// 3) MUTATIONS: invalidate ["todos"] on success -> the query refetches itself
export const addTodo = runtime.fn(
(text: string) => TodoApi.use((a) => a.add(text)),
{ reactivityKeys: ["todos"] },
)
export const toggleTodo = runtime.fn(
(id: number) => TodoApi.use((a) => a.toggle(id)),
{ reactivityKeys: ["todos"] },
)
// 4) ancillary state: filter in the URL, draft marked for SSR hydration
export const filter = Atom.searchParam("filter") // ?filter=active
export const draft = Atom.make("").pipe(Atom.serializable({ key: "draft", schema: Schema.String }))
// 5) DERIVED view model — recomputes when todos or filter change
export const visibleTodos = Atom.make((get) => {
const f = get(filter)
return AsyncResult.map(get(todosAtom), (todos) =>
f === "active" ? todos.filter((todo) => !todo.done)
: f === "done" ? todos.filter((todo) => todo.done)
: todos
)
})The draft is deliberately a registry-managed Atom: it is shared by the feature and carries hydration metadata. For a component-local form buffer that does not participate in the graph or cross an SSR boundary, use an AtomRef instead.
The UI (thin — all logic lives in atoms)
todos/TodoApp.tsx
Fragment · Type-checked with fixture
import { Suspense } from "react"
import { RegistryProvider, useAtomSuspense, useAtomSet, useAtom } from "@effect/atom-react"
import { visibleTodos, addTodo, toggleTodo, draft } from "./atoms.ts"
function List() {
const result = useAtomSuspense(visibleTodos) // suspends while Initial
const toggle = useAtomSet(toggleTodo)
return (
<ul>
{result.value.map((t) => (
<li key={t.id} onClick={() => toggle(t.id)}>{t.done ? "✓" : "○"} {t.text}</li>
))}
</ul>
)
}
function Composer() {
const [text, setText] = useAtom(draft)
const add = useAtomSet(addTodo, { mode: "promise" })
return (
<form onSubmit={(e) => { e.preventDefault(); add(text).then(() => setText("")) }}>
<input value={text} onChange={(e) => setText(e.target.value)} />
</form>
)
}
export function TodoApp() {
return (
<RegistryProvider>
<Composer />
<Suspense fallback={<p>Loading todos…</p>}><List /></Suspense>
</RegistryProvider>
)
}Testing — just a registry, no React
This uses the same test Layers and Effect-aware runner covered in Testing & Dev Tooling.
todos/atoms.test.ts
Fragment · Type-checked with fixture
import { expect, it } from "@effect/vitest"
import { Effect, Layer } from "effect"
import { AtomRegistry, Atom } from "effect/reactivity"
import { runtime, TodoApi, todosAtom } from "./atoms.ts"
const TodoApiTest = Layer.succeed(TodoApi, TodoApi.of({
list: Effect.succeed([]),
add: (text: string) => Effect.succeed({ id: 1, text, done: false }),
toggle: (id: number) => Effect.succeed({ id, text: "", done: true }),
}))
it("derives and reacts", () => {
const count = Atom.make(0)
const double = Atom.make((get) => get(count) * 2)
const r = AtomRegistry.make()
expect(r.get(double)).toBe(0)
r.set(count, 5)
expect(r.get(double)).toBe(10)
})
it("injects a test layer via initialValues", async () => {
// swap the runtime's layer for a deterministic one — no network in tests:
const r = AtomRegistry.make({
initialValues: [Atom.initialValue(runtime.layer, TodoApiTest)],
})
const todos = await Effect.runPromise(AtomRegistry.getResult(r, todosAtom))
expect(todos).toEqual([])
})Performance & correctness checklist
- Define atoms at module scope. Never call
Atom.make/runtime.atominside render with fresh args — useAtom.familyfor parameterised atoms. - Lean on auto-dispose. Default lazy + GC keeps memory bounded. Add
keepAlive/setIdleTTL/useAtomMountonly for state that must outlive its subscribers. - Select narrowly.
useAtomValue(atom, selector)subscribes to the mapped slice. Keep selectors stable, and remember their output usesObject.isunless you build an atom with custom equality. - Use
AtomReffor purely local hot state. Form keystrokes and cursor moves that do not need graph dependencies, sharing, or hydration fit its prop lenses; use anAtomwhen that state deliberately participates in those capabilities. - Batch & debounce.
Atom.batch(() => { … })coalesces synchronous writes;Atom.debouncetames noisy sources. - Prefer reactivity keys over manual refresh. Let mutations invalidate; let queries refetch. Manual
refreshis the escape hatch, not the default. - Mind error boundaries.
useAtomSuspensethrowsCause.squash(cause)on failure unless you passincludeFailure: true. - One registry per world. Per request on the server, per tree on the client, per test. Dehydrate/hydrate to bridge server→client.
Takeaway: You now have the full picture:
Reactivitywires writes to reads ·Atomdescribes reactive values ·AtomRegistryruns & GCs them ·AsyncResultmodels async state ·AtomRefhandles fine-grained local state ·Hydrationcrosses the SSR boundary ·AtomReactbinds it to the view. Same primitives, from a counter to a production data layer.