refactor: repartition server-side and client-side code
This commit is contained in:
64
app/.client/utils/throttle.ts
Normal file
64
app/.client/utils/throttle.ts
Normal file
@@ -0,0 +1,64 @@
|
||||
export function throttle<T extends (...args: any[]) => any>(func: T, wait: number): (...args: Parameters<T>) => void {
|
||||
let timeout: NodeJS.Timeout | null = null;
|
||||
let lastArgs: Parameters<T> | null = null;
|
||||
let lastCallTime = 0;
|
||||
|
||||
return function executedFunction(...args: Parameters<T>) {
|
||||
const now = Date.now();
|
||||
const remaining = wait - (now - lastCallTime);
|
||||
|
||||
lastArgs = args;
|
||||
|
||||
if (remaining <= 0) {
|
||||
lastCallTime = now;
|
||||
func(...args);
|
||||
lastArgs = null;
|
||||
} else if (!timeout) {
|
||||
timeout = setTimeout(() => {
|
||||
lastCallTime = Date.now();
|
||||
timeout = null;
|
||||
|
||||
if (lastArgs) {
|
||||
func(...lastArgs);
|
||||
lastArgs = null;
|
||||
}
|
||||
}, remaining);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
export function throttleWithTrailing<T extends (...args: any[]) => any>(
|
||||
func: T,
|
||||
wait: number,
|
||||
): (...args: Parameters<T>) => void {
|
||||
let timeout: NodeJS.Timeout | null = null;
|
||||
let lastArgs: Parameters<T> | null = null;
|
||||
let lastCallTime = 0;
|
||||
|
||||
return function executedFunction(...args: Parameters<T>) {
|
||||
const now = Date.now();
|
||||
const remaining = wait - (now - lastCallTime);
|
||||
|
||||
lastArgs = args;
|
||||
|
||||
if (remaining <= 0) {
|
||||
if (timeout) {
|
||||
clearTimeout(timeout);
|
||||
timeout = null;
|
||||
}
|
||||
|
||||
lastCallTime = now;
|
||||
func(...args);
|
||||
} else if (!timeout) {
|
||||
timeout = setTimeout(() => {
|
||||
lastCallTime = Date.now();
|
||||
timeout = null;
|
||||
|
||||
if (lastArgs) {
|
||||
func(...lastArgs);
|
||||
lastArgs = null;
|
||||
}
|
||||
}, remaining);
|
||||
}
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user