debounce-fn
Advanced tools
Comparing version 3.0.1 to 4.0.0
@@ -11,13 +11,27 @@ declare namespace debounceFn { | ||
/** | ||
Trigger the function on the leading edge instead of the trailing edge of the `wait` interval. For example, can be useful for preventing accidental double-clicks on a "submit" button from firing a second time. | ||
Trigger the function on the leading edge of the `wait` interval. | ||
For example, this can be useful for preventing accidental double-clicks on a "submit" button from firing a second time. | ||
@default false | ||
*/ | ||
readonly immediate?: boolean; | ||
readonly before?: boolean; | ||
/** | ||
Trigger the function on the trailing edge of the `wait` interval. | ||
@default true | ||
*/ | ||
readonly after?: boolean; | ||
} | ||
interface ImmediateOptions extends Options { | ||
readonly immediate: true; | ||
interface BeforeOptions extends Options { | ||
readonly before: true; | ||
} | ||
interface NoBeforeNoAfterOptions extends Options { | ||
readonly after: false; | ||
readonly before?: false; | ||
} | ||
interface DebouncedFunction<ArgumentsType extends unknown[], ReturnType> { | ||
@@ -48,6 +62,12 @@ (...arguments: ArgumentsType): ReturnType; | ||
input: (...arguments: ArgumentsType) => ReturnType, | ||
options: debounceFn.ImmediateOptions | ||
options: debounceFn.BeforeOptions | ||
): debounceFn.DebouncedFunction<ArgumentsType, ReturnType>; | ||
declare function debounceFn<ArgumentsType extends unknown[], ReturnType>( | ||
input: (...arguments: ArgumentsType) => ReturnType, | ||
options: debounceFn.NoBeforeNoAfterOptions | ||
): debounceFn.DebouncedFunction<ArgumentsType, undefined>; | ||
declare function debounceFn<ArgumentsType extends unknown[], ReturnType>( | ||
input: (...arguments: ArgumentsType) => ReturnType, | ||
options?: debounceFn.Options | ||
@@ -54,0 +74,0 @@ ): debounceFn.DebouncedFunction<ArgumentsType, ReturnType | undefined>; |
41
index.js
'use strict'; | ||
const mimicFn = require('mimic-fn'); | ||
module.exports = (fn, options = {}) => { | ||
if (typeof fn !== 'function') { | ||
throw new TypeError(`Expected the first argument to be a function, got \`${typeof fn}\``); | ||
module.exports = (inputFunction, options = {}) => { | ||
if (typeof inputFunction !== 'function') { | ||
throw new TypeError(`Expected the first argument to be a function, got \`${typeof inputFunction}\``); | ||
} | ||
const { | ||
wait = 0, | ||
before = false, | ||
after = true | ||
} = options; | ||
if (!before && !after) { | ||
throw new Error('Both `before` and `after` are false, function wouldn\'t be called.'); | ||
} | ||
let timeout; | ||
let result; | ||
const debounced = function (...args) { | ||
const debouncedFunction = function (...arguments_) { | ||
const context = this; | ||
const later = () => { | ||
timeout = null; | ||
if (!options.immediate) { | ||
result = fn.apply(context, args); | ||
timeout = undefined; | ||
if (after) { | ||
result = inputFunction.apply(context, arguments_); | ||
} | ||
}; | ||
const callNow = options.immediate && !timeout; | ||
const shouldCallNow = before && !timeout; | ||
clearTimeout(timeout); | ||
timeout = setTimeout(later, options.wait || 0); | ||
timeout = setTimeout(later, wait); | ||
if (callNow) { | ||
result = fn.apply(context, args); | ||
if (shouldCallNow) { | ||
result = inputFunction.apply(context, arguments_); | ||
} | ||
@@ -33,12 +44,12 @@ | ||
mimicFn(debounced, fn); | ||
mimicFn(debouncedFunction, inputFunction); | ||
debounced.cancel = () => { | ||
debouncedFunction.cancel = () => { | ||
if (timeout) { | ||
clearTimeout(timeout); | ||
timeout = null; | ||
timeout = undefined; | ||
} | ||
}; | ||
return debounced; | ||
return debouncedFunction; | ||
}; |
{ | ||
"name": "debounce-fn", | ||
"version": "3.0.1", | ||
"version": "4.0.0", | ||
"description": "Debounce a function", | ||
"license": "MIT", | ||
"repository": "sindresorhus/debounce-fn", | ||
"funding": "https://github.com/sponsors/sindresorhus", | ||
"author": { | ||
"name": "Sindre Sorhus", | ||
"email": "sindresorhus@gmail.com", | ||
"url": "sindresorhus.com" | ||
"url": "https://sindresorhus.com" | ||
}, | ||
"engines": { | ||
"node": ">=8" | ||
"node": ">=10" | ||
}, | ||
@@ -33,3 +34,3 @@ "scripts": { | ||
"dependencies": { | ||
"mimic-fn": "^2.1.0" | ||
"mimic-fn": "^3.0.0" | ||
}, | ||
@@ -39,5 +40,5 @@ "devDependencies": { | ||
"delay": "^4.2.0", | ||
"tsd": "^0.7.2", | ||
"xo": "^0.24.0" | ||
"tsd": "^0.11.0", | ||
"xo": "^0.26.1" | ||
} | ||
} |
@@ -5,3 +5,2 @@ # debounce-fn [![Build Status](https://travis-ci.org/sindresorhus/debounce-fn.svg?branch=master)](https://travis-ci.org/sindresorhus/debounce-fn) | ||
## Install | ||
@@ -13,3 +12,2 @@ | ||
## Usage | ||
@@ -25,6 +23,5 @@ | ||
## API | ||
### debounceFn(input, [options]) | ||
### debounceFn(input, options?) | ||
@@ -43,7 +40,7 @@ Returns a debounced function that delays calling the `input` function until after `wait` milliseconds have elapsed since the last time the debounced function was called. | ||
Type: `Object` | ||
Type: `object` | ||
##### wait | ||
Type: `number`<br> | ||
Type: `number`\ | ||
Default: `0` | ||
@@ -53,17 +50,20 @@ | ||
##### immediate | ||
##### before | ||
Type: `boolean`<br> | ||
Type: `boolean`\ | ||
Default: `false` | ||
Trigger the function on the leading edge instead of the trailing edge of the `wait` interval. For example, can be useful for preventing accidental double-clicks on a "submit" button from firing a second time. | ||
Trigger the function on the leading edge of the `wait` interval. | ||
For example, can be useful for preventing accidental double-clicks on a "submit" button from firing a second time. | ||
## Related | ||
##### after | ||
- [p-debounce](https://github.com/sindresorhus/p-debounce) - Debounce promise-returning & async functions | ||
Type: `boolean`\ | ||
Default: `true` | ||
Trigger the function on the trailing edge of the `wait` interval. | ||
## License | ||
## Related | ||
MIT © [Sindre Sorhus](https://sindresorhus.com) | ||
- [p-debounce](https://github.com/sindresorhus/p-debounce) - Debounce promise-returning & async functions |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
6367
98
+ Addedmimic-fn@3.1.0(transitive)
- Removedmimic-fn@2.1.0(transitive)
Updatedmimic-fn@^3.0.0