first commit
This commit is contained in:
46
lib/loading-utils.ts
Normal file
46
lib/loading-utils.ts
Normal file
@@ -0,0 +1,46 @@
|
||||
/**
|
||||
* Debounce function for handling rapid state changes
|
||||
* @param func - Function to debounce
|
||||
* @param delay - Delay in milliseconds
|
||||
*/
|
||||
export function debounce<T extends (...args: any[]) => any>(func: T, delay: number): (...args: Parameters<T>) => void {
|
||||
let timeoutId: ReturnType<typeof setTimeout>
|
||||
|
||||
return (...args: Parameters<T>) => {
|
||||
clearTimeout(timeoutId)
|
||||
timeoutId = setTimeout(() => func(...args), delay)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Throttle function for rate-limiting function calls
|
||||
* @param func - Function to throttle
|
||||
* @param limit - Minimum time between calls
|
||||
*/
|
||||
export function throttle<T extends (...args: any[]) => any>(func: T, limit: number): (...args: Parameters<T>) => void {
|
||||
let lastRun = 0
|
||||
|
||||
return (...args: Parameters<T>) => {
|
||||
const now = Date.now()
|
||||
if (now - lastRun >= limit) {
|
||||
func(...args)
|
||||
lastRun = now
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sleep utility for simulating delays
|
||||
* @param ms - Milliseconds to sleep
|
||||
*/
|
||||
export function sleep(ms: number): Promise<void> {
|
||||
return new Promise((resolve) => setTimeout(resolve, ms))
|
||||
}
|
||||
|
||||
/**
|
||||
* Simulate loading state
|
||||
* @param duration - Duration of loading state
|
||||
*/
|
||||
export async function simulateLoading(duration = 500): Promise<void> {
|
||||
return sleep(duration)
|
||||
}
|
||||
Reference in New Issue
Block a user