UNPKG

preact-missing-hooks

Version:

A lightweight, extendable collection of missing React-like hooks for Preact — plus fresh, powerful new ones designed specifically for modern Preact apps.

37 lines (36 loc) 1.77 kB
export type ThreadedWorkerMode = "sequential" | "parallel"; export interface UseThreadedWorkerOptions { /** Sequential: single worker, priority-ordered. Parallel: worker pool. */ mode: ThreadedWorkerMode; /** Max concurrent workers. Only used when mode is "parallel". Default 4. */ concurrency?: number; } export interface RunOptions { /** 1 = highest priority. Lower number runs first. FIFO within same priority. */ priority?: number; } export interface UseThreadedWorkerReturn<TData, TResult> { /** Enqueue work. Returns a Promise that resolves with the worker result. */ run: (data: TData, options?: RunOptions) => Promise<TResult>; /** True while any task is queued or running. */ loading: boolean; /** Result of the most recently completed successful task. */ result: TResult | undefined; /** Error from the most recently failed task. */ error: unknown; /** Number of tasks currently queued + running. */ queueSize: number; /** Clear all pending (not yet started) tasks. Running tasks continue. */ clearQueue: () => void; /** Stop accepting new work and clear pending queue. Running tasks finish. */ terminate: () => void; } /** * Production-grade hook to run async work in a queue with optional priority * and either sequential or parallel execution. * * @param workerFn - Async function to run for each task (e.g. API call, heavy compute). * @param options - mode: "sequential" | "parallel", concurrency (parallel only). * @returns run, loading, result, error, queueSize, clearQueue, terminate. */ export declare function useThreadedWorker<TData, TResult>(workerFn: (data: TData) => Promise<TResult>, options: UseThreadedWorkerOptions): UseThreadedWorkerReturn<TData, TResult>;