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.

58 lines (57 loc) 2.59 kB
/** * useWorkerNotifications – listen to worker messages and maintain running state, counts, history, and derived stats. * @module useWorkerNotifications */ /** Supported worker event types for tracking. Worker should postMessage with these shapes. */ export type WorkerEventType = "task_start" | "task_end" | "task_fail" | "queue_size"; export interface WorkerNotificationEvent { type: WorkerEventType; taskId?: string; duration?: number; error?: string; size?: number; timestamp: number; } export interface UseWorkerNotificationsOptions { /** Max events to keep in history. Default 100. */ maxHistory?: number; /** Window in ms for throughput calculation (completed per second). Default 1000. */ throughputWindowMs?: number; } export interface UseWorkerNotificationsReturn { /** Task IDs currently running (received task_start, not yet task_end/task_fail). */ runningTasks: string[]; /** Total tasks that completed successfully. */ completedCount: number; /** Total tasks that failed. */ failedCount: number; /** Recent events (oldest first), capped at maxHistory. */ eventHistory: WorkerNotificationEvent[]; /** Average task duration in ms (from task_end events that include duration). */ averageDurationMs: number; /** Completed tasks per second over the throughput window. */ throughputPerSecond: number; /** Last reported queue size (from queue_size events); 0 if never sent. */ currentQueueSize: number; /** Default view: all active worker data and progress in one object. */ progress: { runningTasks: string[]; completedCount: number; failedCount: number; averageDurationMs: number; throughputPerSecond: number; currentQueueSize: number; totalProcessed: number; recentEventCount: number; }; } /** * Listens to a Worker's messages and maintains state: running tasks, completed/failed counts, * event history, execution time per task, average duration, throughput per second, and queue size. * Worker should postMessage with: { type: 'task_start'|'task_end'|'task_fail'|'queue_size', taskId?, duration?, error?, size? }. * * @param worker - The Worker instance to listen to, or null/undefined to listen to nothing. * @param options - Optional maxHistory and throughputWindowMs. * @returns State and derived stats plus a default progress object. */ export declare function useWorkerNotifications(worker: Worker | null | undefined, options?: UseWorkerNotificationsOptions): UseWorkerNotificationsReturn;