mirror of
https://github.com/toss/es-toolkit.git
synced 2024-11-24 03:32:58 +03:00
feat(debounce, throttle): Support passing arguments to callback function in debounce & throttle function (#26)
* feat: add support for variable arguments in debounce.ts * feat: add support for variable arguments in throttle.ts * Update src/function/debounce.ts * Apply suggestions from code review * Update src/function/debounce.ts * Update src/function/throttle.ts * docs: update related docs --------- Co-authored-by: Sojin Park <raon0211@gmail.com> Co-authored-by: Sojin Park <raon0211@toss.im>
This commit is contained in:
parent
cf65b2c601
commit
79046ea5c1
@ -7,17 +7,17 @@ debounce된 함수는 또한 대기 중인 실행을 취소하는 `cancel` 메
|
||||
## 인터페이스
|
||||
|
||||
```typescript
|
||||
function debounce(func: () => void, debounceMs: number): { (): void; cancel: () => void };
|
||||
function debounce<F extends (...args: any[]) => void>(func: F, debounceMs: number): F & { cancel: () => void };
|
||||
```
|
||||
|
||||
### 파라미터
|
||||
|
||||
- `func` (`() => void`): debounce된 함수를 만들 함수.
|
||||
- `func` (`F`): debounce된 함수를 만들 함수.
|
||||
- `debounceMs`(`number`): debounce로 지연시킬 밀리초.
|
||||
|
||||
### 결괏값
|
||||
|
||||
(`{ (): void; cancel: () => void }`): `cancel` 메서드를 가지고 있는 debounce된 함수.
|
||||
(`F & { cancel: () => void }`): `cancel` 메서드를 가지고 있는 debounce된 함수.
|
||||
|
||||
## 예시
|
||||
|
||||
|
@ -5,17 +5,17 @@
|
||||
## 인터페이스
|
||||
|
||||
```typescript
|
||||
function throttle(func: () => void, throttleMs: number): () => void;
|
||||
function throttle<F extends (...args: any[]) => void>(func: F, throttleMs: number): F;
|
||||
```
|
||||
|
||||
### 파라미터
|
||||
|
||||
- `func` (`() => void`): throttle할 함수.
|
||||
- `func` (`F`): throttle할 함수.
|
||||
- `throttleMs`(`number`): 실행을 throttle할 밀리초.
|
||||
|
||||
### 반환 값
|
||||
|
||||
(`() => void`): 새로운 throttle된 함수.
|
||||
(`F`): 새로운 throttle된 함수.
|
||||
|
||||
## 예시
|
||||
|
||||
|
@ -7,17 +7,17 @@ method to cancel any pending execution.
|
||||
## Signature
|
||||
|
||||
```typescript
|
||||
function debounce(func: () => void, debounceMs: number): { (): void; cancel: () => void };
|
||||
function debounce<F extends (...args: any[]) => void>(func: F, debounceMs: number): F & { cancel: () => void };
|
||||
```
|
||||
|
||||
### Parameters
|
||||
|
||||
- `func` (`() => void`): The function to debounce.
|
||||
- `func` (`F`): The function to debounce.
|
||||
- `debounceMs`(`number`): The number of milliseconds to delay.
|
||||
|
||||
### Returns
|
||||
|
||||
(`{ (): void; cancel: () => void }`): A new debounced function with a `cancel` method.
|
||||
(`F & { cancel: () => void }`): A new debounced function with a `cancel` method.
|
||||
|
||||
## Examples
|
||||
|
||||
|
@ -7,17 +7,17 @@ within the wait time will not trigger the execution of the original function.
|
||||
## Signature
|
||||
|
||||
```typescript
|
||||
function throttle(func: () => void, throttleMs: number): () => void;
|
||||
function throttle<F extends (...args: any[]) => void>(func: F, throttleMs: number): F;
|
||||
```
|
||||
|
||||
### Parameters
|
||||
|
||||
- `func` (`() => void`): The function to throttle.
|
||||
- `func` (`F`): The function to throttle.
|
||||
- `throttleMs`(`number`): The number of milliseconds to throttle executions to.
|
||||
|
||||
### Returns
|
||||
|
||||
(`() => void`): A new throttled function.
|
||||
(`F`): A new throttled function.
|
||||
|
||||
## Examples
|
||||
|
||||
|
@ -3,9 +3,9 @@
|
||||
* have elapsed since the last time the debounced function was invoked. The debounced function also has a `cancel`
|
||||
* method to cancel any pending execution.
|
||||
*
|
||||
* @param {() => void} func - The function to debounce.
|
||||
* @param {F} func - The function to debounce.
|
||||
* @param {number} debounceMs - The number of milliseconds to delay.
|
||||
* @returns {{ (): void; cancel: () => void }} A new debounced function with a `cancel` method.
|
||||
* @returns {F & { cancel: () => void }} A new debounced function with a `cancel` method.
|
||||
*
|
||||
* @example
|
||||
* const debouncedFunction = debounce(() => {
|
||||
@ -18,18 +18,18 @@
|
||||
* // Will not log anything as the previous call is canceled
|
||||
* debouncedFunction.cancel();
|
||||
*/
|
||||
export function debounce(func: () => void, debounceMs: number): { (): void; cancel: () => void } {
|
||||
export function debounce<F extends (...args: any[]) => void>(func: F, debounceMs: number): F & { cancel: () => void } {
|
||||
let timeoutId: number | NodeJS.Timeout | null = null;
|
||||
|
||||
const debounced = function () {
|
||||
const debounced = function (...args: Parameters<F>) {
|
||||
if (timeoutId != null) {
|
||||
clearTimeout(timeoutId);
|
||||
}
|
||||
|
||||
timeoutId = setTimeout(() => {
|
||||
func();
|
||||
func(...args);
|
||||
}, debounceMs);
|
||||
};
|
||||
} as F & { cancel: () => void };
|
||||
|
||||
debounced.cancel = function () {
|
||||
if (timeoutId != null) {
|
||||
|
@ -3,9 +3,9 @@
|
||||
* per every `throttleMs` milliseconds. Subsequent calls to the throttled function
|
||||
* within the wait time will not trigger the execution of the original function.
|
||||
*
|
||||
* @param {() => void} func - The function to throttle.
|
||||
* @param {F} func - The function to throttle.
|
||||
* @param {number} throttleMs - The number of milliseconds to throttle executions to.
|
||||
* @returns {() => void} A new throttled function.
|
||||
* @returns {F} A new throttled function that accepts the same parameters as the original function.
|
||||
*
|
||||
* @example
|
||||
* const throttledFunction = throttle(() => {
|
||||
@ -23,17 +23,17 @@
|
||||
* throttledFunction(); // Will log 'Function executed'
|
||||
* }, 1000);
|
||||
*/
|
||||
export function throttle(func: () => void, throttleMs: number) {
|
||||
export function throttle<F extends (...args: any[]) => void>(func: F, throttleMs: number) {
|
||||
let lastCallTime: number | null;
|
||||
|
||||
const throttledFunction = function () {
|
||||
const throttledFunction = function (...args: Parameters<F>) {
|
||||
const now = Date.now();
|
||||
|
||||
if (lastCallTime == null || now - lastCallTime >= throttleMs) {
|
||||
lastCallTime = now;
|
||||
func();
|
||||
func(...args);
|
||||
}
|
||||
};
|
||||
} as F;
|
||||
|
||||
return throttledFunction;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user