export type EffectFunction = () => void | (() => void); export type EffectErrorFunction = (error: any, source?: Computation) => void; export declare let config: { verbose: boolean; }; export declare const setVerbose: (value: boolean) => void; export declare const Ident: { prefix: string | void; register: number; create(prefix?: string, extra?: string): string; }; export declare const register: (id: string, value: any) => void; /** * Represents a readonly signal that holds a value of type `T`. * Provides methods to get the current value, peek at the value without triggering * reactivity, and subscribe to changes in the value. * * @template T - The type of the value held by the signal. */ export interface ISignal { readonly id: string; readonly value: T; get(): T; peek(): T; subscribe(run: (value: T) => void): () => void; } /** * Represents a mutable signal that holds a value of type `T`. * Provides methods to get, set, and update the value. * * @template T - The type of the value held by the signal. */ export interface IMutableSignal extends ISignal { get value(): T; set value(newValue: T); set(newValue: T): void; update(updater: (value: T) => T): void; } /** * *Use the `signal` function to create a new signal.* * * The `Signal` class represents a reactive value that can be observed for changes. * It implements the `IMutableSignal` interface and provides methods to get, set, * and update the value, as well as to subscribe to changes. * * @template T - The type of the value held by the signal. */ export declare class Signal implements IMutableSignal { readonly id: string; private _value; private _subscriptions; dependents: Set; constructor(value: T); get value(): T; set value(newValue: T); peek(): T; get(): T; set(newValue: T): void; update(updater: (value: T) => T): void; subscribe(run: (value: T) => void): () => void; } /** * Use this function to run a block of code with a specific id prefix, which can be useful * for debugging and logging. It will use the specified id for the duration of the * block and then revert to the previous id. Any signals or computeds created within * the block will use the specified id prefix. * * @param id - The id prefix to use for the block of code. * @param worker - The function to run with the specified id. * @template T - The return type of the worker function. * @returns The result of the worker function. * * @usage * * ```ts * const authState = withIdPrefix("auth-state", () => signal("initial value")) * ``` */ export declare function withIdPrefix(id: string | void, worker: () => T): T; /** * *Use the `effect` function to create a new computation effect.* * * Represents a computation that runs an effect function and tracks its dependencies. */ export declare class Computation { readonly id: string; fn: EffectFunction; dependencies: Set>; private _isRunning; private _isCleaning; private _fnCleanup; onInvalidate: (() => void) | null; parentComputation: Computation | null; childComputations: Set; errorFn: EffectErrorFunction; constructor(fn: EffectFunction, parentComputation?: Computation | null, onError?: EffectErrorFunction); run(): void; invalidate(): void; cleanup(clearFromParent?: boolean): void; protected static globalErrorHandler: (error: any, source?: Computation) => void; static setGlobalErrorHandler(handler: (error: any) => void): void; static current: Computation | null; static isBatching: boolean; static stack: Computation[]; static pending: Set; } /** * *Use the `computed` function to create a new computed signal.* * * Represents a computed signal that derives its value from a function. */ export declare class ComputedSignal implements ISignal { protected fn: () => T; protected onError?: EffectErrorFunction | undefined; protected lazy: boolean; readonly id: string; private _signal; private _isEvaluated; private parentComputation; cleanup?: () => void; constructor(fn: () => T, onError?: EffectErrorFunction | undefined, lazy?: boolean); get value(): T; peek(): T; get(): T; subscribe(run: (value: T) => void): () => void; private _evaluate; } /** * Creates a new signal with the specified value. * * @param value - The initial value of the signal. * @template T - The type of the value held by the signal. * @returns A new signal with the specified value. * * @usage * ```ts * const mySignal = signal(42) * * console.log(mySignal.get()) // or mySignal.value * // => 42 * mySignal.set(100) * mySignal.update((value) => value + 1) * mySignal.subscribe((value) => console.log("Signal updated:", value)) * // => Signal updated: 101 * mySignal.peek() // untracked value access * ``` */ export declare function signal(value: T): IMutableSignal; /** * Creates a new computation effect and returns a cleanup function. * * @param fn - The effect function to be executed. * @param onError - Optional callback to handle errors that occur during the execution of the effect function. * @returns A function that, when called, cleans up the computation effect. * * @usage * ```ts * const unsubscribe = effect(() => { * const value = mySignal.value * // do something with the value, will re-run when mySignal changes * }) * ``` */ export declare function effect(fn: EffectFunction, onError?: (error: any) => void): () => void; /** * Runs the specified function without tracking dependencies. * * @param fn - The function to run without tracking dependencies. */ export declare function untracked(fn: () => void): void; /** * Creates a new computed signal that derives its value from the specified function. * * @param fn - The function to derive the value of the computed signal. * @param onError - Optional callback to handle errors that occur during the computation. * @param type - The type of the computed signal. "eager" will compute the value immediately, "lazy" will compute the value on first access. Default: "lazy" * @returns A new computed signal. * * @usage * ```ts * const myComputed = computed(() => { * return mySignal.value * 2 // will re-compute when mySignal changes * }) * mySignal.set(50) * console.log(myComputed.get()) * // => 100 * ``` */ export declare function computed(fn: () => T, onError?: (error: any) => void, type?: "eager" | "lazy"): ISignal; /** * Runs the specified function and batches any updates that occur within it. * * @param fn - The function to run within a batch. * * @usage * ```ts * batch(() => { * mySignalA.set(42) * mySignalB.set(100) * }) * // Both updates will be batched and only trigger one reactivity update. */ export declare function batch(fn: () => void): void; /** * Checks if the specified signal is a signal. * * @param signal - The signal to check. * @returns `true` if the signal is a readonly signal, otherwise `false`. */ export declare function isSignal(signal: any): signal is ISignal; /** * Checks if the specified signal is a mutable signal. * @param signal - The signal to check. * @returns `true` if the signal is a mutable signal, otherwise `false`. */ export declare function isMutableSignal(signal: any): signal is IMutableSignal; type EventCallback = (detail: T) => void; export interface SparkEvent { (callback: EventCallback): () => void; send(detail: T): void; clear(): void; } /** * Creates a new event that can be subscribed to and triggered. * * @returns A new event object. * * @usage * ```ts * const myEvent = event() * * const unsubscribe = myEvent((value) => { * console.log("Event received:", value) * }) * * myEvent.send(42) * // => Event received: 42 * ``` */ export declare function event(): SparkEvent; export {};