UNPKG

stunk

Version:

Stunk is a lightweight, framework-agnostic state management library for JavaScript and TypeScript. It uses chunk-based state units for efficient updates, reactivity, and performance optimization in React, Vue, Svelte, and Vanilla JS/TS applications.

83 lines (72 loc) 3.2 kB
import { C as Chunk } from '../core-C7jFiHdO.js'; /** * A lightweight hook that subscribes to a chunk and returns its current value, along with setters, selector, reset and destroy. * Ensures reactivity and prevents unnecessary re-renders. */ declare function useChunk<T, S = T>(chunk: Chunk<T>, selector?: (value: T) => S): readonly [S, (valueOrUpdater: T | ((currentValue: T) => T)) => void, () => void, () => void]; /** * A hook for creating a read-only derived value from a chunk. * Ensures reactivity and updates when the source chunk changes. */ declare function useDerive<T, D>(chunk: Chunk<T>, fn: (value: T) => D): D; type ChunkValue<T> = T extends Chunk<infer U> ? U : never; type DependencyValues<T extends Chunk<any>[]> = { [K in keyof T]: T[K] extends Chunk<any> ? ChunkValue<T[K]> : never; }; /** * A hook that computes a value based on multiple chunks. * Automatically re-computes when any dependency changes. */ declare function useComputed<TDeps extends Chunk<any>[], TResult>(dependencies: [...TDeps], computeFn: (...args: DependencyValues<TDeps>) => TResult): TResult; /** * A lightweight hook that subscribes to a chunk and returns only its current value. * Useful for read-only components that don't need to update the chunk. */ declare function useChunkValue<T, S = T>(chunk: Chunk<T>, selector?: (value: T) => S): S; /** * A hook that subscribes to a specific property of a chunk. * This optimizes renders by only updating when the selected property changes. */ declare function useChunkProperty<T, K extends keyof T>(chunk: Chunk<T>, property: K): T[K]; /** * Hook to read values from multiple chunks at once. * Only re-renders when any of the chunk values change. */ declare function useChunkValues<T extends Chunk<any>[]>(chunks: [...T]): { [K in keyof T]: T[K] extends Chunk<infer U> ? U : never; }; interface AsyncState<T, E extends Error> { loading: boolean; error: E | null; data: T | null; lastFetched?: number; } interface AsyncChunk<T, E extends Error = Error> extends Chunk<AsyncState<T, E>> { /** Reload the data from the source. */ reload: () => Promise<void>; /** Smart refresh - respects stale time */ refresh: () => Promise<void>; /** Mutate the data directly. */ mutate: (mutator: (currentData: T | null) => T) => void; /** Reset the state to the initial value. */ reset: () => void; /** Clean up intervals and timeouts */ cleanup: () => void; } /** * A hook that handles asynchronous state with built-in reactivity. * Provides loading, error, and data states with full asyncChunk functionality. */ declare function useAsyncChunk<T, E extends Error = Error, P extends any[] = []>(asyncChunk: AsyncChunk<T, E> | (AsyncChunk<T, E> & { setParams: (...params: P) => void; }), params?: P): { data: T | null; loading: boolean; error: E | null; lastFetched: number | undefined; reload: (...params: any[]) => any; refresh: (...params: any[]) => any; mutate: (mutator: (currentData: T | null) => T) => void; reset: () => void; }; export { useAsyncChunk, useChunk, useChunkProperty, useChunkValue, useChunkValues, useComputed, useDerive };