interface Effect { (): void; } /** * A function that is called to dispose something. */ export type TeardownHook = () => void; /** * Run a function while capturing teardown hooks. * * + If an error is thrown by the specified function, teardown hooks are called in reverse registration order and the error is re-thrown. * + If an error is thrown by a teardown hook, remaining ones are not called and the error is re-thrown. * * @param fn The function to run. * @returns A function to run all captured teardown hooks in reverse registration order. */ export declare function capture(fn: () => void): TeardownHook; /** * Run a function while capturing teardown hooks. * * + When disposed before the specified function finishes, teardown hooks are called in reverse registration order immediately after the function finishes. * + If an error is thrown by the specified function, teardown hooks are called in reverse registration order and the error is re-thrown. * + If an error is thrown by a teardown hook, remaining ones are not called and the error is re-thrown. * * @param fn The function to run. * @returns The function's return value. */ export declare function captureSelf(fn: (dispose: TeardownHook) => T): T; /** * Run a function while intentionally leaking teardown hooks. * * @param fn The function to run. * @returns The function's return value. */ export declare function leak(fn: () => T): T; /** * Run a function and immediately call teardown hooks if it throws an error. * * + If an error is thrown, teardown hooks are immediately called in reverse registration order and the error is re-thrown. * + If no error is thrown, teardown hooks are registered in the outer context. * * @param fn The function to run. * @returns The function's return value. */ export declare function teardownOnError(fn: () => T): T; /** * Register a teardown hook to be called when the current lifecycle is disposed. * * @throws An error if teardown hooks are not captured in the current context. See {@link leak} for intentionally leaking teardown hooks. * @param hook The hook to register. This may be called multiple times. */ export declare function teardown(hook: TeardownHook): void; /** * Run a function in isolation from the following side effect causing APIs: * + Teardown hooks can not be registered and cause an error. To isolate only teardown hooks, use {@link leak} instead. * + Accesses are not tracked and the default tracking behavior is restored. To only isolate accesses, use {@link untrack} instead. * * Note that contexts are not isolated. * * @param fn The function to run. * @param args The function arguments. * @returns The function's return value. */ export declare function isolate any>(fn: F, ...args: Parameters): ReturnType; /** * Internal test utility to check if lifecycle & access tracking is isolated in the current context. */ export declare function isIsolated(): boolean; /** * Fields are considered internal and not subject to semantic versioning. */ export interface StateEq { s(value: T): S; e(prev: S, next: T): boolean; } /** * Create a state equality check that uses the previous and next values. * * @param e A function that returns true if the values are equal. */ export declare function eq(e: (prev: T, next: T) => boolean): StateEq>; /** * Represents a value that changes over time. */ export declare class Signal { #private; /** * The current value without access tracking or observer notifications when set. */ inert: T; /** * Create a new signal. * * @param value The initial value. * @param eq Defines how two values are checked for equality. See {@link eq}. Default uses `Object.is`. */ constructor(value: T, eq?: StateEq, unknown>); /** * Reactively access the current value. */ get value(): T; /** * Set the current value. * * Observers only re-run when their last known value of this signal has changed. * * @example * ```tsx * import { $, watch } from "rvx"; * * const count = $(0); * * watch(count, count => { * console.log("Count:", count); * }); * * count.value++; * ``` */ set value(value: T); /** * Check if this signal has any active observers. */ get active(): boolean; /** * Manually access this signal. */ access(): void; /** * Manually notify observers. * * Note that observers might not re-run if their last known value of this signal is equal to the current one. */ notify(): void; /** * Pass this signal to a function and get its result. * * @example * ```tsx * const value = $(42); * * * ``` */ pipe(fn: (self: this, ...args: A) => R, ...args: A): R; } /** * Create a new signal. * * @param value The initial value. * @param eq Defines how two values are checked for equality. See {@link eq}. Default uses `Object.is`. */ export declare function $(value: T, eq?: StateEq, unknown>): Signal; /** * A {@link Signal} with a manually versioned state. */ export declare class ObjSignal extends Signal { #private; constructor(value: T); /** * Bump the version of this signal & notify observers. */ notify(): void; } /** * Shorthand for creating a {@link ObjSignal versioned signal}. * * @example * ```tsx * let value = 0; * const signal = $obj(); * * watch(() => { * signal.access(); * return value; * }, () => { * console.log("Value:", value); * }); * * value++; * signal.notify(); * ``` */ export declare function $obj(): ObjSignal; /** * Shorthand for creating a {@link ObjSignal versioned signal}. * * @example * ```tsx * const map = $obj(new Map()); * map.inert.set("foo", 1); * map.inert.set("bar", 2); * map.notify(); * ``` */ export declare function $obj(value: T): ObjSignal; /** * A value, signal or function to get a value. * * @example * ```tsx * import { $, watch } from "rvx"; * * const message = $("Example"); * * // Not reactive: * watch(message.value, message => { * console.log("A:", message); * }); * * // Reactive: * watch(message, message => { * console.log("B:", message); * }); * * // Reactive: * watch(() => message.value, message => { * console.log("C:", message); * }); * * message.value = "Hello World!"; * ``` */ export type Expression = Signal | (() => T) | T; /** * Utility to get the result type of an expression. */ export type ExpressionResult = T extends Expression ? R : never; /** * Utility type for expressions that should never be static values. * * This can be used instead of the {@link Expression} type in places where accepting static values doesn't make sense. */ export type Reactive = Signal | (() => T); /** * Utility type to require `T` to not be reactive. */ export type Static = unknown extends T ? never : Exclude>; /** * Watch an expression until the current lifecycle is disposed. * * + Both the expression and effect are called at least once immediately. * + Lifecycle hooks from the expression or effect are called before a signal update is processed or when the current lifecycle is disposed. * * @param expr The expression to watch. * @param effect An optional effect to call with each expression result without tracking accesses. * * @example * ```tsx * import { $, watch } from "rvx"; * * const count = $(0); * * // Capture teardown hooks registered by "watch": * const dispose = capture(() => { * // Start watching: * watch(count, count => { * console.log("Count:", count); * }); * }); * * count.value = 1; * * // Stop watching: * dispose(); * * count.value = 2; * ``` */ export declare function watch(expr: Reactive, effect: (value: NoInfer) => void): void; /** * @deprecated * This call can be removed because the expression is always static. You can call the effect directly. */ export declare function watch(expr: Static, effect: (value: NoInfer) => void): void; /** * Watch an expression until the current lifecycle is disposed. * * + Both the expression and effect are called at least once immediately. * + Lifecycle hooks from the expression or effect are called before a signal update is processed or when the current lifecycle is disposed. * * @param expr The expression to watch. * @param effect An optional effect to call with each expression result without tracking accesses. * * @example * ```tsx * import { $, watch } from "rvx"; * * const count = $(0); * * // Capture teardown hooks registered by "watch": * const dispose = capture(() => { * // Start watching: * watch(count, count => { * console.log("Count:", count); * }); * }); * * count.value = 1; * * // Stop watching: * dispose(); * * count.value = 2; * ``` */ export declare function watch(expr: () => void): void; export declare function watch(expr: Expression, effect: (value: NoInfer) => void): void; export declare function dispatch(): void; export declare function schedule(effect: Effect): void; /** * Watch an expression until the current lifecycle is disposed. * * @param expr The expression to watch. * @param effect An effect to call with each subsequent expression result without tracking accesses. * @returns The first expression result. */ export declare function watchUpdates(expr: Reactive, effect: (value: NoInfer) => void): T; /** * @deprecated * This call can be removed because the expression is always static. */ export declare function watchUpdates(expr: Static, effect: (value: NoInfer) => void): T; /** * Watch an expression until the current lifecycle is disposed. * * @param expr The expression to watch. * @param effect An effect to call with each subsequent expression result without tracking accesses. * @returns The first expression result. */ export declare function watchUpdates(expr: Expression, effect: (value: NoInfer) => void): T; /** * Wrap an expression to re-run only when any accessed observable has changed. * * + The {@link Context} from where `computed` was called is available in the expression. * + Teardown hooks in the expression are not supported. * + The expression only re-runs when the result is accessed after any used observable has changed. * + Observers only re-run when the result is not equal to the previous one. * + The expression may re-run before an accessing observer while that observer checks if it should re-run. * * @param expr The expression to wrap. * @param eq Defines how two results are checked for equality. See {@link eq}. Default uses `Object.is`. * @returns The wrapped expression. * * @example * ```tsx * import { $, computed } from "rvx"; * * const a = $(2); * const b = $(3); * * // This computed always returns the sum of the current signal values: * const sum = computed(() => a.value + b.value); * ``` */ export declare function computed(expr: Reactive, eq?: StateEq, any>): () => T; /** * @deprecated * This call can be removed because the expression is always static. You can replace it with a function that returns the value directly. */ export declare function computed(expr: Static, eq?: StateEq, any>): () => T; /** * Wrap an expression to re-run only when any accessed observable has changed. * * + The {@link Context} from where `computed` was called is available in the expression. * + Teardown hooks in the expression are not supported. * + The expression only re-runs when the result is accessed after any used observable has changed. * + Observers only re-run when the result is not equal to the previous one. * + The expression may re-run before an accessing observer while that observer checks if it should re-run. * * @param expr The expression to wrap. * @param eq Defines how two results are checked for equality. See {@link eq}. Default uses `Object.is`. * @returns The wrapped expression. * * @example * ```tsx * import { $, computed } from "rvx"; * * const a = $(2); * const b = $(3); * * // This computed always returns the sum of the current signal values: * const sum = computed(() => a.value + b.value); * ``` */ export declare function computed(expr: Expression, eq?: StateEq, any>): () => T; /** * Wrap an expression that modifies a value in place to re-run only when any accessed observable has changed. * * + The {@link Context} from where `computedObj` was called is available in the expression. * + Teardown hooks in the expression are not supported. * + The expression only re-runs when the result is accessed after any used observable has changed. * + Observers only re-run if `update` has been called by the expression since their last access. * + The expression may re-run before an accessing observer while that observer checks if it should re-run. * * @param expr The expression to compute or update the current state. The first argument is a function to call when the computed state has changed. * @param value The initial value. * * @example * ```tsx * import { $, computedObj } from "rvx"; * * const words = $(["example", "foo", "bar"]); * * const lengths = computedObj((update, value: number[]) => { * const current = words.value; * let changed = false; * for (let i = 0; i < current.length; i++) { * if (value[i] !== current[i].length) { * value[i] = current[i].length; * changed = true; * } * } * if (value.length !== current.length) { * value.length = current.length; * changed = true; * } * if (changed) { * update(value); * } * }, []); * ``` */ export declare function computedObj(expr: (update: (next: T) => void, value: T) => void, value: T): () => T; /** * Wrap an expression that modifies a value in place to re-run only when any accessed observable has changed. * * + The {@link Context} from where `computedObj` was called is available in the expression. * + Teardown hooks in the expression are not supported. * + The expression only re-runs when the result is accessed after any used observable has changed. * + Observers only re-run if `update` has been called by the expression since their last access. * + The expression may re-run before an accessing observer while that observer checks if it should re-run. * * @param expr The expression to compute or update the current state. The first argument is a function to call when the computed state has changed. * @param value The initial value. * * @example * ```tsx * import { $, computedObj } from "rvx"; * * const words = $(["example", "foo", "bar"]); * * const lengths = computedObj((update, value: number[]) => { * const current = words.value; * let changed = false; * for (let i = 0; i < current.length; i++) { * if (value[i] !== current[i].length) { * value[i] = current[i].length; * changed = true; * } * } * if (value.length !== current.length) { * value.length = current.length; * changed = true; * } * if (changed) { * update(value); * } * }, []); * ``` */ export declare function computedObj(expr: (update: (next: T | undefined) => void, value: T | undefined) => void): () => T | undefined; /** * {@link get Evaluate an expression} without tracking accesses. * * @param expr The expression to evaluate. * @returns The function's return value. * * @example * ```tsx * import { $, untrack, watch } from "rvx"; * * const a = $(2); * const b = $(3); * * watch(() => a.value + untrack(b), sum => { * console.log("Sum:", sum); * }); * * // This causes an update: * a.value = 4; * * // This has no effect: * b.value = 5; * ``` */ export declare function untrack(expr: Reactive): T; /** * @deprecated * This call can be removed because the expression is always static. You can use the value directly. */ export declare function untrack(expr: Static): T; /** * {@link get Evaluate an expression} without tracking accesses. * * @param expr The expression to evaluate. * @returns The function's return value. * * @example * ```tsx * import { $, untrack, watch } from "rvx"; * * const a = $(2); * const b = $(3); * * watch(() => a.value + untrack(b), sum => { * console.log("Sum:", sum); * }); * * // This causes an update: * a.value = 4; * * // This has no effect: * b.value = 5; * ``` */ export declare function untrack(expr: Expression): T; /** * Check if accesses are currently tracked. */ export declare function isTracking(): boolean; /** * Evaluate an expression while tracking accesses to schedule the trigger effect when any accessed observable has changed. * * See {@link trigger}. */ export interface TriggerPipe { (expr: Reactive): T; /** * @deprecated * This call can be removed because the expression is always static. You can use the value directly. */ (expr: Static): T; (expr: Expression): T; (): void; } /** * Create a pipe for evaluating expressions that schedules an effect once when any accessed observables from the latest evaluated expression have changed. * * + The effect only runs when any accessed observable has changed. * + When the lifecycle at which the pipe was created is disposed, the effect will not be called anymore. * + It is guaranteed that the effect is called before any outer observers like {@link watch} or {@link watchUpdates} are notified. * + If pipes are nested, the effect for the most inner one is called first. * * @param callback The effect to schedule. * @returns The pipe to evaluate expressions. */ export declare function trigger(callback: () => void): TriggerPipe; /** * Manually evaluate an expression in the current context. * * This can be used to access reactive and non reactive inputs. * * @param expr The expression to evaluate. * @returns The expression result. * * @example * ```tsx * import { $, get } from "rvx"; * * const count = $(42); * * get(42) // 42 * get(count) // 42 * get(() => 42) // 42 * ``` */ export declare function get(expr: Reactive): T; /** * @deprecated * This call can be removed because the expression is always static. You can use the value directly. */ export declare function get(expr: Static): T; /** * Manually evaluate an expression in the current context. * * This can be used to access reactive and non reactive inputs. * * @param expr The expression to evaluate. * @returns The expression result. * * @example * ```tsx * import { $, get } from "rvx"; * * const count = $(42); * * get(42) // 42 * get(count) // 42 * get(() => 42) // 42 * ``` */ export declare function get(expr: Expression): T; export type MapFn = (input: I) => O; /** * Map an expression value while preserving if the expression is static or not. * * @example * ```tsx * import { $, map, get } from "rvx"; * * const count = $(42); * const doubleCount = map(count, value => value * 2); * * get(doubleCount) // 84 * ``` */ export declare function map(input: Reactive, mapFn: MapFn, O>): Expression; /** * @deprecated * This call can be removed because the input is always static. You can call the map function directly. */ export declare function map(input: Static, mapFn: MapFn, O>): Expression; /** * Map an expression value while preserving if the expression is static or not. * * @example * ```tsx * import { $, map, get } from "rvx"; * * const count = $(42); * const doubleCount = map(count, value => value * 2); * * get(doubleCount) // 84 * ``` */ export declare function map(input: Expression, mapFn: MapFn, O>): Expression; export {}; //# sourceMappingURL=signals.d.ts.map