Skip to content

Commit

Permalink
fix: update debounce function to ensure debounceMs is required and im…
Browse files Browse the repository at this point in the history
…prove throttle integration
  • Loading branch information
Zoltan Erdos committed Feb 8, 2025
1 parent 95dfdf2 commit 1b50de1
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
10 changes: 5 additions & 5 deletions packages/code/src/@/lib/debounce.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ interface DebouncedFunction<T extends (...args: unknown[]) => unknown> {
flush: () => void;
}

function debounce<T extends (...args[]) => any>(
export const debounce = <T extends (...args: unknown[]) => unknown>(
func: T,
debounceMs: number | undefined,
{ signal, edges }: DebounceOptions = {},
): DebouncedFunction<T> {
debounceMs: number,
{ signal, edges }: DebounceOptions = {}
): DebouncedFunction<T> =>{
let pendingArgs: Parameters<T> | null = null;
const leading = edges?.includes("leading") ?? false;
const trailing = edges?.includes("trailing") ?? true;
Expand Down Expand Up @@ -84,4 +84,4 @@ function debounce<T extends (...args[]) => any>(
return debounced as DebouncedFunction<T>;
}

export { debounce, type DebouncedFunction, type DebounceOptions };
export { type DebouncedFunction, type DebounceOptions };
2 changes: 1 addition & 1 deletion packages/code/src/@/lib/throttle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ function throttle<T extends (...args: any[]) => any>(
{ signal, edges = ["leading", "trailing"] }: ThrottleOptions = {},
): DebouncedFunction<T> {
let pendingAt: number | null = null;
const debounced = debounce(func, throttleMs, { signal, edges });
const debounced = debounce(func, throttleMs || 0, { signal, edges });

function throttled(this: unknown, ...args: Parameters<T>): void {
if (pendingAt == null) {
Expand Down

0 comments on commit 1b50de1

Please sign in to comment.