UNPKG

flexium

Version:

A lightweight, signals-based UI framework with cross-platform renderers

37 lines (35 loc) 1.38 kB
type StateGetter<T> = { (): T; loading: boolean; error: any; state: 'unresolved' | 'pending' | 'ready' | 'refreshing' | 'errored'; latest: T | undefined; read: () => T | undefined; map: T extends (infer U)[] ? (fn: (item: U, index: () => number) => any) => any : never; }; type StateSetter<T> = { (newValue: T | ((prev: T) => T)): void; mutate: (v: T | undefined) => void; refetch: () => void; }; /** * Unified State API * * Usage: * 1. Local Value: const [count, setCount] = state(0); * 2. Local Resource: const [data, actions] = state(async () => fetch('/api').then(r => r.json())); * 3. Global Value: const [theme, setTheme] = state('light', { key: 'theme' }); * 4. Global Resource: const [user, actions] = state(fetchUser, { key: 'user' }); * 5. Computed (Derived): const [double] = state(() => count() * 2); * * @param initialValueOrFetcher - Initial value, or a function to derive/fetch state. * @param options - Optional settings (e.g., global key). */ declare function state<T>(initialValueOrFetcher: T | ((...args: any[]) => T | Promise<T>), options?: { key?: string; }): [StateGetter<T>, StateSetter<T>]; /** * Clear all global states (useful for testing or resetting app) */ declare function clearGlobalState(): void; export { type StateGetter as S, type StateSetter as a, clearGlobalState as c, state as s };