react-understate
Version:
A lightweight, reactive signals library for React 18+ with automatic dependency tracking and optimized performance
71 lines (64 loc) • 3.01 kB
TypeScript
type DebugOptions = {
enabled?: boolean;
logger?: (message: string, ...args: any[]) => void;
showFile?: boolean;
};
interface State<T> {
readonly rawValue: T;
update(fn: (prev: T) => T | Promise<T>): Promise<void>;
subscribe(fn: () => void): () => void;
get value(): T;
set value(newValue: T | ((prev: T) => T | Promise<T>));
get requiredValue(): NonNullable<T>;
set requiredValue(newValue: NonNullable<T>);
}
declare function configureDebug(options?: DebugOptions): Readonly<DebugOptions>;
declare function action<T extends (...args: any[]) => any>(fn: T, name?: string): T;
declare function state<T>(initialValue: T, name?: string): State<T>;
declare function batch(fn: () => void, name?: string): void;
declare function findUserCodeLine(stack: string): string | undefined;
declare function parseStackLine(line: string): {
file: string;
line: string;
col: string;
} | null;
declare function createFileLocation(file: string, line: string, col: string): string;
declare function getFileLocationStyle(): string;
declare function logDebug(message: string, debugConfig: {
enabled?: boolean;
logger?: (message: string, ...args: any[]) => void;
showFile?: boolean;
}): void;
declare function derived<T>(computeFn: () => T, name?: string): State<T>;
declare function asyncDerived<T>(computeFn: () => Promise<T>, name?: string): State<Promise<T>>;
type EffectOptions = {
once?: boolean;
preventOverlap?: boolean;
preventLoops?: boolean;
};
declare function effect(fn: () => void | (() => void) | Promise<void> | Promise<() => void>, name?: string, options?: EffectOptions): () => void;
type ExtractStateValues<T> = {
[K in keyof T]: T[K] extends State<infer U> ? U : T[K];
};
declare function useUnderstate<T extends readonly State<unknown>[]>(...signals: T): {
[K in keyof T]: T[K] extends State<infer U> ? U : never;
};
declare function useUnderstate<T extends Record<string, unknown>>(store: T): ExtractStateValues<T>;
declare function setReact(_reactModule: unknown): void;
declare function persistLocalStorage<T>(state: State<T>, key: string, options?: {
loadInitial?: boolean;
syncAcrossTabs?: boolean;
serialize?: (value: T) => string;
deserialize?: (value: string) => T;
onError?: (error: Error) => void;
}): () => void;
declare function persistSessionStorage<T>(state: State<T>, key: string, options?: {
loadInitial?: boolean;
syncAcrossTabs?: boolean;
serialize?: (value: T) => string;
deserialize?: (value: string) => T;
onError?: (error: Error) => void;
}): () => void;
declare function persistStates<T extends Record<string, State<unknown>>>(states: T, keyPrefix: string, storage?: Storage): () => void;
export { action, asyncDerived, batch, configureDebug, createFileLocation, derived, effect, findUserCodeLine, getFileLocationStyle, logDebug, parseStackLine, persistLocalStorage, persistSessionStorage, persistStates, setReact, state, useUnderstate };
export type { State };