# Stores, Optics & Resources

> Lazy CoW ObjectStore, DeepStore, selectors, bidirectional focus/lens optics, NotReady-aware async resources, and WeakRef devtools.

Beyond primitive `atom`s and `computed`s, `zwire` offers opt-in subpaths for structured records, deep path lenses, lazy async resources, and graph inspection. Every extension composes with `@zwire/core` through the five lazy extension slots and adds **0 B** when unused.

## Fine-grained stores (`@zwire/core/store` & `zwire::store`)

- **`createObjectStore(scope, initial)`**: Tracks per-key atoms lazily and maintains an immutable snapshot using lazy Copy-on-Write (`_sh` / `snapshotShared`). `setKey()` mutates in-place while no caller has observed the snapshot, cloning once after `get()` exposes it—giving React `useSyncExternalStore` snapshot immutability without eager per-write clones.
- **`createDeepStore(scope, initial)`**: Zero-Proxy recursive store supporting `Date`, `Map`, `Set`, and frozen objects, paired with **`reconcile()`** for structural diffing.
- **`createSelector(source)`** & **`createProjection(fn)`**: `O(1)` keyed selection (only the matching key's subscribers re-run when the selected ID changes) and derived structural projections.
- **Rust `#[derive(ReactiveStore)]` & `MapStore`**: Generates typed per-field atom accessors at compile time (`zwire-derive`) plus dynamic `MapStore` and `projection` helpers in `zwire::store`.

```ts
import { createScope } from '@zwire/core';
import { createObjectStore, createSelector } from '@zwire/core/store';

const scope = createScope();
const user = createObjectStore(scope, { name: 'Ada', role: 'admin', active: true });
const isAdmin = createSelector(() => user.field('role').get(), { scope });

console.log(user.field('name').get()); // "Ada"
console.log(isAdmin('admin').get());   // true
```

## Type-safe optics (`@zwire/core/optics` & `zwire::optics`)

- **`createAtomMap(scope, factory)`**: Keyed family of lazy atoms with automatic eviction when unwatched.
- **`focus(atom, ...path)`** & **`lens(atom, { get, set })`**: Bidirectional type-safe lenses over nested immutable state (`DeepPath<T>`, `DeepValue<T, P>`). Rust mirrors bidirectional optics with `zwire::optics::Lens`.

```ts
import { atom, createScope } from '@zwire/core';
import { focus, lens } from '@zwire/core/optics';

const scope = createScope();
const profile = atom({ user: { name: 'Ada' } }, { scope });
const userName = focus(profile, 'user', 'name');
userName.set('Grace');

const celsius = atom(0, { scope });
const fahrenheit = lens(celsius, {
  get: (c) => (c * 9) / 5 + 32,
  set: (_c, f) => ((f - 32) * 5) / 9,
});
```

## Async resources (`@zwire/core/resource` & `zwire::resource`)

`resource(fetcher, { scope, lazy })` tracks async state (`pending` / `ready` / `error`) and integrates with the core `NotReady` sentinel so derived `computed()` nodes can read pending values without triggering error handlers:

```ts
import { createScope } from '@zwire/core';
import { isPending, resolve, resource } from '@zwire/core/resource';

const scope = createScope();
const profile = resource(async () => ({ name: 'Ada' }), { scope, lazy: true });

if (isPending(profile)) {
  const data = await resolve(profile);
  console.log(data.name); // "Ada"
}
```

UI adapters expose `useResource` and `useSuspenseResource` under `@zwire/{react,vue,solid,svelte}/resource`. Rust provides an executor-agnostic `#![no_std]` `Resource<T, E>` in `zwire::resource`.

## Graph inspection (`@zwire/core/devtools`)

`createInspectableScope()` registers nodes with `WeakRef`-backed labels (`scope.atom(0, { label: 'total' })`) and exposes `inspectGraph()` for dependency visualization without preventing garbage collection.
