import { JSXElementType, JSXElement, Props } from '../elements.js'; import '../jsx-globals.js'; import { capture } from './lifecycle.js'; import { For } from './control.js'; /** * State value holder - callable to read, has set method to update * @example * const count = state(0); * count(); // read: 0 * count.set(1); // write: triggers re-render */ interface State { (): T; set(...args: StateSetterArgs): void; [Symbol.iterator](): IterableIterator[number]>; } type StateUpdater = (prev: T) => T; type StateSetterArgs = [Extract unknown>] extends [ never, ] ? [value: T] | [updater: StateUpdater] : [updater: StateUpdater]; /** * Public setter type for state cells. */ type StateSetter = (...args: StateSetterArgs) => void; /** * Tuple-first state handle returned by `state()`. */ type StateTuple = [get: State, set: StateSetter] & State; /** Options shared by `state()` and `derive()`. */ interface ReactiveOptions { /** * Decides whether a new value equals the current one. When it returns * true the write is ignored and nothing is notified. Defaults to `Object.is`. */ equals?: (a: T, b: T) => boolean; /** Debug name shown in loop-guard, circular-dependency and unused-state diagnostics. */ name?: string; } /** Options for `selector()`. */ interface SelectorOptions { equals?: SelectorEquals; /** Debug name shown in diagnostics. */ name?: string; } /** Options for `store()`. */ interface StoreOptions { /** Debug name shown in write-guard errors. */ name?: string; } /** * Creates a local state value for a component * Optimized for: * - O(1) read performance * - Minimal allocation per state * - Fast scheduler integration * * IMPORTANT: state() must be called during component render execution. * It captures the current component instance from context. * Calling outside a component function will throw an error. * * @example * ```ts * // ✅ Correct: called during render * export function Counter() { * const [count, setCount] = state(0); * return { type: 'button', children: [count()] }; * } * * // ❌ Wrong: called outside component * const count = state(0); * export function BadComponent() { * return { type: 'div' }; * } * ``` */ declare function state( initialValue: T, options?: ReactiveOptions ): StateTuple; declare const SNAPSHOT_SOURCE_BRAND: unique symbol; type SnapshotSourceBrand = { readonly [SNAPSHOT_SOURCE_BRAND]: true; }; /** A reactive derived value produced by {@link derive}; call it to read the current result. */ interface Derived { (): T; } type SnapshotSource = { value: T | null; pending?: boolean; error?: Error | null; } & SnapshotSourceBrand; /** Creates a render-scoped derived value; must be called during component render. */ declare function derive( fn: () => TOut, options?: ReactiveOptions ): Derived; declare function derive( source: SnapshotSource | TIn | (() => TIn), map: (value: TIn) => TOut, options?: ReactiveOptions ): Derived; /** A fine-grained reactive membership check produced by {@link selector}. */ interface Selector { (candidate: T): boolean; } type SelectorEquals = { bivarianceHack(a: T, b: T): boolean; }['bivarianceHack']; /** * Creates a render-scoped predicate for keyed membership in reactive list rows. * * Use this when a `` child needs to compare each stable item with a * changing selected value. Unlike a plain closure capture, the predicate * subscribes the affected rows and updates them without rebuilding the list. * Must be called during component render. */ declare function selector( source: () => T, equals?: SelectorEquals | SelectorOptions ): Selector; /** * Runs `fn` with scheduling deferred and flushes once, synchronously, when the * outermost batch exits. Works anywhere: event handlers, timers, callbacks. * Returns the value of `fn`; a throw still flushes queued work. */ declare function batch(fn: () => T): T; /** * Runs `fn` without subscribing the running computation (a render or * `derive()`) to any source `fn` reads. Returns the value of `fn`. */ declare function untrack(fn: () => T): T; /** * Creates a deep reactive proxy over a plain object or array. Must be called * during component render; like `state()` it claims a positional slot and the * initial value is used only on the first render. * * Reading a property inside a render, `derive()` or binding subscribes to just * that property. Assignments, `delete` and array mutations notify the readers * they affect. Nested plain objects and arrays are wrapped lazily, with one * proxy per target. Writes during render throw, like `state.set()`. */ declare function store(initial: T, options?: StoreOptions): T; /** Returns the raw object behind a `store()` proxy; other values are returned as is. */ declare function unwrap(value: T): T; export { store, unwrap, batch, untrack, ReactiveOptions, SelectorOptions, StoreOptions, State, StateUpdater, StateSetterArgs, StateSetter, StateTuple, state, SNAPSHOT_SOURCE_BRAND, SnapshotSourceBrand, Derived, SnapshotSource, derive, Selector, SelectorEquals, selector, };