( registry: AtomRegistry.AtomRegistry, dehydratedState: Iterable<DehydratedAtom> )
| 126 | * @since 4.0.0 |
| 127 | */ |
| 128 | export const hydrate = ( |
| 129 | registry: AtomRegistry.AtomRegistry, |
| 130 | dehydratedState: Iterable<DehydratedAtom> |
| 131 | ): void => { |
| 132 | for (const datom of (dehydratedState as Iterable<DehydratedAtomValue>)) { |
| 133 | registry.setSerializable(datom.key, datom.value) |
| 134 | |
| 135 | // If there's a resultPromise, it means this was in Initial state when dehydrated |
| 136 | // and we should wait for it to resolve to a non-Initial state, then update the registry |
| 137 | if (!datom.resultPromise) continue |
| 138 | datom.resultPromise.then((resolvedValue) => { |
| 139 | // Try to update the existing node directly instead of using setSerializable |
| 140 | const nodes = registry.getNodes() |
| 141 | const node = nodes.get(datom.key) |
| 142 | if (node) { |
| 143 | // Decode the resolved value using the node's atom serializable decoder |
| 144 | const atom = node.atom as any |
| 145 | if (atom[Atom.SerializableTypeId]) { |
| 146 | const decoded = atom[Atom.SerializableTypeId].decode(resolvedValue) |
| 147 | ;(node as any).setValue(decoded) |
| 148 | } |
| 149 | } else { |
| 150 | // Fallback to setSerializable if node doesn't exist yet |
| 151 | registry.setSerializable(datom.key, resolvedValue) |
| 152 | } |
| 153 | }) |
| 154 | } |
| 155 | } |
nothing calls this directly
no test coverage detected