UNPKG

kinetic-slider

Version:

A WebGL-powered kinetic slider component using PIXI.js

189 lines (188 loc) 5.79 kB
import { type EnhancedSprite } from '../types'; import ResourceManager from './ResourceManager'; /** * Configuration options for the SlidingWindowManager */ export interface SlidingWindowOptions { /** Number of slides to keep loaded on each side of the current slide */ windowSize?: number; /** Whether to enable debug logging */ debug?: boolean; /** Total number of slides in the slider */ totalSlides: number; /** Initial active slide index */ initialIndex?: number; } /** * Slide initialization state */ export declare enum SlideState { /** Slide is not initialized at all */ UNINITIALIZED = "uninitialized", /** Slide has a lightweight placeholder */ PLACEHOLDER = "placeholder", /** Slide is fully loaded and ready for display */ LOADED = "loaded", /** Slide is currently visible */ ACTIVE = "active", /** Slide failed to load */ ERROR = "error" } /** * Information about a slide's current state */ export interface SlideInfo { /** Index of the slide */ index: number; /** Current state of the slide */ state: SlideState; /** Reference to the sprite if available */ sprite?: EnhancedSprite; /** Whether the slide is within the current visibility window */ inWindow: boolean; /** Last time this slide was active */ lastActiveTime?: number; } /** * Manages a sliding window of initialized slides to optimize memory usage * and improve performance by only fully loading slides that are visible * or likely to become visible soon. */ export default class SlidingWindowManager { /** Current active slide index */ private activeIndex; /** Size of the window on each side of the active slide */ private windowSize; /** Total number of slides */ private totalSlides; /** Information about each slide */ private slides; /** Whether debug logging is enabled */ private debug; /** Resource manager for tracking resources */ private resourceManager?; /** Direction of the last navigation (-1 for prev, 1 for next, 0 for initial) */ private lastDirection; /** * Creates a new SlidingWindowManager * * @param options - Configuration options * @param resourceManager - Optional ResourceManager for resource tracking */ constructor(options: SlidingWindowOptions, resourceManager?: ResourceManager); /** * Log a message if debug is enabled */ private log; /** * Check if a slide index is within the current window * * @param index - Slide index to check * @param centerIndex - Center of the window (usually activeIndex) * @returns Whether the index is within the window */ private isInWindow; /** * Get all slide indices that should be in the current window * * @returns Array of slide indices in the window */ getWindowIndices(): number[]; /** * Get all slide indices that should be in the extended window * (includes prediction based on navigation direction) * * @returns Array of slide indices in the extended window */ getExtendedWindowIndices(): number[]; /** * Update the active slide index and recalculate the window * * @param newIndex - New active slide index * @returns Object containing arrays of indices that entered and left the window */ updateActiveIndex(newIndex: number): { entered: number[]; left: number[]; }; /** * Register a sprite for a slide * * @param index - Slide index * @param sprite - Sprite instance * @param state - Current state of the slide */ registerSlide(index: number, sprite: EnhancedSprite, state?: SlideState): void; /** * Update the state of a slide * * @param index - Slide index * @param state - New state */ updateSlideState(index: number, state: SlideState): void; /** * Get information about a slide * * @param index - Slide index * @returns Slide information or null if index is invalid */ getSlideInfo(index: number): SlideInfo | null; /** * Get the current active index * * @returns Active slide index */ getActiveIndex(): number; /** * Get all slide information * * @returns Array of slide information */ getAllSlides(): SlideInfo[]; /** * Get slides that need to be initialized (converted from UNINITIALIZED to at least PLACEHOLDER) * * @returns Array of slide indices that need initialization */ getSlidesToInitialize(): number[]; /** * Get slides that need to be fully loaded (converted from PLACEHOLDER to LOADED) * * @returns Array of slide indices that need to be fully loaded */ getSlidesToLoad(): number[]; /** * Get slides that can be unloaded (converted from LOADED to PLACEHOLDER) * * @returns Array of slide indices that can be unloaded */ getSlidesToUnload(): number[]; /** * Set the window size * * @param size - New window size */ setWindowSize(size: number): void; /** * Get the current window size * * @returns Current window size */ getWindowSize(): number; /** * Get the last navigation direction * * @returns Last direction (-1 for prev, 1 for next, 0 for initial) */ getLastDirection(): number; /** * Alias for updateActiveIndex to maintain naming consistency * * @param newIndex - New current slide index * @returns Object containing arrays of indices that entered and left the window */ updateCurrentIndex(newIndex: number): { entered: number[]; left: number[]; }; }