-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(component-library): replace throttle
- Loading branch information
Showing
6 changed files
with
78 additions
and
4 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
2 changes: 1 addition & 1 deletion
2
.../src/components/table-and-list/mt-data-table/composables/useScrollPossibilitiesClasses.ts
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
2 changes: 1 addition & 1 deletion
2
packages/component-library/src/directives/stickyColumn.directive.ts
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,60 @@ | ||
import { throttle } from "./throttle"; | ||
|
||
describe("throttle", () => { | ||
beforeEach(() => { | ||
vi.useFakeTimers(); | ||
}); | ||
|
||
afterEach(() => { | ||
vi.useRealTimers(); | ||
}); | ||
|
||
it("calls the function immediately", () => { | ||
// ARRANGE | ||
const handler = vi.fn(); | ||
|
||
vi.setSystemTime(new Date("December 1, 2025 00:00:00")); | ||
|
||
const subject = throttle(handler, 2_000); | ||
|
||
// ACT | ||
subject(); | ||
|
||
// ASSERT | ||
expect(handler).toHaveBeenCalledOnce(); | ||
}); | ||
|
||
it("does not call the function again within the time frame", () => { | ||
// ARRANGE | ||
const handler = vi.fn(); | ||
|
||
vi.setSystemTime(new Date("December 1, 2025 00:00:00")); | ||
|
||
const subject = throttle(handler, 2_000); | ||
|
||
// ACT | ||
subject(); | ||
vi.setSystemTime(new Date("December 1, 2025 00:00:01")); | ||
subject(); | ||
|
||
// ASSERT | ||
expect(handler).toHaveBeenCalledOnce(); | ||
}); | ||
|
||
it("calls the function again after the time frame", () => { | ||
// ARRANGE | ||
const handler = vi.fn(); | ||
|
||
vi.setSystemTime(new Date("December 1, 2025 00:00:00")); | ||
|
||
const subject = throttle(handler, 2_000); | ||
|
||
// ACT | ||
subject(); | ||
vi.setSystemTime(new Date("December 1, 2025 00:00:02")); | ||
subject(); | ||
|
||
// ASSERT | ||
expect(handler).toHaveBeenCalledTimes(2); | ||
}); | ||
}); |
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,14 @@ | ||
export function throttle<T extends (...args: any[]) => void>( | ||
func: T, | ||
timeFrame: number, | ||
): (...args: Parameters<T>) => void { | ||
let lastTime = 0; | ||
|
||
return function (this: any, ...args: Parameters<T>) { | ||
const now = new Date(); | ||
if (now.getTime() - lastTime >= timeFrame) { | ||
func.apply(this, args); | ||
lastTime = now.getTime(); | ||
} | ||
}; | ||
} |