This commit is contained in:
21
node_modules/perfect-debounce/LICENSE
generated
vendored
Normal file
21
node_modules/perfect-debounce/LICENSE
generated
vendored
Normal file
@@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) Pooya Parsa <pooya@pi0.io>
|
||||
|
||||
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.
|
92
node_modules/perfect-debounce/README.md
generated
vendored
Normal file
92
node_modules/perfect-debounce/README.md
generated
vendored
Normal file
@@ -0,0 +1,92 @@
|
||||
# perfect-debounce
|
||||
|
||||
[![npm version][npm-version-src]][npm-version-href]
|
||||
[![npm downloads][npm-downloads-src]][npm-downloads-href]
|
||||
[![Github Actions][github-actions-src]][github-actions-href]
|
||||
[![Codecov][codecov-src]][codecov-href]
|
||||
|
||||
> An improved debounce function with Promise support.
|
||||
|
||||
- Well tested debounce implementation
|
||||
- Native Promise support
|
||||
- Avoid duplicate calls while promise is being resolved
|
||||
- Configurable `trailing` and `leading` behavior
|
||||
|
||||
## Usage
|
||||
|
||||
Install package:
|
||||
|
||||
```sh
|
||||
# npm
|
||||
npm install perfect-debounce
|
||||
|
||||
# yarn
|
||||
yarn add perfect-debounce
|
||||
|
||||
# pnpm
|
||||
pnpm add perfect-debounce
|
||||
```
|
||||
|
||||
Import:
|
||||
|
||||
```js
|
||||
// ESM
|
||||
import { debounce } from 'perfect-debounce'
|
||||
|
||||
// CommonJS
|
||||
const { debounce } = require('perfect-debounce')
|
||||
```
|
||||
|
||||
Debounce function:
|
||||
|
||||
```js
|
||||
const debounced = debounce(async () => {
|
||||
// Some heavy stuff
|
||||
}, 25)
|
||||
```
|
||||
|
||||
When calling `debounced`, it will wait at least for `25ms` as configured before actually calling our function. This helps to avoid multiple calls.
|
||||
|
||||
To avoid initial wait, we can set `leading: true` option. It will cause function to be immediately called if there is no other call:
|
||||
|
||||
```js
|
||||
const debounced = debounce(async () => {
|
||||
// Some heavy stuff
|
||||
}, 25, { leading: true })
|
||||
```
|
||||
|
||||
If executing async function takes longer than debounce value, duplicate calls will be still prevented a last call will happen. To disable this behavior, we can set `trailing: false` option:
|
||||
|
||||
```js
|
||||
const debounced = debounce(async () => {
|
||||
// Some heavy stuff
|
||||
}, 25, { trailing: false })
|
||||
```
|
||||
|
||||
## 💻 Development
|
||||
|
||||
- Clone this repository
|
||||
- Enable [Corepack](https://github.com/nodejs/corepack) using `corepack enable` (use `npm i -g corepack` for Node.js < 16.10)
|
||||
- Install dependencies using `pnpm install`
|
||||
- Run interactive tests using `pnpm dev`
|
||||
|
||||
## License
|
||||
|
||||
Made with 💛
|
||||
|
||||
Based on [sindresorhus/p-debounce](https://github.com/sindresorhus/p-debounce).
|
||||
|
||||
Published under [MIT License](./LICENSE).
|
||||
|
||||
<!-- Badges -->
|
||||
[npm-version-src]: https://img.shields.io/npm/v/perfect-debounce?style=flat-square
|
||||
[npm-version-href]: https://npmjs.com/package/perfect-debounce
|
||||
|
||||
[npm-downloads-src]: https://img.shields.io/npm/dm/perfect-debounce?style=flat-square
|
||||
[npm-downloads-href]: https://npmjs.com/package/perfect-debounce
|
||||
|
||||
[github-actions-src]: https://img.shields.io/github/workflow/status/unjs/perfect-debounce/ci/main?style=flat-square
|
||||
[github-actions-href]: https://github.com/unjs/perfect-debounce/actions?query=workflow%3Aci
|
||||
|
||||
[codecov-src]: https://img.shields.io/codecov/c/gh/unjs/perfect-debounce/main?style=flat-square
|
||||
[codecov-href]: https://codecov.io/gh/unjs/perfect-debounce
|
59
node_modules/perfect-debounce/dist/index.cjs
generated
vendored
Normal file
59
node_modules/perfect-debounce/dist/index.cjs
generated
vendored
Normal file
@@ -0,0 +1,59 @@
|
||||
'use strict';
|
||||
|
||||
const DEBOUNCE_DEFAULTS = {
|
||||
trailing: true
|
||||
};
|
||||
function debounce(fn, wait = 25, options = {}) {
|
||||
options = { ...DEBOUNCE_DEFAULTS, ...options };
|
||||
if (!Number.isFinite(wait)) {
|
||||
throw new TypeError("Expected `wait` to be a finite number");
|
||||
}
|
||||
let leadingValue;
|
||||
let timeout;
|
||||
let resolveList = [];
|
||||
let currentPromise;
|
||||
let trailingArgs;
|
||||
const applyFn = (_this, args) => {
|
||||
currentPromise = _applyPromised(fn, _this, args);
|
||||
currentPromise.finally(() => {
|
||||
currentPromise = null;
|
||||
if (options.trailing && trailingArgs && !timeout) {
|
||||
const promise = applyFn(_this, trailingArgs);
|
||||
trailingArgs = null;
|
||||
return promise;
|
||||
}
|
||||
});
|
||||
return currentPromise;
|
||||
};
|
||||
return function(...args) {
|
||||
if (currentPromise) {
|
||||
if (options.trailing) {
|
||||
trailingArgs = args;
|
||||
}
|
||||
return currentPromise;
|
||||
}
|
||||
return new Promise((resolve) => {
|
||||
const shouldCallNow = !timeout && options.leading;
|
||||
clearTimeout(timeout);
|
||||
timeout = setTimeout(() => {
|
||||
timeout = null;
|
||||
const promise = options.leading ? leadingValue : applyFn(this, args);
|
||||
for (const _resolve of resolveList) {
|
||||
_resolve(promise);
|
||||
}
|
||||
resolveList = [];
|
||||
}, wait);
|
||||
if (shouldCallNow) {
|
||||
leadingValue = applyFn(this, args);
|
||||
resolve(leadingValue);
|
||||
} else {
|
||||
resolveList.push(resolve);
|
||||
}
|
||||
});
|
||||
};
|
||||
}
|
||||
async function _applyPromised(fn, _this, args) {
|
||||
return await fn.apply(_this, args);
|
||||
}
|
||||
|
||||
exports.debounce = debounce;
|
34
node_modules/perfect-debounce/dist/index.d.ts
generated
vendored
Normal file
34
node_modules/perfect-debounce/dist/index.d.ts
generated
vendored
Normal file
@@ -0,0 +1,34 @@
|
||||
interface DebounceOptions {
|
||||
/**
|
||||
Call the `fn` on the [leading edge of the timeout](https://css-tricks.com/debouncing-throttling-explained-examples/#article-header-id-1).
|
||||
Meaning immediately, instead of waiting for `wait` milliseconds.
|
||||
@default false
|
||||
*/
|
||||
readonly leading?: boolean;
|
||||
/**
|
||||
Call the `fn` on trailing edge with last used arguments. Result of call is from previous call.
|
||||
@default false
|
||||
*/
|
||||
readonly trailing?: boolean;
|
||||
}
|
||||
/**
|
||||
Debounce functions
|
||||
@param fn - Promise-returning/async function to debounce.
|
||||
@param wait - Milliseconds to wait before calling `fn`. Default value is 25ms
|
||||
@returns A function that delays calling `fn` until after `wait` milliseconds have elapsed since the last time it was called.
|
||||
@example
|
||||
```
|
||||
import { debounce } from 'perfect-debounce';
|
||||
const expensiveCall = async input => input;
|
||||
const debouncedFn = debounce(expensiveCall, 200);
|
||||
for (const number of [1, 2, 3]) {
|
||||
console.log(await debouncedFn(number));
|
||||
}
|
||||
//=> 3
|
||||
//=> 3
|
||||
//=> 3
|
||||
```
|
||||
*/
|
||||
declare function debounce<ArgumentsT extends unknown[], ReturnT>(fn: (...args: ArgumentsT) => PromiseLike<ReturnT> | ReturnT, wait?: number, options?: DebounceOptions): (...args: ArgumentsT) => Promise<ReturnT>;
|
||||
|
||||
export { DebounceOptions, debounce };
|
57
node_modules/perfect-debounce/dist/index.mjs
generated
vendored
Normal file
57
node_modules/perfect-debounce/dist/index.mjs
generated
vendored
Normal file
@@ -0,0 +1,57 @@
|
||||
const DEBOUNCE_DEFAULTS = {
|
||||
trailing: true
|
||||
};
|
||||
function debounce(fn, wait = 25, options = {}) {
|
||||
options = { ...DEBOUNCE_DEFAULTS, ...options };
|
||||
if (!Number.isFinite(wait)) {
|
||||
throw new TypeError("Expected `wait` to be a finite number");
|
||||
}
|
||||
let leadingValue;
|
||||
let timeout;
|
||||
let resolveList = [];
|
||||
let currentPromise;
|
||||
let trailingArgs;
|
||||
const applyFn = (_this, args) => {
|
||||
currentPromise = _applyPromised(fn, _this, args);
|
||||
currentPromise.finally(() => {
|
||||
currentPromise = null;
|
||||
if (options.trailing && trailingArgs && !timeout) {
|
||||
const promise = applyFn(_this, trailingArgs);
|
||||
trailingArgs = null;
|
||||
return promise;
|
||||
}
|
||||
});
|
||||
return currentPromise;
|
||||
};
|
||||
return function(...args) {
|
||||
if (currentPromise) {
|
||||
if (options.trailing) {
|
||||
trailingArgs = args;
|
||||
}
|
||||
return currentPromise;
|
||||
}
|
||||
return new Promise((resolve) => {
|
||||
const shouldCallNow = !timeout && options.leading;
|
||||
clearTimeout(timeout);
|
||||
timeout = setTimeout(() => {
|
||||
timeout = null;
|
||||
const promise = options.leading ? leadingValue : applyFn(this, args);
|
||||
for (const _resolve of resolveList) {
|
||||
_resolve(promise);
|
||||
}
|
||||
resolveList = [];
|
||||
}, wait);
|
||||
if (shouldCallNow) {
|
||||
leadingValue = applyFn(this, args);
|
||||
resolve(leadingValue);
|
||||
} else {
|
||||
resolveList.push(resolve);
|
||||
}
|
||||
});
|
||||
};
|
||||
}
|
||||
async function _applyPromised(fn, _this, args) {
|
||||
return await fn.apply(_this, args);
|
||||
}
|
||||
|
||||
export { debounce };
|
44
node_modules/perfect-debounce/package.json
generated
vendored
Normal file
44
node_modules/perfect-debounce/package.json
generated
vendored
Normal file
@@ -0,0 +1,44 @@
|
||||
{
|
||||
"name": "perfect-debounce",
|
||||
"version": "1.0.0",
|
||||
"description": "",
|
||||
"repository": "unjs/perfect-debounce",
|
||||
"license": "MIT",
|
||||
"sideEffects": false,
|
||||
"type": "module",
|
||||
"exports": {
|
||||
".": {
|
||||
"types": "./dist/index.d.ts",
|
||||
"import": "./dist/index.mjs",
|
||||
"require": "./dist/index.cjs"
|
||||
}
|
||||
},
|
||||
"main": "./dist/index.cjs",
|
||||
"module": "./dist/index.mjs",
|
||||
"types": "./dist/index.d.ts",
|
||||
"files": [
|
||||
"dist"
|
||||
],
|
||||
"scripts": {
|
||||
"build": "unbuild",
|
||||
"dev": "vitest dev",
|
||||
"lint": "eslint --ext .ts,.js,.mjs,.cjs . && prettier --check src test",
|
||||
"lint:fix": "eslint --ext .ts,.js,.mjs,.cjs . --fix && prettier -w src test",
|
||||
"release": "pnpm test && pnpm build && changelogen --release --push && npm publish",
|
||||
"test": "vitest run --coverage"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/node": "^18.16.3",
|
||||
"@vitest/coverage-c8": "^0.31.0",
|
||||
"changelogen": "^0.5.3",
|
||||
"eslint": "^8.39.0",
|
||||
"eslint-config-unjs": "^0.1.0",
|
||||
"in-range": "^3.0.0",
|
||||
"prettier": "^2.8.8",
|
||||
"time-span": "^5.1.0",
|
||||
"typescript": "^5.0.4",
|
||||
"unbuild": "^1.2.1",
|
||||
"vitest": "^0.31.0"
|
||||
},
|
||||
"packageManager": "pnpm@8.4.0"
|
||||
}
|
Reference in New Issue
Block a user