Reactivity & Atom
For a connected, application-oriented treatment of these modules, see Reactivity — From Atoms to Mastery.
Note: Eight core modules under
effect/reactivity, one mental model. Atom describes a reactive value (a constant, a derived computation, a writable cell, or an Effect/Stream). AtomRegistry is the store that runs atoms, caches them, and tracks the dependency graph. AsyncResult is the loading/success/failure state every effectful atom produces. AtomRef is a lightweight standalone observable cell (no registry). Reactivity is the low-level key-based invalidation engine. AtomHttpApi and AtomRpc turn a typed comp-service client into ready-made query/mutation atoms. Hydration ships server-computed atom state to the client. Then three framework bindings wrap it for components.
Atom
effect/reactivity/Atom — unstable
An Atom<A> is not a value — it's a recipe with a read function the registry runs, plus metadata for caching, laziness, and refresh. Atom.make is overloaded: pass a plain value for a writable cell, a function of get for a derived/computed atom, or an Effect/Stream for an atom whose value is an AsyncResult.
Mental model: spreadsheet cells. A writable atom is a cell you type into. A derived atom is a formula cell — it calls get(other) to read inputs, and the registry records that edge so the formula reruns only when an input changes. An effectful atom is an async formula; instead of a bare value it exposes the state of that computation.
Fragment · Type-checked with fixture
import { Effect } from "effect"
import { Atom } from "effect/reactivity"
// 1. Writable state — pass a plain value. Atom.make(x) returns Writable<x>.
// A planner-controlled proposed raise, in percent.
const proposedRaisePct = Atom.make(4)
// 2. Derived / computed — a function of `get`. Re-reads only when the raise changes.
// New salary as a multiplier of the current base (base read elsewhere).
const raiseMultiplier = Atom.make((get) => 1 + get(proposedRaisePct) / 100)
// 3. Effect-backed — value becomes AsyncResult<EquityGrant, HrisUnavailable>.
const equityGrant = Atom.make(
Effect.gen(function*() {
return yield* fetchEquityGrant("emp_1042")
})
)
// 4. Derived effectful — depends on another atom AND runs an Effect.
// Re-fetch the comp summary whenever the proposed raise changes.
const compSummary = Atom.make((get) =>
Effect.gen(function*() {
const pct = get(proposedRaisePct) // tracked dependency
return yield* computeCompSummary("emp_1042", pct)
})
)For event-driven work, use Atom.fn. It returns an AtomResultFn<Arg, A, E>: a writable atom whose value is an AsyncResult and whose write kicks off the computation with the supplied argument. Write Atom.Reset to clear it or Atom.Interrupt to cancel the in-flight fiber. Atom.fnSync is the synchronous sibling (value is an Option until first call).
Fragment · Type-checked
import { Effect } from "effect"
import { Atom } from "effect/reactivity"
interface RaiseRecommendation {
readonly employeeId: string
readonly newSalary: number
}
declare const recordRaise: (rec: RaiseRecommendation) => Effect.Effect<void>
// A "mutation"-style atom: setting a raise recommendation runs the Effect.
// The construction option allows overlapping writes; without it, a new write
// replaces the in-flight one.
const submitRaise = Atom.fn(
(rec: RaiseRecommendation) => recordRaise(rec),
{ concurrent: true }
)
// registry.set(submitRaise, rec) -> Initial -> waiting -> Success
// registry.set(submitRaise, Atom.Reset) -> back to Initial
// registry.set(submitRaise, Atom.Interrupt) -> cancel the in-flight writeWith { concurrent: true }, two overlapping calls that both resolve synchronously each still land their own Success/Failure outcome — a synchronous result is no longer lost when another concurrent call settles around the same time.
Tip:
Atom.map/Atom.mapResulttransform an atom's value (the latter maps inside theAsyncResultsuccess). Every atom usesObject.isby default;Atom.withEquality<A>(equals)installs a comparator that suppresses dependent and listener updates when successive values compare equal.Atom.keepAlivestops an atom being disposed when it has no subscribers;Atom.setIdleTTL(atom, "30 seconds")disposes it after a quiet period instead.Atom.family((employeeId) => Atom.make(...))memoizes one atom per argument (viaWeakRefwhere available); a finalizer for an evicted entry never removes a newer atom that has since been cached under the same key.Atom.withLabeltags an atom for debugging.Atom.subscriptionRefbridges aSubscriptionRefinto an atom, andAtom.pullturns aStreaminto a paginated, writable "load more" atom.
Other high-value combinators stay on the same graph: debounce(duration) delays noisy publications; withRefresh(duration) schedules a refresh; swr({ staleTime, ... }) adds stale-while-revalidate behavior, deferring a stale refresh until after the triggering read and skipping it outright if the source has since become fresh or the atom was disposed — so a one-shot unmounted read never kicks off a refresh for a source it no longer needs; withFallback(fallback) supplies an async result (marked waiting) while the primary is Initial — when the primary is writable the combined atom stays writable and forwards every write and refresh to the primary, never to the fallback; and optimistic / optimisticFn model provisional mutation state with refresh or rollback. batch(fn) coalesces synchronous writes, including writes that a batch commit listener itself queues — those are processed rather than dropped. Persistence and URL helpers include platform-neutral kvs (schema-typed KeyValueStore persistence), browser-oriented searchParam (preserves the URL hash and omits empty query strings), and refreshOnWindowFocus (safe to use on the server — no window means no focus events, so it simply never fires); on the server, withServerValue, withServerValueInitial, and getServerValue provide deterministic reads. The Reactivity deep dive shows these in a connected application.
Where services come from. An effectful atom that needs services can't conjure a Layer out of thin air. Atom.runtime(layer) builds an AtomRuntime — itself an atom holding the built Context — and gives you runtime.atom(...), runtime.fn(...), and runtime.pull(...) constructors that run Effects with that layer provided. By default its Layer.MemoMap is registry-scoped: derived atoms share built services inside one AtomRegistry, while separate SSR requests and tests build isolated instances. Use Atom.context({ memoMap }) only when you intentionally need custom or cross-registry sharing. Replacing a runtime atom's layer through registry initial values (for example to inject a test layer) keeps the Reactivity and global layers, so invalidation stays shared. When an atom is disposed, a failing scope finalizer is logged at References.UnhandledLogLevel through the runtime's logger; interruption-only cleanup is not logged.
Fragment · Type-checked with fixture
import { Effect } from "effect"
import { Atom } from "effect/reactivity"
// The built Layer is shared by live/retained runtime atoms in this registry.
// If the final consumer is disposed, a later read may acquire it again.
const runtime = Atom.runtime(CompService.layer)
// This effect can `yield* CompService` — the runtime provides it.
const roster = runtime.atom(
Effect.gen(function*() {
const comp = yield* CompService
return yield* comp.listEmployees("dept_eng")
})
)Reach for it when you have application state — local, derived, or fetched — that something needs to react to.
AtomRegistry
effect/reactivity/AtomRegistry — unstable
The store. A Registry evaluates atoms, caches their current values in Nodes, tracks parent/child dependency links, applies writes and refreshes, fans out to subscribers, and disposes unused nodes. Atoms are stateless descriptions; the registry is where state lives. The same atom can hold different values in two different registries. Most apps have exactly one (a framework provider creates it); tests and SSR spin up throwaway registries freely.
Mental model: the spreadsheet engine. Atoms are formulas; the registry is the running document with cached cell values and the recalculation graph. It also owns the scheduler that batches update work.
Self-contained source · Type-checked
import { Atom, AtomRegistry } from "effect/reactivity"
const proposedRaisePct = Atom.make(4)
const raiseMultiplier = Atom.make((get) => 1 + get(proposedRaisePct) / 100)
const registry = AtomRegistry.make()
registry.get(proposedRaisePct) // 4
registry.set(proposedRaisePct, 6)
registry.get(raiseMultiplier) // 1.06 — recomputed because the raise changed
// Subscribe imperatively; returns an unsubscribe fn.
const cancel = registry.subscribe(raiseMultiplier, (m) => console.log("multiplier =", m))
registry.set(proposedRaisePct, 8) // logs: multiplier = 1.08
cancel()
// `mount` keeps an atom alive (and running, for effectful atoms) for a lifetime.
const unmount = registry.mount(proposedRaisePct)
unmount()subscribe(atom, f) calls f on the atom's next change; pass { immediate: true } to also receive the current value at once. Either way, subscribing builds the atom (running its read function, and starting an effectful atom), so the listener hears later changes even when nothing has read the atom yet. The same holds for get.subscribe, get.stream with withoutInitialValue, and useAtomSubscribe in @effect/atom-react.
As a service. AtomRegistry is also a Context.Service with a layer. AtomRegistry.getResult waits for an AsyncResult atom to leave Initial. AtomRegistry.toStream / toStreamResult turn an atom into a Stream of its changes. make accepts initialValues (seed atoms before first read — basis of hydration), a custom scheduleTask, and a defaultIdleTTL.
The dependency graph underneath is robust against races between reads, writes, and disposal: an update reaches every current dependent through its node (no missed updates through a stale node), a dependency is released only once it is actually safe to do so, a superseded build of an atom cannot leak past the node that replaced it, and an atom whose build failed while being observed recovers on the next read instead of staying stuck.
Reach for it when you need to read or write atoms outside a component — in tests, in an Effect, at the SSR boundary — or to scope a self-contained bundle of reactive state to a subtree.
AtomRef
effect/reactivity/AtomRef — unstable
A standalone observable cell — read, set, map, subscribe — that does not go through a registry. AtomRef.make(value) gives a mutable ref; .prop("field") derives a child ref focused on one property of an object (or index of an array), and writing the child writes back through the parent immutably. AtomRef.collection(items) manages a reactive list of item refs with push/insertAt/remove.
Mental model: a featherweight observable independent of the spreadsheet. Equality-aware — a set to an equal value is a no-op and notifies nobody. No dependency graph, no async story. Use for fine-grained form state where a stable handle to one nested field should re-render only its own consumers.
Self-contained source · Type-checked
import { AtomRef } from "effect/reactivity"
// A single editable raise recommendation form.
const draft = AtomRef.make({ employeeId: "emp_1042", raisePct: 4, note: "" })
const raisePct = draft.prop("raisePct") // AtomRef<number> focused on .raisePct
raisePct.set(6) // updates draft to { ..., raisePct: 6 }
draft.value // { employeeId: "emp_1042", raisePct: 6, note: "" }
const cancel = raisePct.subscribe((p) => console.log("raise ->", p))
raisePct.update((p) => p + 1) // logs: raise -> 7
cancel()Note: Don't confuse
AtomRefwith a handle to an atom's value. It is its own cell type, used standalone. The bridge in the other direction isAtom.subscriptionRef, which lifts aSubscriptionRefinto a registry-managed atom. In React,useAtomRef(ref)subscribes a component straight to anAtomRef.
Reach for it when you want surgical, per-field observable state with minimal machinery and no need for the registry's dependency tracking.
AsyncResult
effect/reactivity/AsyncResult — unstable
The state of an asynchronous value. An AsyncResult<A, E> is one of three tags — Initial (no value yet), Success (carries value + timestamp), Failure (carries a Cause<E> on .cause, plus a previousSuccess) — and every state also carries a waiting boolean. The waiting flag lets you keep showing the last value while a refresh, retry, or revalidation is in flight instead of flashing back to a spinner.
Mental model: Exit plus "I might still be loading," designed for rendering. Every effectful atom (Atom.make(effect), Atom.fn, query atoms) produces one. You almost never construct these by hand — you match on them.
Fragment · Type-checked with fixture
import { AsyncResult } from "effect/reactivity"
declare const result: AsyncResult.AsyncResult<CompSummary, HrisUnavailable>
// Three-way match — initial vs failure vs success.
// onFailure receives the Failure, whose `.cause` is a Cause<HrisUnavailable>.
const view = AsyncResult.match(result, {
onInitial: () => "Loading comp…",
onFailure: (f) => `Error: ${f.cause}`,
onSuccess: (s) => `${s.value.employeeName}: $${s.value.proposedBase}`
})
// The UI-grade matcher: treats `waiting` (and Initial) as one "loading-ish"
// case, and splits Failure into your typed error vs an unexpected defect.
const ui = AsyncResult.matchWithWaiting(result, {
onWaiting: (r) => ({ status: "loading", stale: AsyncResult.value(r) }),
onError: (e) => ({ status: "error", error: e }), // e is HrisUnavailable
onDefect: (d) => ({ status: "crash", defect: d }),
onSuccess: (s) => ({ status: "ready", data: s.value })
})Tip:
AsyncResult.value(r)returns anOption<A>of the latest success and, while aFailureis showing, falls back to itspreviousSuccess.getOrElsesupplies a default,map/flatMaptransform the success, and guardsisSuccess/isFailure/isInitial/isWaitinglet you branch directly. BecauseFailureholds a fullCause,matchWithErrorandmatchWithWaitingare preferred over a raw three-way match when defects and typed errors should render differently.AsyncResult.Schemaencodes results across the wire — used byAtomHttpApi/AtomRpcfor hydration.AsyncResult.isInterruptedreturns a plainbooleanand does not narrow; branch withisFailurefirst. Two results are equal only when their contents are equal, so unrelatedEqualvalues that share a tag and hash no longer compare equal. When an effect atom's asynchronous run fails, the last exposed success (aninitialValueor a value published withget.setSelf) stays available as the failure'spreviousSuccess.
AsyncResult.all combines records or iterables (and plain values) into one result. The first non-success determines the result; an all-success result is waiting if any input success is waiting. When that first non-success is a failure, previousSuccess is rebuilt in the combined record or tuple shape, using each input's current or previous value. If any input has no value, the combined previous success is absent. Never render a single failed input's old value as though it were the whole combined view.
Reach for it when — any atom backed by an Effect or Stream gives you one; render its four states (loading, stale-while-revalidating, error, success) cleanly.
AtomHttpApi
effect/reactivity/AtomHttpApi — unstable
The bridge from a typed HttpApi client to ready-made atoms. AtomHttpApi.Service<Self>()(id, { api, httpClient }) builds a Context.Service that wraps the generated client and exposes two atom factories: .query(group, endpoint, request) returns a read atom of AsyncResult<Success, Error>, and .mutation(group, endpoint) returns an AtomResultFn you write to fire the call. It owns its own Atom.runtime, so the HTTP client layer is provided automatically.
Mental model: your API definition is the contract; this turns each endpoint into a reactive cell. Query atoms are deduped via Atom.family when their complete request keys are structurally equal: endpoint, params/query/payload/headers, response mode, reactivity keys, TTL, and serialization key all participate. For decoded-only queries, a serializationKey makes the decoded result hydratable and must be stable and unique for each distinct request within an endpoint, or different query atoms can share the same hydration identity. timeToLive controls idle retention, and reactivityKeys enable auto-refresh. Mutations run the endpoint and invalidate their supplied reactivity keys on success.
Self-contained source · Type-checked
import { Schema } from "effect"
import { HttpApi, HttpApiEndpoint, HttpApiGroup } from "effect/http-api"
import { FetchHttpClient } from "effect/http"
import { AtomHttpApi } from "effect/reactivity"
const CompSummary = Schema.Struct({
employeeId: Schema.Int,
baseSalary: Schema.Finite,
level: Schema.String
})
const CompApi = HttpApi.make("comp").add(
HttpApiGroup.make("comp").add(
HttpApiEndpoint.get("getCompSummary", "/employees/:id/comp", {
params: { id: Schema.FiniteFromString },
success: CompSummary
})
)
)
// One service = the client + atom factories, with its HTTP client layer baked in.
class CompClient extends AtomHttpApi.Service<CompClient>()("app/CompClient", {
api: CompApi,
httpClient: FetchHttpClient.layer,
baseUrl: "https://hr.internal/api"
}) {}
// This API declares no endpoint/middleware errors, so the typed result is
// AsyncResult<CompSummary, never>. Transport and decoding failures are defects.
const summary1042 = CompClient.query("comp", "getCompSummary", {
params: { id: 1042 },
serializationKey: "comp:1042" // makes it hydratable
})Query atoms preserve endpoint and middleware errors in the typed error channel. The client catches transport and response-decoding failures of the request itself and converts them to defects, so render those through the Cause/onDefect path. A declared write endpoint would be exposed with CompClient.mutation(group, endpoint); this read-only example deliberately does not invent one.
Result types follow the generated client. The success type of a query or mutation atom is exactly what the generated HttpApiClient method returns for that endpoint and responseMode, including the non-JSON shapes:
| Endpoint / mode | Atom success value | Where failures show up |
|---|---|---|
Ordinary endpoint, "decoded-only" (the query default) | the decoded success type | endpoint + middleware errors typed; transport/decoding as defects |
responseMode: "decoded-and-response" / "response-only" | a [decoded, HttpClientResponse] tuple, or the HttpClientResponse alone | same; "response-only" drops the endpoint's error type |
Server-sent events (HttpApiSchema.StreamSse) | a Stream of event values | in the stream's error channel: the declared stream error plus HttpClientError, Schema.SchemaError, Sse.Retry, and Sse.SseError (it was never before), so whoever consumes the stream must handle them |
Binary stream (HttpApiSchema.StreamUint8Array) | a Stream of Uint8Array | HttpClientError in the stream's error channel |
Header-wrapped success (HttpApiSchema.WithHeaders) | the value together with its decoded headers | as for an ordinary endpoint |
timeToLive: 0is an explicit opt-out. OmittingtimeToLiveleaves the query under the registry'sdefaultIdleTTL; passing0(or0n) sets a zero idle TTL, so an unmounted query is disposed as soon as it is unused and fetched again on remount. A finite duration keeps it that long; an infinite one keeps it alive.- Per-call SSE decoding options. A request may carry
sseOptions(nativeSse.DecodeOptions); they are part of the query key, so two queries that differ only insseOptionsare distinct atoms. - Mutations are isolated per service. Two
AtomHttpApi(orAtomRpc) services that share an endpoint name or RPC tag each invoke their own client with their own arguments. - Literal and union
responseModevalues infer the corresponding success types. - Top-level groups work. Endpoints of an
HttpApiGroupdeclared withtopLevel: trueare dispatched on the client root rather than under a group property; you still pass the group identifier toquery/mutation. - Only
"decoded-only"results are serializable.serializationKeyhas no effect in the other response modes, and aStreamis not a value a hydration packet can carry — do not give streaming queries a serialization key.
Reach for it when you have an HttpApi and want its endpoints as cache-aware, hydratable, auto-invalidating query/mutation atoms.
AtomRpc
effect/reactivity/AtomRpc — unstable
Same as AtomHttpApi but for an RpcGroup. AtomRpc.Service<Self>()(id, { group, protocol }) builds a service over a flattened RPC client with the same .query(tag, payload, options) and .mutation(tag) factories. If an RPC's success is an RpcSchema.Stream, its query returns a pull atom (Atom.PullResult) — write to it to pull the next batch and accumulate streamed results.
Mental model: same query/mutation/invalidation machinery as HTTP, keyed off the procedure tag + payload. reactivityKeys and idle retention (timeToLive) carry over. A non-stream query can also use a serializationKey; keep it unique for each distinct payload within its RPC tag. Streaming pull queries ignore serialization keys. Per-request headers are RPC-flavored. Supply the transport as the protocol layer; the runtime is built for you.
Fragment · Type-checked with fixture
import { Schema } from "effect"
import { Rpc, RpcGroup } from "effect/rpc"
import { AtomRpc } from "effect/reactivity"
const CompRpcs = RpcGroup.make(
Rpc.make("getVestedEquity", {
payload: { employeeId: Schema.String },
success: Schema.Struct({
employeeId: Schema.String,
vestedShares: Schema.Natural,
grantDate: Schema.String
})
})
)
class CompRpcClient extends AtomRpc.Service<CompRpcClient>()("app/CompRpcClient", {
group: CompRpcs,
protocol: ProtocolLive // your RpcClient.Protocol transport layer
}) {}
// Query atom: AsyncResult<{ employeeId, vestedShares, grantDate }, RpcClientError | ...>.
const vested = CompRpcClient.query(
"getVestedEquity",
{ employeeId: "emp_1042" },
{ serializationKey: "vested:1042" }
)
// Mutation atom, with key-based invalidation on success:
// const grant = CompRpcClient.mutation("recordEquityGrant")
// registry.set(grant, { payload: { employeeId: "emp_1042", shares: 1200 }, reactivityKeys: ["equity"] })Error and retention rules.
- The result error type is the full client-side union: the RPC's declared error,
RpcClientError, and both the declared error and the client-side error of every middleware on that RPC. A streaming query'sPullResulterror also includes the stream's own error type. Matching exhaustively on_taghas to account for middleware failures such as an expired-session error. - RPCs whose middleware declares
requiresare queryable.queryinfers the same types asmutationfor them rather than resolving tonever. - Tag-selected flat clients infer correctly.
AtomRpc.queryandAtomRpc.mutationbuilt from a flat (union-of-tag) RPC client infer the selected RPC's actual payload and result types instead ofnever. - The atom types have names.
AtomRpc.Query<R>andAtomRpc.Mutation<R>are the typesqueryandmutationreturn for an RPCR(they distribute over a union), so an exported atom can be annotated asAtomRpc.Query<Rpc.ExtractTag<RpcGroup.Rpcs<typeof CompRpcs>, "getVestedEquity">>. Selecting an RPC by tag no longer tests every RPC of the group, which keeps type-checking fast for large groups. timeToLive: 0opts out of idle retention, exactly as inAtomHttpApi; omit the option to inherit the registry default.- Stream-backed mutations invalidate on success only. Reactivity keys of an atom
Streammutation are invalidated after successful completion, not on failure, defect, interruption, or replacement by a newer write. - Mutations cannot target streaming RPCs (the type is
never); usequeryand pull.
Reach for it when your backend speaks Effect RPC and you want the frontend to consume it as atoms — including streaming procedures surfaced as incremental pull atoms.
Hydration
effect/reactivity/Hydration — unstable
Server-to-client state transfer for atoms. Hydration.dehydrate(registry) walks a registry and encodes every atom marked Atom.serializable; decoded-only HttpApi queries and non-stream RPC queries become serializable when given a serializationKey. Entries are keyed by that serialization key. With the default encodeInitialAs: "ignore", or with "value-only", the array is JSON-compatible; embed it only through a framework-safe escaped serialization channel, never raw string interpolation into a <script>. Hydration.hydrate(registry, state) preloads encoded values into another registry before atoms are first read, so the client renders without an initial refetch.
Mental model: freeze-dry the relevant cells on the server, ship the value packet in your HTML, and reconstitute it into the client registry. encodeInitialAs: "promise" instead attaches a live resultPromise; it is only for a streaming transport that preserves promises and must not be JSON-stringified or embedded as ordinary data.
Fragment · Type-checked with fixture
import { Hydration } from "effect/reactivity"
// On the server, after rendering with a registry that ran your comp query atoms:
const packet = Hydration.dehydrate(serverRegistry)
// JSON-serialize `packet` into the page…
// On the client, before first render:
Hydration.hydrate(clientRegistry, packet)- Seeded atoms still run by default. A hydrated (or
Atom.initialValue-seeded) effect or stream atom builds on first read or mount and runs its effect, because the dependencies it reads are only known once it has run. PassskipInitialRun: truetoAtom.serializable({ key, schema, skipInitialRun })orAtom.initialValue(atom, value, { skipInitialRun: true })to keep the seeded value until the atom is refreshed or a dependency changes;get(...)calls in the factory andwithReactivitykeys are still tracked, and anInitialseed never skips the run. - Newer client results are kept. For an atom already in the registry,
hydratedoes not overwrite a non-InitialAsyncResultwith anInitialone, or aSuccesswith an olderSuccess(compared by the results' timestamps). Plain values and failures are always applied, and the packet'sdehydratedAtis not consulted. Existing subscribers are notified immediately. - Decode failures fall back to client computation. If hydration data fails schema decoding, or a streamed initial result cannot be encoded, the atom computes on the client instead of throwing or waiting forever.
Atom.Serializableencodes to and decodes fromSchema.Json, matching the JSON codecAtom.serializableuses at runtime. Code that reads the encoded value directly may need to narrow it.
One bad atom does not abort the packet. If a serializable atom's current value cannot be encoded by its schema (a SchemaError), dehydrate skips that atom and encodes the rest; any other exception still propagates. The skipped atom simply refetches on the client, so treat a missing key as "not hydrated", not as an error — and log encode failures in development, because the usual cause is a schema that does not cover the atom's whole value.
For custom serializable atoms, the codec covers the atom's complete value. In particular, an effectful atom needs an AsyncResult.Schema(...), not just its success schema; the release-matched comprehensive upstream Schema guide covers codecs and serialization in depth.
Reach for it when you do SSR or static rendering and want fetched atom state to survive the trip to the browser instead of refetching on mount.
Reactivity
effect/reactivity/Reactivity — unstable
The low-level invalidation engine that reactivity keys ride on. Reactivity is a Context.Service that maps arbitrary keys to handlers: invalidate(keys) runs every handler registered for those keys, mutation(keys, effect) wraps an effect so a successful run invalidates them, and query(keys, effect) / stream(keys, effect) expose an effect as a queue/stream that reruns whenever its keys are invalidated. It caches nothing — it is pure pub/sub for "this data changed."
Mental model: the wiring under the magic. When an AtomHttpApi mutation lists reactivityKeys: ["comp"] and a query atom was built with matching keys, this service connects the write to the refetch. You rarely call it directly, but it explains how cache invalidation propagates and lets you wire key-based invalidation into non-atom Effects.
Fragment · Type-checked with fixture
import { Effect } from "effect"
import { Reactivity } from "effect/reactivity"
const recordRaise = (rec: RaiseRecommendation) =>
Effect.gen(function*() {
const reactivity = yield* Reactivity.Reactivity
// run the write, and on success invalidate everything keyed "comp".
return yield* reactivity.mutation(["comp"], db.insertRaise(rec))
})Reactivity.Reactivityis both the Context key and the branded service type. There is noReactivity.Servicealias; annotate a dependency asReactivity.Reactivity, and build a custom instance withReactivity.make(it adds the[Reactivity.TypeId]brand) rather than an object literal.- Keys may repeat. A
queryorstreamwhose key list names the same key twice (or a record form that expands to a key you also listed) stores its handler once per key, and its scope cleanup does not fail on the repeat. - Reruns are serialized and coalesced. While one rerun is in flight, further invalidations collapse into a single pending rerun, and a query matched by several invalidated keys reruns once per invalidation or batch flush.
- A failed query stops rerunning. When a
query(orSqlClient.reactiveMailbox) run fails, later invalidations do not rerun it and pending reruns are discarded. - Invalidations inside a SQL transaction wait for the outermost commit and are dropped on rollback.
- Keys can be typed. Augment
Reactivity.Keys(declare module "effect/reactivity/Reactivity" { interface Keys { readonly employee: number } }) and the public reactivity APIs —Reactivity.mutation/query/invalidate,Atomreactivity options,AtomHttpApi,AtomRpc,SqlClient.reactive, andEventLog.groupReactivity— reject undeclared key names and mismatched ids. With no keys declared, any keys are accepted. Methods on theReactivityservice instance still accept any key, for keys known only at runtime.
Reach for it when you need key-based invalidation outside the atom layer (e.g. a SQL repo), or to understand what reactivityKeys are doing.
Framework bindings
The core above is framework-agnostic. Each binding (1) provides an AtomRegistry through the framework's context and (2) exposes hooks/primitives that subscribe a component to that registry.
pkg @effect/atom-react — React hooks +
RegistryProvider. Peer range:react >=19.0.0 <20.0.0andscheduler >=0.25.0 <0.29.0(relaxed fromreact >=19.2.7, so any React 19 works).useAtomValue(atom)reads,useAtom(writable)returns[value, set],useAtomSet/useAtomRefresh/useAtomMountgive write/refresh/keep-alive without subscribing, anduseAtomSuspensereads anAsyncResultatom through React Suspense.HydrationBoundaryapplies dehydrated state;ScopedAtom.makemakes a subtree-local atom.RegistryProvider'sinitialValuesaccepts[atom, value, { skipInitialRun }]tuples. Atom writes triggered while React renders (for example an atom whose initializer sets another atom) are deferred, so other components do not update during render.pkg @effect/atom-solid — The Solid binding.
useAtomValue(() => atom)returns an accessor,useAtom(() => writable)returns an accessor/setter tuple, anduseAtomResourcebridges anAsyncResultatom to a Solid resource. ItsRegistryProvidermay leavedefaultIdleTTLundefined, matching React: unused atoms are then cleaned up immediately instead of lingering for a provider-imposed TTL.pkg @effect/atom-vue — The Vue binding.
useAtomValue(() => atom)returns a readonlyRef, whileuseAtom(() => writable)returns a readonlyRefplus a setter through Vue's provide/inject registry.
In every binding, a setter in promise or promiseExit mode resolves with the atom's next settled result, not a result tied to that write; serialize writes when each caller needs its own result.
@effect/atom-react in practice
@effect/atom-react — pkg
Hooks built on React.useSyncExternalStore that read from the nearest RegistryContext. Wrap your tree in RegistryProvider; it creates one registry, accepts an optional defaultIdleTTL, and schedules whole-registry disposal 500 ms after unmount so an immediate remount can reuse it. The provider does not impose an idle TTL unless you pass one. The fallback context uses a standalone registry with a 400 ms default idle TTL, so a lone hook still works without a provider.
Mental model: the hooks are the only React-specific code. useAtomValue subscribes and re-renders on change; useAtom adds a setter; useAtomSuspense throws a promise while the result is Initial so a <Suspense> boundary shows the fallback (and throws the squashed cause for an error boundary unless you pass includeFailure).
Self-contained source · Type-checked
import { RegistryProvider, useAtomValue, useAtom } from "@effect/atom-react"
import { Atom } from "effect/reactivity"
// A writable atom for a proposed raise %, and a derived preview of the new base.
const proposedRaisePct = Atom.make(4)
const newBasePreview = Atom.make((get) => Math.round(120000 * (1 + get(proposedRaisePct) / 100)))
function RaiseSlider() {
const [pct, setPct] = useAtom(proposedRaisePct) // [value, setter]
const newBase = useAtomValue(newBasePreview) // read-only, re-renders on change
return (
<label>
Raise {pct}% → new base ${newBase.toLocaleString()}
<input
type="range" min={0} max={20} value={pct}
onChange={(e) => setPct(e.target.valueAsNumber)}
/>
</label>
)
}
export function App() {
return (
<RegistryProvider>
<RaiseSlider />
</RegistryProvider>
)
}End-to-end: effectful atom → AsyncResult → React
A query atom from AtomHttpApi (or an RPC client) produces an AsyncResult; the component reads it with useAtomValue and renders the four states with AsyncResult.matchWithWaiting. No useEffect, no manual loading flags, no race conditions.
Fragment · Type-checked with fixture
import { Option } from "effect"
import { useAtomValue } from "@effect/atom-react"
import { AsyncResult } from "effect/reactivity"
// `CompClient` is the AtomHttpApi.Service from the AtomHttpApi section above.
const summary1042 = CompClient.query("comp", "getCompSummary", {
params: { id: 1042 },
serializationKey: "comp:1042"
})
function CompSummaryCard() {
const result = useAtomValue(summary1042) // AsyncResult<CompSummary, never>
return AsyncResult.matchWithWaiting(result, {
// Initial or revalidating: keep showing the last good summary if we have it.
onWaiting: (r) =>
Option.match(AsyncResult.value(r), {
onNone: () => <p>Loading compensation…</p>,
onSome: (s) => (
<p aria-busy="true">Refreshing employee {s.employeeId}: ${s.baseSalary.toLocaleString()}</p>
)
}),
onError: (e) => <p role="alert">Declared endpoint error: {String(e)}</p>,
onDefect: (d) => <p role="alert">Unexpected defect: {String(d)}</p>,
onSuccess: (s) => (
<div>
<h3>Employee {s.value.employeeId}</h3>
<p>Base salary: ${s.value.baseSalary.toLocaleString()}</p>
<p>Level: {s.value.level}</p>
</div>
)
})
}Tip: Prefer Suspense over manual matching?
const s = useAtomSuspense(summary1042)returns theSuccessdirectly and suspends whileInitial— wrap the component in<Suspense fallback>and an error boundary. For SSR, render on the server with a registry,Hydration.dehydrateit, ship the packet, and feed it to<HydrationBoundary state={packet}>on the client.
Reach for it when building UI: useAtom for local writable state, useAtomValue + matchWithWaiting (or useAtomSuspense) for fetched data, useAtomSet for mutations, and RegistryProvider/HydrationBoundary at the edges.