-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.js
More file actions
100 lines (89 loc) · 2.62 KB
/
Copy pathutils.js
File metadata and controls
100 lines (89 loc) · 2.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// ========================================
// Performance Utilities
// ========================================
// Debounce function - limits function calls
export function debounce(func, wait) {
let timeout;
return function executedFunction(...args) {
const later = () => {
clearTimeout(timeout);
func(...args);
};
clearTimeout(timeout);
timeout = setTimeout(later, wait);
};
}
// Throttle function - limits function execution rate
export function throttle(func, limit) {
let inThrottle;
return function(...args) {
if (!inThrottle) {
func.apply(this, args);
inThrottle = true;
setTimeout(() => inThrottle = false, limit);
}
};
}
// RequestAnimationFrame wrapper for smooth animations
export function rafThrottle(func) {
let rafId = null;
return function(...args) {
if (rafId === null) {
rafId = requestAnimationFrame(() => {
func.apply(this, args);
rafId = null;
});
}
};
}
// Lazy load images with intersection observer
export function lazyLoadImages() {
if ('IntersectionObserver' in window) {
const imageObserver = new IntersectionObserver((entries, observer) => {
entries.forEach(entry => {
if (entry.isIntersecting) {
const img = entry.target;
if (img.dataset.src) {
img.src = img.dataset.src;
img.removeAttribute('data-src');
observer.unobserve(img);
}
}
});
}, {
rootMargin: '50px'
});
document.querySelectorAll('img[data-src]').forEach(img => {
imageObserver.observe(img);
});
}
}
// Cache for icon loading
const iconCache = new Map();
// Load icon with caching
export function loadIcon(path) {
if (iconCache.has(path)) {
return Promise.resolve(iconCache.get(path));
}
return new Promise((resolve, reject) => {
const img = new Image();
img.onload = () => {
iconCache.set(path, img);
resolve(img);
};
img.onerror = reject;
img.src = path;
});
}
// Clean up event listeners
export function cleanupEventListeners(element, eventType, handler) {
if (element && handler) {
element.removeEventListener(eventType, handler);
}
}
// Batch DOM updates
export function batchDOMUpdates(updates) {
requestAnimationFrame(() => {
updates.forEach(update => update());
});
}