Hydration.ts
Hydration.ts overview
Section titled “Hydration.ts overview”Saves and restores serializable atom state.
dehydrate reads atoms marked with Atom.serializable from an
AtomRegistry and returns encoded entries keyed by their serialization keys.
hydrate preloads those entries into another registry before the atoms are
read. Initial AsyncResult values can be ignored, encoded as values, or
represented by promises that update the target registry once the result is no
longer initial.
Since v4.0.0
Exports Grouped by Category
Section titled “Exports Grouped by Category”dehydration
Section titled “dehydration”dehydrate
Section titled “dehydrate”Encodes the serializable atoms currently stored in a registry into dehydrated state.
Details
Only atoms marked with Atom.serializable are included. encodeInitialAs
controls whether AsyncResult.Initial values are ignored, encoded as values, or
represented by promises that resolve when the atom leaves the initial state.
Signature
declare const dehydrate: ( registry: AtomRegistry.AtomRegistry, options?: { readonly encodeInitialAs?: "ignore" | "promise" | "value-only" | undefined }) => Array<DehydratedAtom>Since v4.0.0
toValues
Section titled “toValues”Returns dehydrated state entries as DehydratedAtomValue records.
Signature
declare const toValues: (state: ReadonlyArray<DehydratedAtom>) => Array<DehydratedAtomValue>Since v4.0.0
hydration
Section titled “hydration”hydrate
Section titled “hydrate”Applies dehydrated atom state to a registry.
When to use
Use to preload serialized atom values into a target registry before those atoms are read.
Details
Encoded values are preloaded by serialization key. Entries with a
resultPromise update the matching registry node, or preload the resolved value,
when the promise resolves.
Signature
declare const hydrate: (registry: AtomRegistry.AtomRegistry, dehydratedState: Iterable<DehydratedAtom>) => voidSince v4.0.0
models
Section titled “models”DehydratedAtom (interface)
Section titled “DehydratedAtom (interface)”Marker interface for entries in a dehydrated atom registry state.
Signature
export interface DehydratedAtom { readonly "~effect/reactivity/DehydratedAtom": true}Since v4.0.0
DehydratedAtomValue (interface)
Section titled “DehydratedAtomValue (interface)”A dehydrated serializable atom value.
Details
It stores the atom serialization key, encoded value, dehydration timestamp, and
an optional promise used when an AsyncResult.Initial value is encoded as a
future non-initial value.
Signature
export interface DehydratedAtomValue extends DehydratedAtom { readonly key: string readonly value: unknown readonly dehydratedAt: number readonly resultPromise?: Promise<unknown> | undefined}Since v4.0.0