flexium
Version:
A lightweight, signals-based UI framework with cross-platform renderers
151 lines (149 loc) • 4.42 kB
text/typescript
/**
* Signal System - Fine-grained reactivity without VDOM
*
* Architecture:
* - Signals are reactive primitives that notify subscribers on change
* - Computed signals automatically track dependencies and memoize results
* - Effects run side effects and auto-track dependencies
* - Batching prevents cascading updates for performance
*/
/**
* Runs a function once when the component mounts.
* Equivalent to `effect(() => untrack(fn))` but clearer intent.
*/
declare function onMount(fn: () => void | (() => void)): void;
/**
* Base interface for reactive signals
* @internal
*/
interface Signal<T> {
value: T;
(): T;
set(value: T): void;
peek(): T;
}
/**
* Computed signal interface (read-only)
* @internal
*/
interface Computed<T> {
readonly value: T;
(): T;
peek(): T;
}
/**
* Creates a reactive signal
*
* @param initialValue - The initial value of the signal
* @returns A signal object with value getter/setter
*
* @example
* const count = signal(0);
* count.value++; // triggers subscribers
* console.log(count()); // alternative getter syntax
*/
declare function signal<T>(initialValue: T): Signal<T>;
/**
* Creates a computed signal (derived value)
*
* @param fn - Function that computes the derived value
* @returns A read-only computed signal
*
* @example
* const count = signal(1);
* const doubled = computed(() => count.value * 2);
* console.log(doubled.value); // 2
*/
declare function computed<T>(fn: () => T): Computed<T>;
/**
* Creates a side effect that runs when dependencies change
*
* @param fn - Effect function, can return a cleanup function
* @param options - Optional error handler
* @returns Dispose function to stop the effect
*
* @example
* const count = signal(0);
* const dispose = effect(() => {
* console.log('Count:', count.value);
* return () => console.log('Cleanup');
* });
*/
declare function effect(fn: () => void | (() => void), options?: {
onError?: (error: Error) => void;
}): () => void;
/**
* Batches multiple signal updates to prevent cascading updates
*
* @param fn - Function containing signal updates
*
* @example
* batch(() => {
* count.value++;
* name.value = "new";
* }); // effects only run once at the end
*/
declare function batch(fn: () => void): void;
/**
* Runs a function without tracking dependencies
* Useful for reading signals inside effects without creating dependencies
*
* @param fn - Function to run without tracking
* @returns The return value of fn
*/
declare function untrack<T>(fn: () => T): T;
/**
* Creates a root scope for effects
* All effects created within the scope can be disposed together
*
* @param fn - Function that creates effects
* @returns Dispose function for all effects in the scope
*/
declare function root<T>(fn: (dispose: () => void) => T): T;
/**
* Check if a value is a signal
*
* @param value - Value to check
* @returns True if value is a signal or computed
*
* @example
* const count = signal(0);
* isSignal(count); // true
* isSignal(5); // false
*/
declare function isSignal(value: any): value is Signal<any> | Computed<any>;
/**
* Registers a cleanup function that runs before the current effect re-runs or is disposed
*
* @param fn - Cleanup function
*/
declare function onCleanup(fn: () => void): void;
/**
* Resource interface for async data
*/
interface Resource<T> extends Signal<T | undefined> {
loading: boolean;
error: any;
state: 'unresolved' | 'pending' | 'ready' | 'refreshing' | 'errored';
latest: T | undefined;
/**
* Read value, throwing Promise if pending or Error if failed.
* Used by Suspense.
*/
read: () => T | undefined;
}
/**
* Creates a resource for handling async data
*
* @param source - Reactive source (signal or function) that triggers the fetcher
* @param fetcher - Async function that fetches data based on source
* @returns [Resource, Actions] tuple
*/
declare function createResource<T, S = any>(source: S | Signal<S> | (() => S), fetcher: (source: S, { value, refetching }: {
value: T | undefined;
refetching: any;
}) => Promise<T>): [Resource<T>, {
mutate: (v: T | undefined) => void;
refetch: () => void;
}];
export { type Computed as C, type Signal as S, createResource as a, batch as b, computed as c, onMount as d, effect as e, isSignal as i, onCleanup as o, root as r, signal as s, untrack as u };