debounce-fn
Advanced tools
Comparing version 4.0.0 to 5.0.0
@@ -1,42 +0,49 @@ | ||
declare namespace debounceFn { | ||
interface Options { | ||
/** | ||
Time to wait until the `input` function is called. | ||
export interface Options { | ||
/** | ||
Time in milliseconds to wait until the `input` function is called. | ||
@default 0 | ||
*/ | ||
readonly wait?: number; | ||
@default 0 | ||
*/ | ||
readonly wait?: number; | ||
/** | ||
Trigger the function on the leading edge of the `wait` interval. | ||
/** | ||
The maximum time the `input` function is allowed to be delayed before it's invoked. | ||
For example, this can be useful for preventing accidental double-clicks on a "submit" button from firing a second time. | ||
This can be used to control the rate of calls handled in a constant stream. For example, a media player sending updates every few milliseconds but wants to be handled only once a second. | ||
@default false | ||
*/ | ||
readonly before?: boolean; | ||
@default Infinity | ||
*/ | ||
readonly maxWait?: number; | ||
/** | ||
Trigger the function on the trailing edge of the `wait` interval. | ||
/** | ||
Trigger the function on the leading edge of the `wait` interval. | ||
@default true | ||
*/ | ||
readonly after?: boolean; | ||
} | ||
For example, this can be useful for preventing accidental double-clicks on a "submit" button from firing a second time. | ||
interface BeforeOptions extends Options { | ||
readonly before: true; | ||
} | ||
@default false | ||
*/ | ||
readonly before?: boolean; | ||
interface NoBeforeNoAfterOptions extends Options { | ||
readonly after: false; | ||
readonly before?: false; | ||
} | ||
/** | ||
Trigger the function on the trailing edge of the `wait` interval. | ||
interface DebouncedFunction<ArgumentsType extends unknown[], ReturnType> { | ||
(...arguments: ArgumentsType): ReturnType; | ||
cancel(): void; | ||
} | ||
@default true | ||
*/ | ||
readonly after?: boolean; | ||
} | ||
export interface BeforeOptions extends Options { | ||
readonly before: true; | ||
} | ||
export interface NoBeforeNoAfterOptions extends Options { | ||
readonly after: false; | ||
readonly before?: false; | ||
} | ||
export interface DebouncedFunction<ArgumentsType extends unknown[], ReturnType> { | ||
(...arguments: ArgumentsType): ReturnType; | ||
cancel(): void; | ||
} | ||
/** | ||
@@ -52,3 +59,3 @@ [Debounce](https://davidwalsh.name/javascript-debounce-function) a function. | ||
``` | ||
import debounceFn = require('debounce-fn'); | ||
import debounceFn from 'debounce-fn'; | ||
@@ -62,15 +69,15 @@ window.onresize = debounceFn(() => { | ||
input: (...arguments: ArgumentsType) => ReturnType, | ||
options: debounceFn.BeforeOptions | ||
): debounceFn.DebouncedFunction<ArgumentsType, ReturnType>; | ||
options: BeforeOptions | ||
): DebouncedFunction<ArgumentsType, ReturnType>; | ||
declare function debounceFn<ArgumentsType extends unknown[], ReturnType>( | ||
input: (...arguments: ArgumentsType) => ReturnType, | ||
options: debounceFn.NoBeforeNoAfterOptions | ||
): debounceFn.DebouncedFunction<ArgumentsType, undefined>; | ||
options: NoBeforeNoAfterOptions | ||
): DebouncedFunction<ArgumentsType, undefined>; | ||
declare function debounceFn<ArgumentsType extends unknown[], ReturnType>( | ||
input: (...arguments: ArgumentsType) => ReturnType, | ||
options?: debounceFn.Options | ||
): debounceFn.DebouncedFunction<ArgumentsType, ReturnType | undefined>; | ||
options?: Options | ||
): DebouncedFunction<ArgumentsType, ReturnType | undefined>; | ||
export = debounceFn; | ||
export default debounceFn; |
36
index.js
@@ -1,5 +0,4 @@ | ||
'use strict'; | ||
const mimicFn = require('mimic-fn'); | ||
import mimicFn from 'mimic-fn'; | ||
module.exports = (inputFunction, options = {}) => { | ||
const debounceFn = (inputFunction, options = {}) => { | ||
if (typeof inputFunction !== 'function') { | ||
@@ -11,2 +10,3 @@ throw new TypeError(`Expected the first argument to be a function, got \`${typeof inputFunction}\``); | ||
wait = 0, | ||
maxWait = Number.Infinity, | ||
before = false, | ||
@@ -21,2 +21,3 @@ after = true | ||
let timeout; | ||
let maxTimeout; | ||
let result; | ||
@@ -30,2 +31,7 @@ | ||
if (maxTimeout) { | ||
clearTimeout(maxTimeout); | ||
maxTimeout = undefined; | ||
} | ||
if (after) { | ||
@@ -36,2 +42,15 @@ result = inputFunction.apply(context, arguments_); | ||
const maxLater = () => { | ||
maxTimeout = undefined; | ||
if (timeout) { | ||
clearTimeout(timeout); | ||
timeout = undefined; | ||
} | ||
if (after) { | ||
result = inputFunction.apply(context, arguments_); | ||
} | ||
}; | ||
const shouldCallNow = before && !timeout; | ||
@@ -41,2 +60,6 @@ clearTimeout(timeout); | ||
if (maxWait > 0 && maxWait !== Number.Infinity && !maxTimeout) { | ||
maxTimeout = setTimeout(maxLater, maxWait); | ||
} | ||
if (shouldCallNow) { | ||
@@ -56,2 +79,7 @@ result = inputFunction.apply(context, arguments_); | ||
} | ||
if (maxTimeout) { | ||
clearTimeout(maxTimeout); | ||
maxTimeout = undefined; | ||
} | ||
}; | ||
@@ -61,1 +89,3 @@ | ||
}; | ||
export default debounceFn; |
{ | ||
"name": "debounce-fn", | ||
"version": "4.0.0", | ||
"version": "5.0.0", | ||
"description": "Debounce a function", | ||
@@ -13,4 +13,6 @@ "license": "MIT", | ||
}, | ||
"type": "module", | ||
"exports": "./index.js", | ||
"engines": { | ||
"node": ">=10" | ||
"node": ">=12" | ||
}, | ||
@@ -38,7 +40,7 @@ "scripts": { | ||
"devDependencies": { | ||
"ava": "^1.4.1", | ||
"ava": "^3.15.0", | ||
"delay": "^4.2.0", | ||
"tsd": "^0.11.0", | ||
"xo": "^0.26.1" | ||
"tsd": "^0.14.0", | ||
"xo": "^0.37.1" | ||
} | ||
} |
@@ -1,2 +0,2 @@ | ||
# debounce-fn [![Build Status](https://travis-ci.org/sindresorhus/debounce-fn.svg?branch=master)](https://travis-ci.org/sindresorhus/debounce-fn) | ||
# debounce-fn | ||
@@ -14,3 +14,3 @@ > [Debounce](https://davidwalsh.name/javascript-debounce-function) a function | ||
```js | ||
const debounceFn = require('debounce-fn'); | ||
import debounceFn from 'debounce-fn'; | ||
@@ -45,4 +45,13 @@ window.onresize = debounceFn(() => { | ||
Time to wait until the `input` function is called. | ||
Time in milliseconds to wait until the `input` function is called. | ||
##### maxWait | ||
Type: `number`\ | ||
Default: `Infinity` | ||
The maximum time the `input` function is allowed to be delayed before it's invoked. | ||
This can be used to limit the number of calls handled in a constant stream. For example, a media player sending updates every few milliseconds but wants to be handled only once a second. | ||
##### before | ||
@@ -49,0 +58,0 @@ |
7420
125
74
Yes