This commit is contained in:
2024-08-18 23:38:55 +08:00
parent e8dbb9bab3
commit 8f57f57c1d
1004 changed files with 234067 additions and 16087 deletions

View File

@@ -0,0 +1,74 @@
declare namespace debounceFn {
interface Options {
/**
Time to wait until the `input` function is called.
@default 0
*/
readonly wait?: 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;

View File

@@ -0,0 +1,54 @@
'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,
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 result;
const debouncedFunction = function (...arguments_) {
const context = this;
const later = () => {
timeout = undefined;
if (after) {
result = inputFunction.apply(context, arguments_);
}
};
const shouldCallNow = before && !timeout;
clearTimeout(timeout);
timeout = setTimeout(later, wait);
if (shouldCallNow) {
result = inputFunction.apply(context, arguments_);
}
return result;
};
mimicFn(debouncedFunction, inputFunction);
debouncedFunction.cancel = () => {
if (timeout) {
clearTimeout(timeout);
timeout = undefined;
}
};
return debouncedFunction;
};

View 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.

View File

@@ -0,0 +1,78 @@
{
"_args": [
[
"debounce-fn@4.0.0",
"/Users/wuyu/Desktop/61/Git/h5game_ailesson_v2.4/packages/excel_to_json"
]
],
"_development": true,
"_from": "debounce-fn@4.0.0",
"_id": "debounce-fn@4.0.0",
"_inBundle": false,
"_integrity": "sha1-7XbSBtilDmDeDdZtSU2Cg1/+Ycc=",
"_location": "/debounce-fn",
"_phantomChildren": {},
"_requested": {
"type": "version",
"registry": true,
"raw": "debounce-fn@4.0.0",
"name": "debounce-fn",
"escapedName": "debounce-fn",
"rawSpec": "4.0.0",
"saveSpec": null,
"fetchSpec": "4.0.0"
},
"_requiredBy": [
"/conf"
],
"_resolved": "https://registry.npm.taobao.org/debounce-fn/download/debounce-fn-4.0.0.tgz",
"_spec": "4.0.0",
"_where": "/Users/wuyu/Desktop/61/Git/h5game_ailesson_v2.4/packages/excel_to_json",
"author": {
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
"url": "https://sindresorhus.com"
},
"bugs": {
"url": "https://github.com/sindresorhus/debounce-fn/issues"
},
"dependencies": {
"mimic-fn": "^3.0.0"
},
"description": "Debounce a function",
"devDependencies": {
"ava": "^1.4.1",
"delay": "^4.2.0",
"tsd": "^0.11.0",
"xo": "^0.26.1"
},
"engines": {
"node": ">=10"
},
"files": [
"index.js",
"index.d.ts"
],
"funding": "https://github.com/sponsors/sindresorhus",
"homepage": "https://github.com/sindresorhus/debounce-fn#readme",
"keywords": [
"debounce",
"function",
"debouncer",
"fn",
"func",
"throttle",
"delay",
"invoked"
],
"license": "MIT",
"name": "debounce-fn",
"repository": {
"type": "git",
"url": "git+https://github.com/sindresorhus/debounce-fn.git"
},
"scripts": {
"test": "xo && ava && tsd"
},
"version": "4.0.0"
}

View File

@@ -0,0 +1,64 @@
# debounce-fn [![Build Status](https://travis-ci.org/sindresorhus/debounce-fn.svg?branch=master)](https://travis-ci.org/sindresorhus/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