page wrnexusreactive {
  seo {
    title = "@wrnexus/reactive"
    description = "Small type-safe reactive signal primitives."
  }

  view {
    <div class="docs-shell">
      <SkipLink label="Skip to content" href="#main" class="docs-skip-link" />
      <header class="topbar">
        <a class="brand" href="/"><span>W</span> WRNexusJS</a>
        <nav aria-label="Primary"><a href="/getting-started">Get started</a><a href="/packages">Packages</a><a href="/components">Components</a><a href="/language">Language</a><a href="/architecture">Architecture</a></nav>
        <div class="topbar-actions"><a class="preview-pill" href="/access">Private preview · v0.3.2</a><button data-wire-theme-toggle class="theme-button" aria-label="Toggle color theme" title="Toggle color theme">◐</button></div>
      </header>
      <div class="mobile-doc-nav"><details><summary>Browse documentation</summary><nav><a href="/getting-started">Get started</a><a href="/packages">Packages</a><a href="/components">Components</a><a href="/language">Language</a><a href="/architecture">Architecture</a><a href="/tutorial">Tutorial</a><a href="/guides/project-structure">Guides</a><a href="/examples">Examples</a><a href="/search">Search</a></nav></details></div>
      <main class="portal-main docs-layout">
      <article id="main" class="documentation prose standalone package-document"><nav class="breadcrumbs" aria-label="Breadcrumb"><a href="/">Home</a><span>/</span><a href="/packages">Packages</a><span>/</span><span aria-current="page">@wrnexus/reactive</span></nav><section class="doc-intro"><span class="eyebrow">Frontend · Package reference</span><h1>@wrnexus/reactive</h1><p>Small type-safe reactive signal primitives.</p><div class="doc-meta"><span>v0.3.2</span><span>Private registry</span><span>Frontend</span></div><section id="access" class="access-callout"><h2>Install the package</h2><p>After WorkRoot approves private registry access, install the release-aligned package:</p><pre><code>bun add @wrnexus/reactive@0.3.2</code><button type="button" class="copy-button" aria-label="Copy installation command">Copy</button></pre><p><a href="/access">Request preview access</a>. Never put registry tokens in source control.</p></section></section><section id="guide"><blockquote>Tiny, type-safe reactive primitives (signals) with zero dependencies.</blockquote>
<p>Part of the <strong>WRNexusJS</strong> framework — an SSR-first, Bun-native full-stack web framework.</p>
<h3 id="overview">Overview</h3>
<p><code>@wrnexus/reactive</code> is the seed of WRNexusJS's reactivity layer: a minimal <code>signal</code> primitive that holds a value, notifies subscribers when it changes, and hands back an unsubscribe function. It is deliberately small and framework-agnostic — it powers nothing on its own, but is shaped so client islands (and later the <code>.wrn</code> compiler's <code>state</code> blocks) can build reactive bindings on top of it. Reach for it when you need observable state without pulling in a full reactivity library.</p>
<pre data-language="bash"><code>bun add @wrnexus/reactive</code></pre>
<blockquote>Private package — the machine must be authenticated to the <code>wrnexus</code> npm org</blockquote>
<blockquote>(a read token in <code>~/.npmrc</code>). Requires <strong>Bun</strong> (Node is not supported).</blockquote>
<h3 id="api">API</h3>
<p>The package has a single entry point (<code>.</code>) exporting one function and three types.</p>
<h4 id="signal-t-initial-t-signal-t"><code>signal&lt;T&gt;(initial: T): Signal&lt;T&gt;</code></h4>
<p>Creates a reactive signal seeded with <code>initial</code>. Returns a <code>Signal&lt;T&gt;</code>:</p>
<div class="table-wrap"><table>
<thead><tr><th>Member</th><th>Signature</th><th>Description</th></tr></thead>
<tbody><tr><td><code>get</code></td><td><code>(): T</code></td><td>Read the current value.</td></tr><tr><td><code>set</code></td><td><code>(next: T): void</code></td><td>Write a new value. Subscribers run <strong>only when the value actually changes</strong> (compared with <code>Object.is</code>).</td></tr><tr><td><code>update</code></td><td><code>(fn: (current: T) =&gt; T): void</code></td><td>Apply a function to the current value; equivalent to <code>set(fn(get()))</code>.</td></tr><tr><td><code>subscribe</code></td><td><code>(fn: Subscriber&lt;T&gt;): Unsubscribe</code></td><td>Register a subscriber; returns a function that removes it.</td></tr></tbody></table></div>
<h4 id="types">Types</h4>
<pre data-language="ts"><code>type Subscriber&lt;T&gt; = (value: T) =&gt; void;
type Unsubscribe = () =&gt; void;

interface Signal&lt;T&gt; &#123;
  get(): T;
  set(next: T): void;
  update(fn: (current: T) =&gt; T): void;
  subscribe(fn: Subscriber&lt;T&gt;): Unsubscribe;
&#125;</code></pre>
<p>Notes on semantics:</p>
<ul>
<li><strong>No-op updates are skipped.</strong> <code>set</code> compares the incoming value to the current</li>
<p>one with <code>Object.is</code>; identical values do not notify subscribers.</p>
<li><strong>Safe unsubscribe during notification.</strong> Subscribers are iterated over a copy of</li>
<p>the subscriber set, so a subscriber may call its own (or another's) unsubscribe while a notification is in flight.</p>
</ul>
<h3 id="usage">Usage</h3>
<pre data-language="ts"><code>import &#123; signal &#125; from &quot;@wrnexus/reactive&quot;;

const count = signal(0);

count.get(); // 0

// Subscribe; the returned function unsubscribes.
const off = count.subscribe((value) =&gt; &#123;
  console.log(&quot;count is now&quot;, value);
&#125;);

count.set(1); // logs: count is now 1
count.set(1); // no-op — value unchanged, no notification
count.update((n) =&gt; n + 1); // logs: count is now 2

off(); // stop listening
count.set(3); // nothing logged</code></pre>
<p>Typed signals infer <code>T</code> from the initial value, or can be annotated explicitly:</p>
<pre data-language="ts"><code>import &#123; signal, type Signal &#125; from &quot;@wrnexus/reactive&quot;;

const user: Signal&lt;&#123; name: string &#125; | null&gt; = signal(null);
user.set(&#123; name: &quot;Ada&quot; &#125;);</code></pre>
<h3 id="requirements-notes">Requirements / Notes</h3>
<ul>
<li><strong>Bun-only.</strong> Distributed as TypeScript source (<code>main</code>/<code>exports</code> point at</li>
<p><code>src/index.ts</code>); consume it under Bun, which runs <code>.ts</code> directly.</p>
<li><strong>Zero dependencies.</strong> The only runtime API used is the standard <code>Object.is</code>.</li>
<li>Foundational primitive for WRNexusJS client islands and the forthcoming <code>.wrn</code></li>
<p>compiler <code>state</code> blocks.</p>
</ul></section><section id="api" class="api"><h2>Complete TypeScript API</h2><p>Generated from the exact installed package declarations.</p><pre data-language="typescript"><code>/**
 * Fine-grained reactive primitives shared by server utilities and client code.
 * Updates are synchronous by default and coalesced inside `batch()`.
 */
type Subscriber&lt;T&gt; = (value: T, previous?: T) =&gt; void;
type Unsubscribe = () =&gt; void;
type Cleanup = () =&gt; void;
interface Signal&lt;T&gt; &#123;
    get(): T;
    set(next: T): void;
    update(fn: (current: T) =&gt; T): void;
    subscribe(fn: Subscriber&lt;T&gt;): Unsubscribe;
&#125;
interface ReadonlySignal&lt;T&gt; &#123;
    get(): T;
    subscribe(fn: Subscriber&lt;T&gt;): Unsubscribe;
&#125;
/** Coalesce every signal notification made by `fn` into one flush. */
declare function batch&lt;T&gt;(fn: () =&gt; T): T;
/** Read reactive values without recording dependencies. */
declare function untrack&lt;T&gt;(fn: () =&gt; T): T;
declare function signal&lt;T&gt;(initial: T): Signal&lt;T&gt;;
/**
 * Run a dependency-tracked side effect. Dependencies are rebuilt after every
 * execution, preventing stale subscriptions when conditional reads change.
 */
declare function effect(run: () =&gt; void | Cleanup): Cleanup;
/** Create a lazily readable derived signal with automatic dependency tracking. */
declare function computed&lt;T&gt;(read: () =&gt; T): ReadonlySignal&lt;T&gt;;

export &#123; type Cleanup, type ReadonlySignal, type Signal, type Subscriber, type Unsubscribe, batch, computed, effect, signal, untrack &#125;;
</code></pre></section><section id="examples" class="examples"><h2>Examples</h2><p>Copy-ready examples from the installed package documentation.</p><div class="example-grid"><article class="example-card"><h3>Typical usage</h3><pre data-language="ts"><code>import &#123; signal &#125; from &quot;@wrnexus/reactive&quot;;

const count = signal(0);

count.get(); // 0

// Subscribe; the returned function unsubscribes.
const off = count.subscribe((value) =&gt; &#123;
  console.log(&quot;count is now&quot;, value);
&#125;);

count.set(1); // logs: count is now 1
count.set(1); // no-op — value unchanged, no notification
count.update((n) =&gt; n + 1); // logs: count is now 2

off(); // stop listening
count.set(3); // nothing logged</code></pre></article><article class="example-card"><h3>Typed signals infer T from the initial value, or can be annotated explicitly</h3><pre data-language="ts"><code>import &#123; signal, type Signal &#125; from &quot;@wrnexus/reactive&quot;;

const user: Signal&lt;&#123; name: string &#125; | null&gt; = signal(null);
user.set(&#123; name: &quot;Ada&quot; &#125;);</code></pre></article></div></section></article>
      <aside class="on-this-page"><h2>On this page</h2><nav><a class="toc-level-2" href="#guide">Guide</a><a class="toc-level-3" href="#overview">Overview</a><a class="toc-level-3" href="#api">API</a><a class="toc-level-4" href="#signal-t-initial-t-signal-t">signal&lt;T&gt;(initial: T): Signal&lt;T&gt;</a><a class="toc-level-4" href="#types">Types</a><a class="toc-level-3" href="#usage">Usage</a><a class="toc-level-3" href="#requirements-notes">Requirements / Notes</a><a class="toc-level-2" href="#api">Complete API</a><a class="toc-level-2" href="#examples">Examples</a></nav></aside>
    </main>
      <footer><div class="footer-brand"><span class="footer-mark" aria-hidden="true">W</span><p><strong>WRNexusJS 0.3.2</strong><span>Complete API documentation generated from installed package declarations.</span></p></div><nav aria-label="Footer"><a href="/packages">All packages</a><a href="/getting-started">Get started</a><a href="/security">Security</a><a href="/support">Support</a><a href="/llms.txt">AI guide</a></nav><p class="footer-meta">Private Developer Preview · Bun-native</p></footer>
      <BackToTop />
    </div>
  }
}
