Skip to content

TypeScript: UI Adapters

All 4 UI adapter packages depend only on published @zwire/core entry points (peerDependencies: { "@zwire/core": "^0.4.0" }): the root entry imports @zwire/core alone and a subpath @zwire/<fw>/<x> may additionally import its mirror @zwire/core/<x>. They share the useSignal / useAtom / useSelector / useScope / useScoped vocabulary, read through to signal.get(), and release subscriptions with their owning component or SSR request.

Every hook takes an optional options object as its last parameter; { scope } overrides the scope provided by ScopeProvider. useSelector also accepts a bare equality function there: useSelector(user, pick, shallowEqual) or useSelector(user, pick, { equals, scope }). useScoped(definition, options?) returns the instance of a core scoped() definition for the active scope.

ScopeProvider accepts an optional scope. Given one, it provides it and never disposes it. Without one it creates a scope under the enclosing scope, provides it, and disposes it when the provider unmounts; mode ('client' | 'ssr') overrides the inherited mode. Misuse that cannot work throws an error prefixed with the package name, such as [@zwire/react] ScopeProvider received a disposed scope.

Each adapter also ships subpaths that mirror @zwire/core subpaths: /tx, /collections, /resource and /protocol. Single-value hooks return a value in React, a ref in Vue, an accessor in Solid and a current ref in Svelte; Vue, Solid and Svelte accept getters for arguments that may change.

React 18/19 Compiler-safe hooks (peerDependencies: { "@zwire/core": "^0.4.0", "react": ">=18.0.0" }).

Export Signature Description
ScopeProvider <ScopeProvider scope?={scope} mode?={mode}>{children}</ScopeProvider> Provides a ReactiveScope to descendant hooks. A scope it creates is disposed once the unmount is final, so StrictMode effect replay never hands children a disposed scope.
useScope useScope(): ReactiveScope Nearest ScopeProvider scope, or defaultScope.
useScoped useScoped<T>(definition: Scoped<T>, options?): T Instance of a scoped() definition for the active scope.
useSignal useSignal<T>(signal: ReadableSignal<T>, options?): T Urgent tearing-free useSyncExternalStore read.
useAtom useAtom(signal, options?) Returns a read-through value and stable setter tuple. The setter accepts a value or an updater.
useDeferredSignal useDeferredSignal<T>(signal: ReadableSignal<T>, options?): DeferredSignalResult<T> Concurrent useDeferredValue-backed read returning { value, isPending } with zero tearing.
useSelector useSelector<T, S>(signal: ReadableSignal<T>, selector: (v: T) => S, equalsOrOptions?): S Memoized fine-grained selector backed by SelectorStabilizer + useInsertionEffect.
SelectorStabilizer class SelectorStabilizer<T, S> Pure state holder separating render-phase snapshot memoization from committed subscription comparison.

Vue 3.5+ composables (peerDependencies: { "@zwire/core": "^0.4.0", "vue": ">=3.5.0" }). useSignal, useAtom and useSelector throw when called outside setup() or an effect scope, where the returned ref could never update.

Export Signature Description
ScopeProvider / provideScope / ScopeKey <ScopeProvider :scope="scope"> / provideScope(scope?, { mode }?) / app.provide(ScopeKey, scope) Scope provision at component or app level. provideScope() returns the scope it provides, creating one when called without a scope.
useScope useScope(): ReactiveScope Resolves the active scope (safe outside an injection context).
useScoped useScoped<T>(definition: Scoped<T>, options?): T Instance of a scoped() definition for the active scope.
useSignal useSignal<T>(signal: MaybeRefOrGetter<ReadableSignal<T>>, options?): Readonly<Ref<T>> Read-through customRef (unwraps signals via toRaw).
useAtom useAtom<T>(signal: MaybeRefOrGetter<WritableSignal<T>>, options?): Ref<T> Two-way ref for v-model (stores values literally, functions included).
useSelector useSelector<T, S>(source, selector, equalsOrOptions?): ComputedRef<S> Lazy computed slice with early cutoff.

Solid 2 primitives (peerDependencies: { "@zwire/core": "^0.4.0", "solid-js": ">=2.0.0" }, engines.node >= 22.12.0).

This adapter targets Solid 2’s callable context providers and owned writes. Solid 1.x is outside its supported peer range.

Export Signature Description
ScopeProvider / ScopeContext <ScopeProvider scope?={scope} mode?={mode}>{children}</ScopeProvider> Provides a ReactiveScope to descendant primitives.
useScope useScope(): ReactiveScope Resolves the active scope.
useScoped useScoped<T>(definition: Scoped<T>, options?): T Instance of a scoped() definition for the active scope.
useSignal useSignal<T>(signal: MaybeAccessor<ReadableSignal<T>>, options?): Accessor<T> Read-through accessor with owner-scoped onCleanup and ownedWrite signal.
useAtom useAtom(signalOrAccessor, options?) Returns an accessor and setter tuple. Writes target the current signal.
useSelector useSelector<T, S>(source, selector, equalsOrOptions?): Accessor<S> createMemo slice with custom equals.

Svelte 5.7+ runes-compatible refs and store adapters (peerDependencies: { "@zwire/core": "^0.4.0", "svelte": ">=5.7.0" }).

Export Signature Description
ScopeProvider <ScopeProvider {scope}>{children}</ScopeProvider> Native Svelte component. Import its default from @zwire/svelte/ScopeProvider.svelte or the named export from @zwire/svelte with the svelte export condition. Replacing the scope remounts the child subtree.
provideScope / useScope provideScope(scope?, { mode }?) / useScope() Svelte context-based scope provision and resolution. provideScope() returns the scope it provides, creating one when called without a scope.
useScoped useScoped<T>(definition: Scoped<T>, options?): T Instance of a scoped() definition for the active scope.
useSignal useSignal<T>(signal: MaybeGetter<ReadableSignal<T>>, options?): SignalRef<T> { readonly current } backed by createSubscriber (never attaches on server).
useAtom useAtom<T>(signal: MaybeGetter<WritableSignal<T>>, options?): AtomRef<T> { current } with setter for bind:value={ref.current}.
useSelector useSelector<T, S>(source, selector, equalsOrOptions?): SignalRef<S> Slice ref with equals cutoff.
toReadable / toWritable toReadable(signal, options?) / toWritable(signal, options?) Svelte store contract ($store, bind:value={$store}).

Each adapter’s /tx subpath imports @zwire/core/tx, so the hooks take no factory. Reactive fields follow the framework: plain values on a fresh object in React (methods stay referentially stable), Readonly<Ref<T>> in Vue, Accessor<T> in Solid, and getters in Svelte.

Export Signature Description
useOptimisticLayer useOptimisticLayer(options?): { active, update, settle, revert } One component-held optimistic layer. The first update(signal, updater) opens it and active stays true until settle(serverUpdate?) or revert(); the next update opens a new layer. With no layer open, settle(serverUpdate) still runs serverUpdate in a batch. Unmounting reverts an open layer, after which update throws. The handle can be passed as the layer argument of TransactionalCollection mutations.
useOptimisticAction useOptimisticAction(fn: (tx, ...args) => Promise<R> | R, options?): { run, runAsync, pending, error, reset } Wraps runOptimistic: one layer per call, reverted on rejection or abort and settled atomically with options.settle(result) on success. run(...args) never throws and reports failures through error; runAsync(...args) returns the promise. options.concurrency is 'parallel' (default, independent layers rebased in creation order), 'latest' (a new call aborts those in flight) or 'drop' (calls made while one is in flight join it). Calls keep running after unmount unless options.abortOnUnmount is set; disposing the scope always aborts. Aborts are not recorded as error.
useDraft useDraft(options?: { base?, scope? }): { open, get, set, run, commit, discard } One component-held STM draft. set and run open it; get(signal) reads the draft while one is open and the live value otherwise, and is reactive to draft changes. commit(options?) and discard() close it, and unmounting discards an open draft.

7.6 Collection hooks (@zwire/<fw>/collections)

Section titled “7.6 Collection hooks (@zwire/<fw>/collections)”

Parameters are structural, so the hooks accept any collection exposing the matching view and add no runtime import of @zwire/core/collections. Only unfiltered Top-K views are cached by core, so the hooks keep one view per argument change. options.filter must be referentially stable in React (a new function builds a new view) and is fixed for the lifetime of the hook elsewhere.

Export Signature Description
useCollectionTopK useCollectionTopK(collection, k, options?: { filter?, scope? }) The first k rows in sort order (Collection, QueryCollection).
useCollectionRange useCollectionRange(collection, offset, limit, options?: { filter?, scope? }) The window of limit rows starting at offset (QueryCollection, TransactionalCollection).
useCollectionItem useCollectionItem(collection, id, options?) One row by id, updating only when that row changes (QueryCollection).
useCollectionSize useCollectionSize(collection, options?) The number of rows (QueryCollection).

The hooks read an existing Resource<T>; create it in a scoped() definition or at module level, not inside a component that may suspend. Subscribing starts a lazy resource.

Export Signature Description
useResource useResource<T>(resource, options?): ResourceView<T> Never suspends. Exposes status ('pending' | 'refreshing' | 'ready' | 'error' | 'disposed'), data (the last successful value, kept while refreshing and after an error), error, pending, refetch() and mutate(next). React returns a discriminated union, so checking status narrows data; Vue returns refs, Solid accessors and Svelte getters.
useSuspenseResource useSuspenseResource<T>(resource, options?) React and Solid only. React returns T: it suspends on the initial load, throws a load error to the nearest error boundary, and returns the last value while refreshing. Solid returns Accessor<T> that shows the nearest <Loading> fallback on the initial load and sends a load error to <Errored>.
Export Signature Description
useHydrate useHydrate(state: HydratedState | undefined, options?): void Applies hydrate(scope, state) synchronously, before descendants read, and at most once per (scope, state object) pair. React applies the state present on the first render during that render and a later state object after commit. Vue accepts a ref or getter and Solid an accessor to apply later snapshots as they arrive.
HydrationBoundary <HydrationBoundary state={state}>{children}</HydrationBoundary> React, Vue and Solid. Hydrates the scope provided by the enclosing ScopeProvider, for use when the provider and the hydration call share a component. In Svelte call useHydrate(state, { scope: provideScope() }) instead.

The runtime crates support no_std with alloc; SyncScope requires the sync and std features. The optional zwire-derive proc macro runs on the compiler host. The workspace uses Edition 2024, Rust 1.100, forbid(unsafe_code), and zero external runtime dependencies.