first commit
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.
|
||||
119
node_modules/perfect-debounce/README.md
generated
vendored
Normal file
119
node_modules/perfect-debounce/README.md
generated
vendored
Normal file
@@ -0,0 +1,119 @@
|
||||
# perfect-debounce
|
||||
|
||||
<!-- automd:badges color=yellow codecov bundlephobia packagephobia -->
|
||||
|
||||
[](https://npmjs.com/package/perfect-debounce)
|
||||
[](https://npm.chart.dev/perfect-debounce)
|
||||
[](https://bundlephobia.com/package/perfect-debounce)
|
||||
[](https://packagephobia.com/result?p=perfect-debounce)
|
||||
[](https://codecov.io/gh/unjs/perfect-debounce)
|
||||
|
||||
<!-- /automd -->
|
||||
|
||||
Improved debounce function with Promise support.
|
||||
|
||||
## Features
|
||||
|
||||
- Well tested debounce implementation
|
||||
- Native Promise support
|
||||
- Avoid duplicate calls while promise is being resolved
|
||||
- Configurable `trailing` and `leading` behavior
|
||||
- Control methods
|
||||
|
||||
## Usage
|
||||
|
||||
Install package:
|
||||
|
||||
```sh
|
||||
npx nypm i perfect-debounce
|
||||
```
|
||||
|
||||
Import:
|
||||
|
||||
```js
|
||||
import { debounce } from "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 your function. This helps to avoid multiple calls.
|
||||
|
||||
### Control Methods
|
||||
|
||||
The returned debounced function provides additional control methods:
|
||||
|
||||
- `debounced.cancel()`: Cancel any pending invocation that has not yet occurred.
|
||||
- `await debounced.flush()`: Immediately invoke the pending function call (if any) and return its result.
|
||||
- `debounced.isPending()`: Returns `true` if there is a pending invocation waiting to be called, otherwise `false`.
|
||||
|
||||
```js
|
||||
debounced.cancel(); // Cancel any pending call
|
||||
await debounced.flush(); // Immediately invoke pending call (if any)
|
||||
debounced.isPending(); // Returns true if a call is pending
|
||||
```
|
||||
|
||||
### Example
|
||||
|
||||
```js
|
||||
const debounced = debounce(async (value) => {
|
||||
// Some async work
|
||||
return value * 2;
|
||||
}, 100);
|
||||
|
||||
debounced(1);
|
||||
debounced(2);
|
||||
debounced(3);
|
||||
|
||||
// Check if a call is pending
|
||||
console.log(debounced.isPending()); // true
|
||||
|
||||
// Immediately invoke the pending call
|
||||
const result = await debounced.flush();
|
||||
console.log(result); // 6
|
||||
|
||||
// Cancel any further pending calls
|
||||
debounced.cancel();
|
||||
```
|
||||
|
||||
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
|
||||
|
||||
Based on [sindresorhus/p-debounce](https://github.com/sindresorhus/p-debounce).
|
||||
|
||||
Made with 💛 Published under [MIT License](./LICENSE).
|
||||
49
node_modules/perfect-debounce/dist/index.d.mts
generated
vendored
Normal file
49
node_modules/perfect-debounce/dist/index.d.mts
generated
vendored
Normal file
@@ -0,0 +1,49 @@
|
||||
//#region src/index.d.ts
|
||||
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 true
|
||||
*/
|
||||
readonly trailing?: boolean;
|
||||
}
|
||||
type DebouncedReturn<ArgumentsT extends unknown[], ReturnT> = ((...args: ArgumentsT) => Promise<ReturnT>) & {
|
||||
/**
|
||||
* Cancel pending function call
|
||||
*/
|
||||
cancel: () => void;
|
||||
/**
|
||||
* Immediately invoke pending function call
|
||||
*/
|
||||
flush: () => Promise<ReturnT> | undefined;
|
||||
/**
|
||||
* Get pending function call
|
||||
*/
|
||||
isPending: () => 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));
|
||||
}
|
||||
//=> 1
|
||||
//=> 2
|
||||
//=> 3
|
||||
```
|
||||
*/
|
||||
declare function debounce<ArgumentsT extends unknown[], ReturnT>(fn: (...args: ArgumentsT) => PromiseLike<ReturnT> | ReturnT, wait?: number, options?: DebounceOptions): DebouncedReturn<ArgumentsT, ReturnT>;
|
||||
//#endregion
|
||||
export { DebounceOptions, debounce };
|
||||
89
node_modules/perfect-debounce/dist/index.mjs
generated
vendored
Normal file
89
node_modules/perfect-debounce/dist/index.mjs
generated
vendored
Normal file
@@ -0,0 +1,89 @@
|
||||
//#region src/index.ts
|
||||
const DEBOUNCE_DEFAULTS = { trailing: true };
|
||||
/**
|
||||
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));
|
||||
}
|
||||
//=> 1
|
||||
//=> 2
|
||||
//=> 3
|
||||
```
|
||||
*/
|
||||
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;
|
||||
};
|
||||
const debounced = function(...args) {
|
||||
if (options.trailing) trailingArgs = args;
|
||||
if (currentPromise) 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);
|
||||
trailingArgs = null;
|
||||
for (const _resolve of resolveList) _resolve(promise);
|
||||
resolveList = [];
|
||||
}, wait);
|
||||
if (shouldCallNow) {
|
||||
leadingValue = applyFn(this, args);
|
||||
resolve(leadingValue);
|
||||
} else resolveList.push(resolve);
|
||||
});
|
||||
};
|
||||
const _clearTimeout = (timer) => {
|
||||
if (timer) {
|
||||
clearTimeout(timer);
|
||||
timeout = null;
|
||||
}
|
||||
};
|
||||
debounced.isPending = () => !!timeout;
|
||||
debounced.cancel = () => {
|
||||
_clearTimeout(timeout);
|
||||
resolveList = [];
|
||||
trailingArgs = null;
|
||||
};
|
||||
debounced.flush = () => {
|
||||
_clearTimeout(timeout);
|
||||
if (!trailingArgs || currentPromise) return;
|
||||
const args = trailingArgs;
|
||||
trailingArgs = null;
|
||||
return applyFn(this, args);
|
||||
};
|
||||
return debounced;
|
||||
}
|
||||
async function _applyPromised(fn, _this, args) {
|
||||
return await fn.apply(_this, args);
|
||||
}
|
||||
|
||||
//#endregion
|
||||
export { debounce };
|
||||
41
node_modules/perfect-debounce/package.json
generated
vendored
Normal file
41
node_modules/perfect-debounce/package.json
generated
vendored
Normal file
@@ -0,0 +1,41 @@
|
||||
{
|
||||
"name": "perfect-debounce",
|
||||
"version": "2.0.0",
|
||||
"description": "",
|
||||
"repository": "unjs/perfect-debounce",
|
||||
"license": "MIT",
|
||||
"sideEffects": false,
|
||||
"type": "module",
|
||||
"exports": {
|
||||
".": "./dist/index.mjs"
|
||||
},
|
||||
"main": "./dist/index.mjs",
|
||||
"module": "./dist/index.mjs",
|
||||
"types": "./dist/index.d.mts",
|
||||
"files": [
|
||||
"dist"
|
||||
],
|
||||
"scripts": {
|
||||
"build": "obuild",
|
||||
"dev": "vitest dev",
|
||||
"lint": "eslint . && prettier --check src test",
|
||||
"lint:fix": "eslint . --fix && prettier -w src test",
|
||||
"release": "pnpm test && pnpm build && changelogen --release --push && npm publish",
|
||||
"test": "vitest run --coverage"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/node": "^24.3.0",
|
||||
"@vitest/coverage-v8": "^3.2.4",
|
||||
"automd": "^0.4.0",
|
||||
"changelogen": "^0.6.2",
|
||||
"eslint": "^9.34.0",
|
||||
"eslint-config-unjs": "^0.5.0",
|
||||
"in-range": "^3.0.0",
|
||||
"obuild": "^0.2.1",
|
||||
"prettier": "^3.6.2",
|
||||
"time-span": "^5.1.0",
|
||||
"typescript": "^5.9.2",
|
||||
"vitest": "^3.2.4"
|
||||
},
|
||||
"packageManager": "pnpm@10.15.0"
|
||||
}
|
||||
Reference in New Issue
Block a user