/** * @file Idle Scheduler * @description Orchestrates non-critical work during browser idle periods using * requestIdleCallback with fallback support. Enables priority-based task scheduling. * * Features: * - requestIdleCallback orchestration * - Priority-based task queue * - Time-sliced execution * - Deadline-aware scheduling * - Task cancellation * - Progress tracking */ /** * Task priority */ export type IdleTaskPriority = 'critical' | 'high' | 'normal' | 'low'; /** * Task status */ export type IdleTaskStatus = 'pending' | 'running' | 'completed' | 'cancelled' | 'failed'; /** * Idle task definition */ export interface IdleTask { id: string; name: string; priority: IdleTaskPriority; work: (deadline: IdleDeadline) => T | Promise; timeout?: number; onComplete?: (result: T) => void; onError?: (error: Error) => void; onProgress?: (progress: number) => void; } /** * Scheduler configuration */ export interface IdleSchedulerConfig { /** Maximum tasks to process per idle callback */ maxTasksPerIdle: number; /** Default task timeout in ms */ defaultTimeout: number; /** Minimum remaining time to start a task (ms) */ minRemainingTime: number; /** Enable fallback for browsers without requestIdleCallback */ enableFallback: boolean; /** Fallback check interval in ms */ fallbackInterval: number; /** Debug mode */ debug: boolean; } /** * Scheduler statistics */ export interface SchedulerStats { totalTasks: number; pendingTasks: number; completedTasks: number; failedTasks: number; cancelledTasks: number; averageWaitTime: number; averageExecutionTime: number; } /** * IdleDeadline interface (for browsers without native support) */ export interface IdleDeadline { didTimeout: boolean; timeRemaining(): number; } /** * Manages idle-time task execution */ export declare class IdleScheduler { private config; private taskQueue; private idleCallbackId; private fallbackTimer; private isRunning; private taskIdCounter; private completionCallbacks; constructor(config?: Partial); /** * Schedule a task to run during idle time */ schedule(work: (deadline: IdleDeadline) => T | Promise, options?: Partial, 'id' | 'work'>>): string; /** * Schedule a task and return a promise for its completion */ scheduleAsync(work: (deadline: IdleDeadline) => T | Promise, options?: Partial, 'id' | 'work'>>): Promise; /** * Cancel a scheduled task */ cancel(taskId: string): boolean; /** * Cancel all pending tasks */ cancelAll(): number; /** * Get task status */ getTaskStatus(taskId: string): IdleTaskStatus | null; /** * Get scheduler statistics */ getStats(): SchedulerStats; /** * Start the scheduler (called automatically when tasks are added) */ start(): void; /** * Stop the scheduler */ stop(): void; /** * Clear all tasks and statistics */ clear(): void; private ensureSchedulerRunning; private scheduleIdleCallback; private processIdleCallback; private processFallback; private executeTask; private getPendingTasksSorted; private hasPendingTasks; private getMinTaskTimeout; private hasIdleCallback; private cancelIdleCallback; private generateTaskId; private log; } /** * Get or create the global idle scheduler instance */ export declare function getIdleScheduler(config?: Partial): IdleScheduler; /** * Reset the scheduler instance */ export declare function resetIdleScheduler(): void; /** * Execute work during idle time (convenience function) */ export declare function runWhenIdle(work: (deadline: IdleDeadline) => T | Promise, options?: Partial, 'id' | 'work'>>): Promise; /** * Defer work to idle time without waiting for result */ export declare function deferToIdle(work: (deadline: IdleDeadline) => T | Promise, options?: Partial, 'id' | 'work'>>): string; /** * Create a yielding iterator for large workloads */ export declare function createYieldingIterator(items: T[], processItem: (item: T) => void, deadline: IdleDeadline): Generator; /** * Process items in chunks during idle time */ export declare function processInIdleChunks(items: T[], processItem: (item: T) => void | Promise, chunkSize?: number): Promise; /** * Debounce to idle time */ export declare function debounceToIdle void>(fn: T, options?: Partial>): (...args: Parameters) => void;