Skip to content

Commit

Permalink
fix: inline modified debounce-fn into this package
Browse files Browse the repository at this point in the history
  • Loading branch information
Julusian committed Aug 13, 2023
1 parent ce1816f commit a068123
Show file tree
Hide file tree
Showing 11 changed files with 481 additions and 7 deletions.
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,14 @@
"CHANGELOG.md",
"dist",
"generated",
"assets"
"assets",
"vendor"
],
"dependencies": {
"@sentry/node": "^7.63.0",
"@sentry/tracing": "^7.63.0",
"ajv": "^8.12.0",
"debounce-fn": "github:julusian/debounce-fn#4.0.0-maxWaithack.0",
"debounce-fn": "link:./vendor/debounce-fn",
"ejson": "^2.2.3",
"eventemitter3": "^4.0.7",
"nanoid": "^3.3.4",
Expand Down
2 changes: 2 additions & 0 deletions vendor/debounce-fn/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
yarn.lock
1 change: 1 addition & 0 deletions vendor/debounce-fn/.npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock=false
83 changes: 83 additions & 0 deletions vendor/debounce-fn/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
declare namespace debounceFn {
interface Options {
/**
Time to wait until the `input` function is called.
@default 0
*/
readonly wait?: number;

/**
Maximum time to wait until the `input` function is called.
Only applies when after is true.
Disabled when 0
@default 0
*/
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;
}

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;
81 changes: 81 additions & 0 deletions vendor/debounce-fn/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
'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,
maxWait = 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 maxTimeout;
let result;

const debouncedFunction = function (...arguments_) {
const context = this;

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;
}

result = inputFunction.apply(context, arguments_);
};

const shouldCallNow = before && !timeout;
clearTimeout(timeout);
timeout = setTimeout(later, wait);

if (maxWait > 0 && !maxTimeout && after) {
maxTimeout = setTimeout(maxLater, maxWait);
}

if (shouldCallNow) {
result = inputFunction.apply(context, arguments_);
}

return result;
};

mimicFn(debouncedFunction, inputFunction);

debouncedFunction.cancel = () => {
if (timeout) {
clearTimeout(timeout);
timeout = undefined;
}

if (maxTimeout) {
clearTimeout(maxTimeout);
maxTimeout = undefined;
}
};

return debouncedFunction;
};
24 changes: 24 additions & 0 deletions vendor/debounce-fn/index.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import {expectType, expectError} from 'tsd';
import debounceFn = require('.');

const stringToBoolean = (string: string) => true;

const options: debounceFn.Options = {};
const debounced = debounceFn(stringToBoolean);
expectType<debounceFn.DebouncedFunction<[string], boolean | undefined>>(debounced);
expectType<boolean | undefined>(debounced('foo'));
debounced.cancel();

expectType<boolean | undefined>(debounceFn(stringToBoolean)('foo'));
expectError<boolean>(debounceFn(stringToBoolean)('foo'));

expectType<boolean | undefined>(debounceFn(stringToBoolean, {wait: 20})('foo'));
expectError<boolean>(debounceFn(stringToBoolean, {wait: 20})('foo'));
expectType<boolean | undefined>(debounceFn(stringToBoolean, {after: true})('foo'));
expectError<boolean>(debounceFn(stringToBoolean, {after: true})('foo'));

expectType<boolean>(debounceFn(stringToBoolean, {before: true})('foo'));
expectType<boolean>(debounceFn(stringToBoolean, {before: true, after: true})('foo'));

expectType<undefined>(debounceFn(stringToBoolean, {after: false})('foo'));
expectError<boolean>(debounceFn(stringToBoolean, {after: false})('foo'));
9 changes: 9 additions & 0 deletions vendor/debounce-fn/license
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
MIT License

Copyright (c) Sindre Sorhus <[email protected]> (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.
42 changes: 42 additions & 0 deletions vendor/debounce-fn/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"name": "debounce-fn",
"version": "4.0.0-maxWaithack.0",
"description": "Debounce a function",
"license": "MIT",
"repository": "sindresorhus/debounce-fn",
"funding": "https://github.com/sponsors/sindresorhus",
"author": {
"name": "Sindre Sorhus",
"email": "[email protected]",
"url": "https://sindresorhus.com"
},
"engines": {
"node": ">=10"
},
"scripts": {
"test": "xo && ava && tsd"
},
"files": [
"index.js",
"index.d.ts"
],
"keywords": [
"debounce",
"function",
"debouncer",
"fn",
"func",
"throttle",
"delay",
"invoked"
],
"dependencies": {
"mimic-fn": "^3.0.0"
},
"devDependencies": {
"ava": "^1.4.1",
"delay": "^4.2.0",
"tsd": "^0.11.0",
"xo": "^0.26.1"
}
}
64 changes: 64 additions & 0 deletions vendor/debounce-fn/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# 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
Loading

0 comments on commit a068123

Please sign in to comment.