first commit
This commit is contained in:
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 };
|
||||
Reference in New Issue
Block a user