-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d1d2171
commit 451e83c
Showing
4 changed files
with
107 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@codedazur/essentials": minor | ||
--- | ||
|
||
The debounce utility was added. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { | ||
afterAll, | ||
afterEach, | ||
beforeAll, | ||
describe, | ||
expect, | ||
it, | ||
vi, | ||
} from "vitest"; | ||
import { debounce } from "./debounce"; | ||
|
||
beforeAll(() => { | ||
vi.useFakeTimers(); | ||
}); | ||
|
||
afterEach(() => { | ||
vi.clearAllTimers(); | ||
}); | ||
|
||
afterAll(() => { | ||
vi.useRealTimers(); | ||
}); | ||
|
||
describe("debounce", () => { | ||
it("should debounce the function", async () => { | ||
const callback = vi.fn(); | ||
const [debounced] = debounce(callback, 100); | ||
|
||
debounced(); | ||
debounced(); | ||
|
||
expect(callback).not.toHaveBeenCalled(); | ||
|
||
await vi.advanceTimersByTimeAsync(50); | ||
expect(callback).not.toHaveBeenCalled(); | ||
|
||
await vi.advanceTimersByTimeAsync(50); | ||
expect(callback).toHaveBeenCalledOnce(); | ||
}); | ||
|
||
it("should support canceling the debounced function", async () => { | ||
const callback = vi.fn(); | ||
const [debounced, cancel] = debounce(callback, 100); | ||
|
||
debounced(); | ||
cancel(); | ||
|
||
await vi.advanceTimersByTimeAsync(100); | ||
expect(callback).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it("should support arguments", async () => { | ||
const callback = vi.fn(); | ||
const [debounced] = debounce(callback, 100); | ||
|
||
debounced(1, 2, 3); | ||
|
||
await vi.advanceTimersByTimeAsync(100); | ||
expect(callback).toHaveBeenCalledWith(1, 2, 3); | ||
}); | ||
|
||
it("should support return values", async () => { | ||
const [debounced] = debounce(() => 42, 100); | ||
|
||
let result; | ||
debounced().then((value) => (result = value)); | ||
expect(result).toBe(undefined); | ||
|
||
await vi.advanceTimersByTimeAsync(100); | ||
expect(result).toBe(42); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
type DebouncedFunction<F extends (...args: never[]) => ReturnType<F>> = ( | ||
...args: Parameters<F> | ||
) => Promise<ReturnType<F>>; | ||
|
||
type CancelFunction = () => void; | ||
|
||
export function debounce<F extends (...args: never[]) => ReturnType<F>>( | ||
callback: F, | ||
ms = 50, | ||
): [DebouncedFunction<F>, CancelFunction] { | ||
let timer: NodeJS.Timeout | undefined; | ||
|
||
return [ | ||
(...args: Parameters<F>) => | ||
new Promise((resolve) => { | ||
if (timer) { | ||
clearTimeout(timer); | ||
} | ||
|
||
timer = setTimeout(() => { | ||
timer = undefined; | ||
resolve(callback(...args)); | ||
}, ms); | ||
}), | ||
() => clearTimeout(timer), | ||
]; | ||
} |