initial commit
This commit is contained in:
79
node_modules/debounce-fn/index.d.ts
generated
vendored
Normal file
79
node_modules/debounce-fn/index.d.ts
generated
vendored
Normal file
@@ -0,0 +1,79 @@
|
||||
export type Options = {
|
||||
/**
|
||||
Time in milliseconds to wait until the `input` function is called.
|
||||
|
||||
@default 0
|
||||
*/
|
||||
readonly wait?: number;
|
||||
|
||||
/**
|
||||
The maximum time the `input` function is allowed to be delayed before it's invoked.
|
||||
|
||||
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 Infinity
|
||||
*/
|
||||
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;
|
||||
};
|
||||
|
||||
export type BeforeOptions = {
|
||||
readonly before: true;
|
||||
} & Options;
|
||||
|
||||
export type NoBeforeNoAfterOptions = {
|
||||
readonly after: false;
|
||||
readonly before?: false;
|
||||
} & Options;
|
||||
|
||||
export type 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 from 'debounce-fn';
|
||||
|
||||
window.onresize = debounceFn(() => {
|
||||
// Do something on window resize
|
||||
}, {wait: 100});
|
||||
```
|
||||
*/
|
||||
export default function debounceFn<ArgumentsType extends unknown[], ReturnType>(
|
||||
input: (...arguments: ArgumentsType) => ReturnType,
|
||||
options: BeforeOptions
|
||||
): DebouncedFunction<ArgumentsType, ReturnType>;
|
||||
|
||||
export default function debounceFn<ArgumentsType extends unknown[], ReturnType>(
|
||||
input: (...arguments: ArgumentsType) => ReturnType,
|
||||
options: NoBeforeNoAfterOptions
|
||||
): DebouncedFunction<ArgumentsType, undefined>;
|
||||
|
||||
export default function debounceFn<ArgumentsType extends unknown[], ReturnType>(
|
||||
input: (...arguments: ArgumentsType) => ReturnType,
|
||||
options?: Options
|
||||
): DebouncedFunction<ArgumentsType, ReturnType | undefined>;
|
||||
88
node_modules/debounce-fn/index.js
generated
vendored
Normal file
88
node_modules/debounce-fn/index.js
generated
vendored
Normal file
@@ -0,0 +1,88 @@
|
||||
import mimicFunction from 'mimic-function';
|
||||
|
||||
const debounceFunction = (inputFunction, options = {}) => {
|
||||
if (typeof inputFunction !== 'function') {
|
||||
throw new TypeError(`Expected the first argument to be a function, got \`${typeof inputFunction}\``);
|
||||
}
|
||||
|
||||
const {
|
||||
wait = 0,
|
||||
maxWait = Number.POSITIVE_INFINITY,
|
||||
before = false,
|
||||
after = true,
|
||||
} = options;
|
||||
|
||||
if (wait < 0 || maxWait < 0) {
|
||||
throw new RangeError('`wait` and `maxWait` must not be negative.');
|
||||
}
|
||||
|
||||
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; // eslint-disable-line unicorn/no-this-assignment
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
if (after) {
|
||||
result = inputFunction.apply(context, arguments_);
|
||||
}
|
||||
};
|
||||
|
||||
const shouldCallNow = before && !timeout;
|
||||
clearTimeout(timeout);
|
||||
timeout = setTimeout(later, wait);
|
||||
|
||||
if (maxWait > 0 && maxWait !== Number.POSITIVE_INFINITY && !maxTimeout) {
|
||||
maxTimeout = setTimeout(maxLater, maxWait);
|
||||
}
|
||||
|
||||
if (shouldCallNow) {
|
||||
result = inputFunction.apply(context, arguments_);
|
||||
}
|
||||
|
||||
return result;
|
||||
};
|
||||
|
||||
mimicFunction(debouncedFunction, inputFunction);
|
||||
|
||||
debouncedFunction.cancel = () => {
|
||||
if (timeout) {
|
||||
clearTimeout(timeout);
|
||||
timeout = undefined;
|
||||
}
|
||||
|
||||
if (maxTimeout) {
|
||||
clearTimeout(maxTimeout);
|
||||
maxTimeout = undefined;
|
||||
}
|
||||
};
|
||||
|
||||
return debouncedFunction;
|
||||
};
|
||||
|
||||
export default debounceFunction;
|
||||
9
node_modules/debounce-fn/license
generated
vendored
Normal file
9
node_modules/debounce-fn/license
generated
vendored
Normal file
@@ -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.
|
||||
48
node_modules/debounce-fn/package.json
generated
vendored
Normal file
48
node_modules/debounce-fn/package.json
generated
vendored
Normal file
@@ -0,0 +1,48 @@
|
||||
{
|
||||
"name": "debounce-fn",
|
||||
"version": "6.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": "https://sindresorhus.com"
|
||||
},
|
||||
"type": "module",
|
||||
"exports": {
|
||||
"types": "./index.d.ts",
|
||||
"default": "./index.js"
|
||||
},
|
||||
"sideEffects": false,
|
||||
"engines": {
|
||||
"node": ">=18"
|
||||
},
|
||||
"scripts": {
|
||||
"test": "xo && ava && tsd"
|
||||
},
|
||||
"files": [
|
||||
"index.js",
|
||||
"index.d.ts"
|
||||
],
|
||||
"keywords": [
|
||||
"debounce",
|
||||
"function",
|
||||
"debouncer",
|
||||
"fn",
|
||||
"func",
|
||||
"throttle",
|
||||
"delay",
|
||||
"invoked"
|
||||
],
|
||||
"dependencies": {
|
||||
"mimic-function": "^5.0.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"ava": "^5.3.1",
|
||||
"delay": "^6.0.0",
|
||||
"tsd": "^0.29.0",
|
||||
"xo": "^0.56.0"
|
||||
}
|
||||
}
|
||||
73
node_modules/debounce-fn/readme.md
generated
vendored
Normal file
73
node_modules/debounce-fn/readme.md
generated
vendored
Normal file
@@ -0,0 +1,73 @@
|
||||
# debounce-fn
|
||||
|
||||
> [Debounce](https://davidwalsh.name/javascript-debounce-function) a function
|
||||
|
||||
## Install
|
||||
|
||||
```sh
|
||||
npm install debounce-fn
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
```js
|
||||
import debounceFunction from 'debounce-fn';
|
||||
|
||||
window.onresize = debounceFunction(() => {
|
||||
// Do something on window resize
|
||||
}, {wait: 100});
|
||||
```
|
||||
|
||||
## API
|
||||
|
||||
### debounceFunction(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 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
|
||||
|
||||
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
|
||||
Reference in New Issue
Block a user