UNPKG

rynex

Version:

A minimalist TypeScript framework for building reactive web applications with no virtual DOM

86 lines 2.3 kB
/** * Rynex Performance Utilities * Performance optimization helpers */ /** * Debounce function execution * Delays execution until after wait time has elapsed since last call */ export function debounce(func, wait) { let timeout = null; return function (...args) { const context = this; if (timeout) { clearTimeout(timeout); } timeout = setTimeout(() => { func.apply(context, args); timeout = null; }, wait); }; } /** * Throttle function execution * Ensures function is called at most once per specified time period */ export function throttle(func, limit) { let inThrottle = false; let lastResult; return function (...args) { const context = this; if (!inThrottle) { lastResult = func.apply(context, args); inThrottle = true; setTimeout(() => { inThrottle = false; }, limit); } return lastResult; }; } /** * Preload component or resource * Loads resource in advance for better performance */ export function preload(loader) { const promise = loader(); // Store in cache for later use if (typeof window !== 'undefined') { window.__rynexPreloadCache = window.__rynexPreloadCache || new Map(); window.__rynexPreloadCache.set(loader, promise); } return promise; } /** * Get preloaded resource * Retrieves previously preloaded resource */ export function getPreloaded(loader) { if (typeof window !== 'undefined' && window.__rynexPreloadCache) { return window.__rynexPreloadCache.get(loader) || null; } return null; } /** * Request idle callback wrapper * Executes callback during browser idle time */ export function onIdle(callback, options) { if (typeof window !== 'undefined' && 'requestIdleCallback' in window) { return window.requestIdleCallback(callback, options); } // Fallback to setTimeout return setTimeout(callback, 1); } /** * Cancel idle callback */ export function cancelIdle(id) { if (typeof window !== 'undefined' && 'cancelIdleCallback' in window) { window.cancelIdleCallback(id); } else { clearTimeout(id); } } //# sourceMappingURL=performance.js.map