generated from sapphiredev/sapphire-template
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
5cc9a2c
commit 541891a
Showing
4 changed files
with
65 additions
and
0 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
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,33 @@ | ||
export type ThrottleFn<T extends (...args: any[]) => any> = T & { flush: () => void }; | ||
|
||
/** | ||
* Creates a throttled function that only invokes `func` at most once per | ||
* every `wait` milliseconds. The throttled function comes with a `flush` method to | ||
* reset the last time the throttled function was invoked. | ||
* | ||
* @param func The function to throttle. | ||
* @param wait The number of milliseconds to throttle invocations to. | ||
* | ||
* @returns Returns the new throttled function. | ||
*/ | ||
export function throttle<T extends (...args: any[]) => any>(func: T, wait: number): ThrottleFn<T> { | ||
let prev = 0; | ||
let prevValue: ReturnType<T>; | ||
|
||
return Object.assign( | ||
(...args: Parameters<T>) => { | ||
const now = Date.now(); | ||
if (now - prev > wait) { | ||
prev = now; | ||
return (prevValue = func(...args)); | ||
} | ||
|
||
return prevValue; | ||
}, | ||
{ | ||
flush() { | ||
prev = 0; | ||
} | ||
} | ||
) as ThrottleFn<T>; | ||
} |
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,26 @@ | ||
import { throttle, sleep } from '../src'; | ||
|
||
describe('throttle', () => { | ||
test('GIVEN number callback THEN returns the same output until the delay', async () => { | ||
const callback = vi.fn((num: number) => num); | ||
|
||
const throttleFunc = throttle(callback, 50); | ||
const now = Date.now(); | ||
expect(throttleFunc(now)).toEqual(now); | ||
expect(throttleFunc(100)).toEqual(now); | ||
expect(callback).toHaveBeenCalledOnce(); | ||
await sleep(100); | ||
expect(throttleFunc(250)).toEqual(250); | ||
}); | ||
|
||
test('GIVEN number callback THEN returns the new output when flush', () => { | ||
const callback = vi.fn((num: number) => num); | ||
|
||
const throttleFunc = throttle(callback, 100); | ||
const now = Date.now(); | ||
expect(throttleFunc(now)).toEqual(now); | ||
throttleFunc.flush(); | ||
expect(throttleFunc(100)).toEqual(100); | ||
expect(callback).toHaveBeenCalledTimes(2); | ||
}); | ||
}); |