@lifeart/gxt
Version:
<img align="right" width="95" height="95" alt="Philosopher’s stone, logo of PostCSS" src="./public/logo.png">
46 lines (45 loc) • 1.49 kB
TypeScript
/**
* Reactive Reference - a Proxy-based wrapper for lazy value evaluation.
*
* This provides an alternative to the `unwrap()` function approach.
* Instead of checking if a value is a function and calling it,
* we wrap reactive values in a Proxy that auto-evaluates on access.
*/
export declare const IS_REF: unique symbol;
export declare const REF_GETTER: unique symbol;
/**
* Check if a value is a Ref proxy
*/
export declare function isRef(value: unknown): value is RefProxy;
/**
* The type of a Ref proxy
*/
export interface RefProxy {
readonly [IS_REF]: true;
readonly [REF_GETTER]: () => unknown;
readonly value: unknown;
valueOf(): unknown;
toString(): string;
[Symbol.toPrimitive](hint: string): unknown;
}
/**
* Create a reactive reference that lazily evaluates its value.
*
* @param getter - A function that returns the current value, or a static value
* @returns A Proxy that auto-evaluates on property access
*
* @example
* ```typescript
* const r = ref(() => this.count);
* console.log(r.value); // Evaluates getter, returns current count
* if (r) { ... } // r is always truthy (it's an object)
* ```
*/
export declare function ref(getter: (() => unknown) | unknown): RefProxy;
/**
* Unwrap a Ref or return the value as-is.
* This is the recommended way to get a value that might be a Ref.
*
* IMPORTANT: Does NOT recurse to avoid calling user callback functions.
*/
export declare function deref(value: unknown): unknown;