refactor(editor): replace debounce and throttle with lodash (#10639)

This commit is contained in:
Saul-Mirone
2025-03-06 04:46:52 +00:00
parent 824f573ff9
commit 2b30d756e2
27 changed files with 118 additions and 202 deletions

View File

@@ -23,96 +23,6 @@ export function noop(_?: unknown) {
return;
}
/**
* @example
* ```ts
* const log = (message: string) => console.log(`[${new Date().toISOString()}] ${message}`);
*
* const throttledLog = throttle(log, 1000);
*
* throttledLog("Hello, world!");
* throttledLog("Hello, world!");
* throttledLog("Hello, world!");
* throttledLog("Hello, world!");
* throttledLog("Hello, world!");
* ```
*/
export function throttle<T extends (...args: any[]) => any>(
fn: T,
limit: number,
options?: { leading?: boolean; trailing?: boolean }
): T;
export function throttle<
Args extends unknown[],
T extends (...args: Args) => void,
>(
fn: (...args: Args) => void,
limit: number,
options?: { leading?: boolean; trailing?: boolean }
): T;
export function throttle<
Args extends unknown[],
T extends (this: unknown, ...args: Args) => void,
>(fn: T, limit: number, { leading = true, trailing = true } = {}): T {
let timer: ReturnType<typeof setTimeout> | null = null;
let lastArgs: Args | null = null;
const setTimer = () => {
if (lastArgs && trailing) {
fn(...lastArgs);
lastArgs = null;
timer = setTimeout(setTimer, limit);
} else {
timer = null;
}
};
return function (this: unknown, ...args: Parameters<T>) {
if (timer) {
// in throttle
lastArgs = args;
return;
}
// Execute the function on the leading edge
if (leading) {
fn.apply(this, args);
}
timer = setTimeout(setTimer, limit);
} as T;
}
export const debounce = <T extends (...args: any[]) => void>(
fn: T,
limit: number,
{ leading = true, trailing = true } = {}
): T => {
let timer: ReturnType<typeof setTimeout> | null = null;
let lastArgs: Parameters<T> | null = null;
// eslint-disable-next-line sonarjs/no-identical-functions
const setTimer = () => {
if (lastArgs && trailing) {
fn(...lastArgs);
lastArgs = null;
timer = setTimeout(setTimer, limit);
} else {
timer = null;
}
};
return function (...args: Parameters<T>) {
if (timer) {
lastArgs = args;
clearTimeout(timer);
}
if (leading && !timer) {
fn(...args);
}
timer = setTimeout(setTimer, limit);
} as T;
};
export async function nextTick() {
// @ts-expect-error check window.scheduler
if ('scheduler' in window && 'yield' in window.scheduler) {