Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions packages/core/src/hooks/useDebounce/debounce.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
// Simplified version of https://github.com/toss/es-toolkit/blob/main/src/function/debounce.ts

// eslint-disable-next-line @typescript-eslint/no-explicit-any -- generic function constraint requires `any[]` due to parameter contravariance
export type DebouncedFunction<F extends (...args: any[]) => void> = {
(...args: Parameters<F>): void;
cancel: () => void;
Expand All @@ -17,12 +17,13 @@ type DebounceOptions = {
edges?: Array<'leading' | 'trailing'>;
};

// eslint-disable-next-line @typescript-eslint/no-explicit-any -- generic function constraint requires `any[]` due to parameter contravariance
export function debounce<F extends (...args: any[]) => void>(
func: F,
debounceMs: number,
{ edges = ['leading', 'trailing'] }: DebounceOptions = {}
): DebouncedFunction<F> {
let pendingThis: any = undefined;
let pendingThis: ThisParameterType<F> | undefined = undefined;
let pendingArgs: Parameters<F> | null = null;

const leading = edges != null && edges.includes('leading');
Expand Down Expand Up @@ -71,7 +72,7 @@ export function debounce<F extends (...args: any[]) => void>(
pendingArgs = null;
};

const debounced = function (this: any, ...args: Parameters<F>) {
const debounced = function (this: ThisParameterType<F>, ...args: Parameters<F>) {
// eslint-disable-next-line @typescript-eslint/no-this-alias
pendingThis = this;
pendingArgs = args;
Expand Down
Loading