mancha
Version:
Javscript HTML rendering engine
103 lines (102 loc) • 4.43 kB
TypeScript
/**
* Internal store properties that are always present. These are managed by the framework
* and should not be set directly by users.
*/
export type InternalStoreState = {
/** Reference to the parent store in a hierarchy. */
$parent?: SignalStore;
/** Reference to the root renderer instance. */
$rootRenderer?: SignalStore;
/** Reference to the root DOM node. */
$rootNode?: Node;
} & {
[key: `$$${string}`]: string | null;
};
/**
* Base type for user-defined store state. Uses `any` intentionally to allow flexible
* user-defined state types without requiring explicit index signatures.
*/
export type StoreState = Record<string, any>;
/** Type for expression evaluation function. */
type EvalFunction = (thisArg: SignalStoreProxy, args: Record<string, unknown>) => unknown;
/**
* Internal proxy type used within the store implementation. Uses `any` for dynamic property access.
*/
type SignalStoreProxy = SignalStore & InternalStoreState & {
[key: string]: any;
};
type Observer<T> = (this: SignalStoreProxy) => T;
type KeyValueHandler = (this: SignalStoreProxy, key: string, value: unknown) => void;
declare abstract class IDebouncer {
timeouts: Map<(...args: unknown[]) => unknown, ReturnType<typeof setTimeout>>;
debounce<T>(millis: number, callback: () => T | Promise<T>): Promise<T>;
}
/** Default debouncer time in millis. */
export declare const REACTIVE_DEBOUNCE_MILLIS = 10;
export declare function getAncestorValue(store: SignalStore | null, key: string): unknown;
export declare function getAncestorKeyStore(store: SignalStore | null, key: string): SignalStore | null;
export declare function setAncestorValue(store: SignalStore, key: string, value: unknown): void;
export declare function setNestedProperty(obj: Record<string, unknown>, path: string, value: unknown): void;
export declare class SignalStore<T extends StoreState = StoreState> extends IDebouncer {
protected readonly evalkeys: string[];
protected readonly expressionCache: Map<string, EvalFunction>;
protected readonly observers: Map<string, Set<Observer<unknown>>>;
protected readonly keyHandlers: Map<RegExp, Set<KeyValueHandler>>;
protected _observer: Observer<unknown> | null;
readonly _store: Map<string, unknown>;
_lock: Promise<void>;
constructor(data?: T);
private wrapFunction;
private wrapObject;
watch<T>(key: string, observer: Observer<T>): void;
addKeyHandler(pattern: RegExp, handler: KeyValueHandler): void;
notify(key: string, debounceMillis?: number): Promise<void>;
get<T>(key: string, observer?: Observer<T>): unknown;
set(key: string, value: unknown): Promise<void>;
del(key: string): Promise<void>;
keys(): string[];
/**
* Checks if a key exists in THIS store only (not ancestors).
* Use `get(key) !== null` to check if a key exists anywhere in the chain.
*/
has(key: string): boolean;
effect<T>(observer: Observer<T>): T;
private proxify;
get $(): SignalStore<T> & InternalStoreState & T;
/**
* Creates an evaluation function for the provided expression.
* @param expr The expression to be evaluated.
* @returns The evaluation function.
*/
private makeEvalFunction;
/**
* Retrieves or creates a cached expression function for the provided expression.
* @param expr - The expression to retrieve or create a cached function for.
* @returns The cached expression function.
*/
private cachedExpressionFunction;
eval(expr: string, args?: Record<string, unknown>): unknown;
/**
* Executes an async function and returns a reactive state object that tracks the result.
*
* @param fn - The async function to execute.
* @param options - Optional arguments to pass to the function.
* @returns A reactive state object with $pending, $result, and $error properties.
*
* @example
* // In :data attribute - executes on mount
* :data="{ users: $resolve(api.listUsers) }"
*
* // With options
* :data="{ user: $resolve(api.getUser, { path: { id: userId } }) }"
*
* // In :on:click - executes on click
* :on:click="result = $resolve(api.deleteUser, { path: { id } })"
*/
$resolve<T, O = unknown>(fn: (options?: O) => Promise<T>, options?: O): {
$pending: boolean;
$result: T | null;
$error: Error | null;
};
}
export {};