UNPKG

kinetic-slider

Version:

A WebGL-powered kinetic slider component using PIXI.js

239 lines (238 loc) 7.58 kB
/** * @file RenderScheduler.ts * @description Provides centralized render scheduling and batching for optimized UI updates. * Coordinates the timing of visual effects to minimize rendering overhead and improve performance. */ import { UpdateType } from './UpdateTypes'; import { type ThrottlerConfig } from './FrameThrottler'; /** * Priority levels for rendering updates. * Higher numbers indicate higher priority. * @enum {number} */ export declare enum UpdatePriority { /** Background, non-visual updates */ LOW = 0, /** Standard visual updates */ NORMAL = 1, /** Important visual feedback */ HIGH = 2, /** Must-run-immediately updates */ CRITICAL = 3 } /** * Interface for update tasks managed by the scheduler. * @interface */ export interface UpdateTask { /** Unique identifier for the task */ id: string; /** Function to execute when the task runs */ callback: () => void; /** Priority level determining execution order */ priority: UpdatePriority; /** Timestamp when the task was added to the queue */ timestamp: number; } /** * Coordinates and batches render updates for KineticSlider. * This helps reduce unnecessary render cycles by grouping related updates * and executing them at the optimal time. * * @example * ```typescript * // Get the scheduler instance * const scheduler = RenderScheduler.getInstance(); * * // Schedule a normal priority update * scheduler.scheduleUpdate('my-component-update', () => { * // Update logic here * }); * * // Schedule a high priority update with a specific update type * scheduler.scheduleTypedUpdate('my-component', UpdateType.MOUSE_RESPONSE, () => { * // Mouse response update logic * }); * ``` */ export declare class RenderScheduler { /** Singleton instance of the scheduler */ private static instance; /** Map of task IDs to pending update tasks */ private updateQueue; /** Whether the processing loop is active */ private isProcessing; /** Current requestAnimationFrame ID or null if not active */ private rafId; /** Timestamp of the last frame execution */ private lastFrameTime; /** Minimum time between frames in milliseconds (~60fps default) */ private frameThrottle; /** Frame throttler for advanced timing control */ private frameThrottler; /** * Get the singleton instance of the RenderScheduler. * @returns {RenderScheduler} The singleton instance */ static getInstance(): RenderScheduler; /** * Private constructor for singleton pattern. * @private */ private constructor(); /** * Schedule a task for execution. * If a task with the same ID already exists, it will be replaced. * * @param {string} id - Unique identifier for the task * @param {() => void} callback - Function to execute * @param {UpdatePriority} [priority=UpdatePriority.NORMAL] - Priority level * @returns {RenderScheduler} The scheduler instance for chaining * * @example * ```typescript * scheduler.scheduleUpdate('update-text', () => { * element.textContent = 'Updated text'; * }, UpdatePriority.NORMAL); * ``` */ scheduleUpdate(id: string, callback: () => void, priority?: UpdatePriority): RenderScheduler; /** * Schedule an update using a standard update type. * This provides a more semantic API for scheduling updates. * * @param {string} componentId - ID of the component requesting the update * @param {UpdateType} updateType - Type of update * @param {() => void} callback - Function to execute * @param {string} [suffix] - Optional suffix for the update ID * @returns {RenderScheduler} The scheduler instance for chaining * * @example * ```typescript * scheduler.scheduleTypedUpdate( * 'slider', * UpdateType.SLIDE_TRANSFORM, * () => updateSlidePosition() * ); * ``` */ scheduleTypedUpdate(componentId: string, updateType: UpdateType, callback: () => void, suffix?: string): RenderScheduler; /** * Cancel a scheduled update. * * @param {string} id - ID of the task to cancel * @returns {boolean} True if a task was found and removed * * @example * ```typescript * const wasRemoved = scheduler.cancelUpdate('update-text'); * console.log(`Update was ${wasRemoved ? 'successfully' : 'not'} canceled`); * ``` */ cancelUpdate(id: string): boolean; /** * Cancel an update that was scheduled with a standard update type. * * @param {string} componentId - ID of the component that scheduled the update * @param {UpdateType} updateType - Type of update to cancel * @param {string} [suffix] - Optional suffix from the update ID * @returns {boolean} True if a task was found and removed * * @example * ```typescript * scheduler.cancelTypedUpdate('slider', UpdateType.SLIDE_TRANSFORM); * ``` */ cancelTypedUpdate(componentId: string, updateType: UpdateType, suffix?: string): boolean; /** * Immediately execute a task with CRITICAL priority. * Bypasses the queue but still respects frame throttling. * * @param {() => void} callback - Function to execute immediately * * @example * ```typescript * scheduler.executeImmediate(() => { * // Handle urgent user input * processCriticalUserAction(); * }); * ``` */ executeImmediate(callback: () => void): void; /** * Start the processing loop if not already running. * @private */ private startProcessing; /** * Process the queue using requestAnimationFrame for timing. * @private */ private processQueue; /** * Execute all queued tasks in priority order. * @private */ private executeQueuedTasks; /** * Set the frame throttle rate. * * @param {number} milliseconds - Minimum time between frames * * @example * ```typescript * // Set to 30fps (33.33ms between frames) * scheduler.setFrameThrottle(33.33); * ``` */ setFrameThrottle(milliseconds: number): void; /** * Get current queue size for debugging. * * @returns {number} Number of tasks currently in the queue * * @example * ```typescript * const queueSize = scheduler.getQueueSize(); * console.log(`Current queue size: ${queueSize}`); * ``` */ getQueueSize(): number; /** * Clear all pending updates. * * @example * ```typescript * // Cancel all pending updates (e.g., when component unmounts) * scheduler.clearQueue(); * ``` */ clearQueue(): void; /** * Configure frame throttling behavior. * * @param {ThrottlerConfig} config - Throttling configuration options * * @example * ```typescript * scheduler.configureThrottling({ * targetFps: 30, * strategy: ThrottleStrategy.ADAPTIVE * }); * ``` */ configureThrottling(config: Partial<ThrottlerConfig>): void; /** * Get current performance metrics. * * @returns {Object} Performance metrics including queue size and frame rate * * @example * ```typescript * const metrics = scheduler.getPerformanceMetrics(); * console.log(`Current FPS: ${metrics.currentFps}`); * ``` */ getPerformanceMetrics(): object; } export default RenderScheduler;