Simplified scroll to bottom logic

This commit is contained in:
yangdx
2025-04-23 00:38:35 +08:00
parent 5bfa2e7dc6
commit 6f064925eb
2 changed files with 135 additions and 66 deletions

View File

@@ -19,6 +19,39 @@ export function errorMessage(error: any) {
return error instanceof Error ? error.message : `${error}`
}
/**
* Creates a throttled function that limits how often the original function can be called
* @param fn The function to throttle
* @param delay The delay in milliseconds
* @returns A throttled version of the function
*/
export function throttle<T extends (...args: any[]) => any>(fn: T, delay: number): (...args: Parameters<T>) => void {
let lastCall = 0
let timeoutId: ReturnType<typeof setTimeout> | null = null
return function(this: any, ...args: Parameters<T>) {
const now = Date.now()
const remaining = delay - (now - lastCall)
if (remaining <= 0) {
// If enough time has passed, execute the function immediately
if (timeoutId) {
clearTimeout(timeoutId)
timeoutId = null
}
lastCall = now
fn.apply(this, args)
} else if (!timeoutId) {
// If not enough time has passed, set a timeout to execute after the remaining time
timeoutId = setTimeout(() => {
lastCall = Date.now()
timeoutId = null
fn.apply(this, args)
}, remaining)
}
}
}
type WithSelectors<S> = S extends { getState: () => infer T }
? S & { use: { [K in keyof T]: () => T[K] } }
: never