Skip to content

Public predicate to check whether a serializable key is hydrated in an AtomRegistry #10833

Open

Description

@CasperEngl

Problem

When using Hydration.dehydrate, Hydration.hydrate, and Atom.withServerValue for SSR / hydration, it is common to want a serializable atom to:

  • return its hydrated value during Atom.getServerValue(atom, registry) when it was hydrated into the registry, and
  • fail fast (or return a fallback) without executing its Effect when it was not hydrated.

Today, checking whether a serializable key is present in an AtomRegistry requires inspecting two separate internal structures:

  1. Immediately after Hydration.hydrate(registry, state), the entry lives only in registry.preloadedSerializable (registry.getNodes().has(key) is false).
  2. On the first read (ensureNode), AtomRegistry deletes the key from registry.preloadedSerializable and creates a node in registry.getNodes(), so on any subsequent read (e.g. a second component reading the same atom or React reading getServerSnapshot again during hydration), registry.preloadedSerializable.has(key) is false while registry.getNodes().has(key) is true.

Neither check works on its own, and combining them couples user code to preloadedSerializable and getNodes() internals.

Environment

  • effect: 4.1.5 (verified on main at 66ab99202)

Minimal Reproducible Example

Install [email protected], save as repro.ts, and run with node repro.ts (type-checks cleanly with tsc --noEmit under strict: true):

import { Effect, Schema } from "effect"
import { AsyncResult, Atom, AtomRegistry, Hydration } from "effect/reactivity"

const currentRegistry = Atom.readable((get) => get.registry).pipe(Atom.keepAlive)
const resultSchema = AsyncResult.Schema({ success: Schema.String })

let serverRuns = 0

function makeQuery(id: string) {
  const key = `item:${id}`
  const base = Atom.make(
    Effect.sync(() => {
      serverRuns++
      return `fetched:${id}`
    }),
  ).pipe(
    Atom.serializable({
      key,
      schema: resultSchema,
      skipInitialRun: true,
    }),
  )

  return Atom.withServerValue(base, (get) => {
    const registry = get(currentRegistry)
    const inNodes = registry.getNodes().has(key)
    const inPreloaded = registry.preloadedSerializable.has(key)
    console.log(`[${key}] inNodes=${inNodes}, inPreloaded=${inPreloaded}`)

    if (inNodes || inPreloaded) {
      return get(base)
    }
    throw new Error(`${key} was not hydrated`)
  })
}

// 1. Prefetch "item:1" on the server and dehydrate
const serverRegistry = AtomRegistry.make()
const serverItem1 = makeQuery("1")
serverRegistry.get(serverItem1)
const dehydrated = Hydration.dehydrate(serverRegistry)
console.log("after server prefetch, serverRuns =", serverRuns)

// 2. Hydrate into a render registry
const renderRegistry = AtomRegistry.make()
Hydration.hydrate(renderRegistry, dehydrated)

const item1 = makeQuery("1")
const item2 = makeQuery("2")

// Read #1 of hydrated atom: only in preloadedSerializable
console.log("item:1 read #1:", Atom.getServerValue(item1, renderRegistry))

// Read #2 of hydrated atom: moved to getNodes, removed from preloadedSerializable
console.log("item:1 read #2:", Atom.getServerValue(item1, renderRegistry))

// Read of unhydrated atom: in neither map -> fails fast without running its Effect
try {
  Atom.getServerValue(item2, renderRegistry)
} catch (error) {
  console.log("item:2 read error:", (error as Error).message)
}

console.log("final serverRuns (expected 1):", serverRuns)

Output:

after server prefetch, serverRuns = 1
[item:1] inNodes=false, inPreloaded=true
item:1 read #1: { _tag: 'Success', value: 'fetched:1', waiting: false, timestamp: ... }
[item:1] inNodes=true, inPreloaded=false
item:1 read #2: { _tag: 'Success', value: 'fetched:1', waiting: false, timestamp: ... }
[item:2] inNodes=false, inPreloaded=false
item:2 read error: item:2 was not hydrated
final serverRuns (expected 1): 1

Relevant implementation details (main at 66ab99202)

  • packages/effect/src/reactivity/Hydration.ts:166-171 — Hydration.hydrate calls registry.setSerializable(datom.key, datom.value).
  • packages/effect/src/reactivity/AtomRegistry.ts:82,398,415-416 — setSerializable writes into registry.preloadedSerializable.
  • packages/effect/src/reactivity/AtomRegistry.ts:484-486 — ensureNode reads and deletes the entry from preloadedSerializable when materializing the node in nodes.

Proposal

Add a documented public predicate — for example AtomRegistry.isHydrated(registry, key) (or a method on AtomRegistry / Hydration) — that returns true when the registry already holds a value for a serializable key (whether still preloaded or already materialized as a node), so callers do not have to depend on getNodes() and preloadedSerializable directly.

Activity

  1. self-assigned this
    on Nov 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions