-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.js
More file actions
75 lines (66 loc) · 1.97 KB
/
Copy pathutils.js
File metadata and controls
75 lines (66 loc) · 1.97 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
class RetryUtils {
static async withRetry(operation, options = {}) {
const {
maxAttempts = 3,
initialDelay = 1000,
maxDelay = 10000,
backoffFactor = 2,
shouldRetry = (error) => true,
} = options;
let lastError;
let delay = initialDelay;
for (let attempt = 1; attempt <= maxAttempts; attempt++) {
try {
return await operation();
} catch (error) {
lastError = error;
console.warn(`Attempt ${attempt} failed:`, error);
if (attempt === maxAttempts || !shouldRetry(error)) {
throw lastError;
}
await new Promise((resolve) => setTimeout(resolve, delay));
delay = Math.min(delay * backoffFactor, maxDelay);
}
}
}
}
class NetworkUtils {
static isOnline() {
return navigator.onLine;
}
static async waitForOnline() {
if (this.isOnline()) {
return true;
}
return new Promise((resolve) => {
const handleOnline = () => {
window.removeEventListener("online", handleOnline);
resolve(true);
};
window.addEventListener("online", handleOnline);
});
}
}
class PerformanceUtils {
static debounce(func, wait) {
let timeout;
return function executedFunction(...args) {
const later = () => {
clearTimeout(timeout);
func(...args);
};
clearTimeout(timeout);
timeout = setTimeout(later, wait);
};
}
static throttle(func, limit) {
let inThrottle;
return function executedFunction(...args) {
if (!inThrottle) {
func(...args);
inThrottle = true;
setTimeout(() => (inThrottle = false), limit);
}
};
}
}