Stores, Optics & Resources
이 콘텐츠는 아직 번역되지 않았습니다.
Beyond primitive atoms and computeds, 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)
Section titled “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 afterget()exposes it—giving ReactuseSyncExternalStoresnapshot immutability without eager per-write clones.createDeepStore(scope, initial): Zero-Proxy recursive store supportingDate,Map,Set, and frozen objects, paired withreconcile()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 dynamicMapStoreandprojectionhelpers inzwire::store.
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()); // trueType-safe optics (@zwire/core/optics & zwire::optics)
Section titled “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 withzwire::optics::Lens.
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)
Section titled “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:
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)
Section titled “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.