Skip to content

Commit

Permalink
fix(console): test debounce and throttle utilities (#6982)
Browse files Browse the repository at this point in the history
Fix implementation of debounce and throttle utilities, including the
addition of tests.
  • Loading branch information
skyrpex authored Aug 5, 2024
1 parent 6d72349 commit 6c42f56
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 15 deletions.
24 changes: 24 additions & 0 deletions apps/wing-console/packages/utilities/src/debounce.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { afterEach, beforeEach, expect, test, vi } from "vitest";

import { debounce } from "./debounce.js";

beforeEach(() => {
vi.useFakeTimers();
});

afterEach(() => {
vi.useRealTimers();
});

test("debounces calls", () => {
const function_ = vi.fn();
const debounced = debounce(function_, 1);

debounced();
debounced();
debounced();
expect(function_).toHaveBeenCalledTimes(0);

vi.advanceTimersByTime(1);
expect(function_).toHaveBeenCalledTimes(1);
});
17 changes: 4 additions & 13 deletions apps/wing-console/packages/utilities/src/debounce.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,16 @@
export function debounce<T extends (...arguments_: any[]) => void>(
function_: T,
wait: number,
immediate: boolean = false,
): (...arguments_: Parameters<T>) => void {
let timeout: NodeJS.Timeout | undefined;

return (...arguments_: Parameters<T>): void => {
const later = () => {
timeout = undefined;
if (!immediate) {
function_(...arguments_);
}
};

const callNow = immediate && !timeout;

clearTimeout(timeout);
timeout = setTimeout(later, wait);
timeout = setTimeout(() => {
timeout = undefined;

if (callNow) {
// Trailing call.
function_(...arguments_);
}
}, wait);
};
}
39 changes: 39 additions & 0 deletions apps/wing-console/packages/utilities/src/throttle.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { afterEach, beforeEach, expect, test, vi } from "vitest";

import { throttle } from "./throttle.js";

beforeEach(() => {
vi.useFakeTimers();
});

afterEach(() => {
vi.useRealTimers();
});

test("throttles calls", () => {
const function_ = vi.fn();
const throttled = throttle(function_, 100);

throttled();
throttled();
throttled();
expect(function_).toHaveBeenCalledTimes(1);

vi.advanceTimersByTime(99);
throttled();
throttled();
throttled();
expect(function_).toHaveBeenCalledTimes(1);

vi.advanceTimersByTime(1);
throttled();
throttled();
throttled();
expect(function_).toHaveBeenCalledTimes(2);

vi.advanceTimersByTime(100);
throttled();
throttled();
throttled();
expect(function_).toHaveBeenCalledTimes(3);
});
4 changes: 2 additions & 2 deletions apps/wing-console/packages/utilities/src/throttle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ export function throttle<T extends (...arguments_: any[]) => void>(
function_: T,
timeFrame: number,
): (...arguments_: Parameters<T>) => void {
let lastTime = 0;
let lastTime: number | undefined;
return (...arguments_) => {
const now = Date.now();
if (now - lastTime >= timeFrame) {
if (lastTime === undefined || now - lastTime >= timeFrame) {
function_(...arguments_);
lastTime = now;
}
Expand Down

0 comments on commit 6c42f56

Please sign in to comment.