@aihu/signals
v0.5.2
Published
Tiny reactive signals — the reactive primitive at the core of aihu.
Readme
@aihu/signals
Aihu — agentic discovery and interaction, for human purpose.
Tiny reactive signals — the reactive primitive at the core of aihu.
Part of the runtime core layer of the Aihu meta-framework. Shipped to the client; sized via bun run size. The runtime core is dep-free and stacks under @aihu/runtime → @aihu/router → @aihu/server → @aihu/app.
Small (≤ 2 kB gz) reactive primitives — the foundation layer for Aihu's arbor renderer. One read shape (signal tuple), one underlying cell. Sync semantics, lazy computed, explicit batch. No proxies, no scheduler queue, no global tick.
Hello counter
import { signal, effect } from '@aihu/signals'
const [count, setCount] = signal(0)
effect(() => console.log('count =', count()))
setCount(1) // logs "count = 1"
setCount((n) => n + 10) // logs "count = 11"Derived values with computed
import { signal, computed, effect } from '@aihu/signals'
const [n, setN] = signal(2)
const doubled = computed(() => n() * 2)
effect(() => console.log('doubled =', doubled())) // logs "doubled = 4"
setN(3) // logs "doubled = 6"computed is lazy: the body doesn't run until you read it. Subsequent reads return the cached value until a dep changes.
Atomic updates with batch
import { signal, effect, batch } from '@aihu/signals'
const [name, setName] = signal('')
const [email, setEmail] = signal('')
effect(() => save(name(), email())) // runs once per save event
batch(() => {
setName('Ada')
setEmail('[email protected]')
})
// effect ran exactly once after the batch closed — not once per write.batch returns void. To get a value out, capture it in a closure variable:
let id: string
batch(() => { id = insertRow(); setName('Ada') })Cross-library cheat sheet
| Vue 3 | Solid | Preact Signals | aihu |
|---|---|---|---|
| ref(0) + .value | createSignal(0) → [get, set] | signal(0) + .value | signal(0) → [get, set] |
| computed(fn) + .value | createMemo(fn) (eager) | computed(fn) + .value (lazy) | computed(fn) (lazy, call-shape) |
| watchEffect(fn) → WatchHandle.stop() | createEffect(fn) (microtask) | effect(fn) → dispose | effect(fn) → dispose (sync) |
| nextTick (no batch) | batch(fn) (returns) | batch(fn) (void) | batch(fn) (void) |
Untrack a read
import { signal, computed, untrack } from '@aihu/signals'
const [a, setA] = signal(1)
const [b, setB] = signal(10)
const sum = computed(() => a() + untrack(() => b()))
// sum tracks `a` only; setB updates won't recompute `sum`Effect error isolation
When multiple effects fire in the same wave and one throws, sibling effects still run. The first thrown error rethrows directly; multiple thrown errors surface as AggregateError:
const [n, setN] = signal(0)
effect(() => { /* runs */ })
effect(() => { if (n() > 0) throw new Error('boom') })
effect(() => { /* still runs after sibling throws */ })
try { setN(1) } catch (e) { /* e is the original Error */ }Computed-body throws bypass this path and continue to fail fast.
Storing functions in signals
Signal<T> for function-typed T requires the closure-of-closure idiom:
const [getFn, setFn] = signal<() => number>(() => 5)
setFn(() => 6) // ❌ TypeScript error
setFn(() => () => 6) // ✅ stores the functionThe runtime disambiguates value vs updater by typeof === 'function', so a raw function is unambiguous only via the updater form. Mirrors SolidJS's Setter<T>. Detail: .team/phase-2/spec-signals-write-of-functions.md.
v0 limitations
- Cycle errors carry no chain context.
SignalCircularErroris thrown synchronously from the writer; richer chain info lands with devtools. - No
peek/onCleanup. Single-purpose primitives only; arbor's higher-level scopes live in@aihu/arbor. - Batch cascade cap.
batch()re-iterates up to 100 times before throwingSignalCircularError. The cap is internal; if tooling needs to sanity-check legitimate cascade depth in the future, we'll re-export it.
Install
npm install @aihu/signals
# or
bun add @aihu/signalsAuto-generated against @aihu/[email protected].
Package facts
| | |
|---|---|
| Version | 0.5.2 |
| Tier | A — Reactive runtime core — signals/computeds/effects |
| Bundle size | 2.18 kB (gz) — limit 2350 B |
| Published files | 3 entries |
| License | MIT |
Auto-generated against @aihu/[email protected].
Exports
| Subpath | ESM | CJS |
|---|---|---|
| . | ./dist/index.js | — |
| ./lifecycle | ./dist/lifecycle.js | — |
Auto-generated against @aihu/[email protected].
Dependencies
Zero runtime dependencies (per the dep-free thesis).
Auto-generated against @aihu/[email protected].
Server-Side Rendering (SSR)
@aihu/signals uses module-level reactive state (currentObserver, batchQueue, effectQueue, wave, and the link pool) that is shared across all concurrent requests in the same Node.js worker thread — it is not request-isolated by default.
If you use @aihu/signals in server-side code (via @aihu-plugin/data's resource() or directly):
- Wrap all async operations inside effects with
untrack()to preventcurrentObserverfrom leaking across request boundaries during event-loop yields. - Do not hold live reactive graphs between requests — dispose effects when the request completes.
- For true request isolation, instantiate signals within a request-scoped context rather than at module level.
Client-side usage (browser custom elements, arbor mounts) is unaffected — each tab is an isolated JS environment.
See also
Auto-generated against @aihu/[email protected].
License
MIT — see LICENSE.
Auto-generated against @aihu/[email protected].
