-
Notifications
You must be signed in to change notification settings - Fork 16
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
Showing
34 changed files
with
1,976 additions
and
1,449 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
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,83 @@ | ||
declare namespace debounceFn { | ||
interface Options { | ||
/** | ||
Time to wait until the `input` function is called. | ||
@default 0 | ||
*/ | ||
readonly wait?: number | ||
|
||
/** | ||
Maximum time to wait until the `input` function is called. | ||
Only applies when after is true. | ||
Disabled when 0 | ||
@default 0 | ||
*/ | ||
readonly maxWait?: number | ||
|
||
/** | ||
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 before?: boolean | ||
|
||
/** | ||
Trigger the function on the trailing edge of the `wait` interval. | ||
@default true | ||
*/ | ||
readonly after?: boolean | ||
} | ||
|
||
interface BeforeOptions extends Options { | ||
readonly before: true | ||
} | ||
|
||
interface NoBeforeNoAfterOptions extends Options { | ||
readonly after: false | ||
readonly before?: false | ||
} | ||
|
||
interface DebouncedFunction<ArgumentsType extends unknown[], ReturnType> { | ||
(...arguments: ArgumentsType): ReturnType | ||
cancel(): void | ||
} | ||
} | ||
|
||
/** | ||
[Debounce](https://davidwalsh.name/javascript-debounce-function) a function. | ||
@param input - Function to debounce. | ||
@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. | ||
It comes with a `.cancel()` method to cancel any scheduled `input` function calls. | ||
@example | ||
``` | ||
import debounceFn = require('debounce-fn'); | ||
window.onresize = debounceFn(() => { | ||
// Do something on window resize | ||
}, {wait: 100}); | ||
``` | ||
*/ | ||
declare function debounceFn<ArgumentsType extends unknown[], ReturnType>( | ||
input: (...arguments: ArgumentsType) => ReturnType, | ||
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 | ||
): debounceFn.DebouncedFunction<ArgumentsType, ReturnType | undefined> | ||
|
||
export = debounceFn |
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,76 @@ | ||
'use strict' | ||
const mimicFn = require('mimic-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, maxWait = 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 maxTimeout | ||
let result | ||
|
||
const debouncedFunction = function (...arguments_) { | ||
const context = this | ||
|
||
const later = () => { | ||
timeout = undefined | ||
|
||
if (maxTimeout) { | ||
clearTimeout(maxTimeout) | ||
maxTimeout = undefined | ||
} | ||
|
||
if (after) { | ||
result = inputFunction.apply(context, arguments_) | ||
} | ||
} | ||
|
||
const maxLater = () => { | ||
maxTimeout = undefined | ||
|
||
if (timeout) { | ||
clearTimeout(timeout) | ||
timeout = undefined | ||
} | ||
|
||
result = inputFunction.apply(context, arguments_) | ||
} | ||
|
||
const shouldCallNow = before && !timeout | ||
clearTimeout(timeout) | ||
timeout = setTimeout(later, wait) | ||
|
||
if (maxWait > 0 && !maxTimeout && after) { | ||
maxTimeout = setTimeout(maxLater, maxWait) | ||
} | ||
|
||
if (shouldCallNow) { | ||
result = inputFunction.apply(context, arguments_) | ||
} | ||
|
||
return result | ||
} | ||
|
||
mimicFn(debouncedFunction, inputFunction) | ||
|
||
debouncedFunction.cancel = () => { | ||
if (timeout) { | ||
clearTimeout(timeout) | ||
timeout = undefined | ||
} | ||
|
||
if (maxTimeout) { | ||
clearTimeout(maxTimeout) | ||
maxTimeout = undefined | ||
} | ||
} | ||
|
||
return debouncedFunction | ||
} |
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,9 @@ | ||
MIT License | ||
|
||
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (https://sindresorhus.com) | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
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,64 @@ | ||
# debounce-fn | ||
|
||
> [Debounce](https://davidwalsh.name/javascript-debounce-function) a function | ||
## Install | ||
|
||
``` | ||
$ npm install debounce-fn | ||
``` | ||
|
||
## Usage | ||
|
||
```js | ||
const debounceFn = require('debounce-fn'); | ||
|
||
window.onresize = debounceFn(() => { | ||
// Do something on window resize | ||
}, {wait: 100}); | ||
``` | ||
|
||
## API | ||
|
||
### debounceFn(input, options?) | ||
|
||
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. | ||
|
||
It comes with a `.cancel()` method to cancel any scheduled `input` function calls. | ||
|
||
#### input | ||
|
||
Type: `Function` | ||
|
||
Function to debounce. | ||
|
||
#### options | ||
|
||
Type: `object` | ||
|
||
##### wait | ||
|
||
Type: `number`\ | ||
Default: `0` | ||
|
||
Time to wait until the `input` function is called. | ||
|
||
##### before | ||
|
||
Type: `boolean`\ | ||
Default: `false` | ||
|
||
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. | ||
|
||
##### after | ||
|
||
Type: `boolean`\ | ||
Default: `true` | ||
|
||
Trigger the function on the trailing edge of the `wait` interval. | ||
|
||
## Related | ||
|
||
- [p-debounce](https://github.com/sindresorhus/p-debounce) - Debounce promise-returning & async functions |
Oops, something went wrong.