@sv443-network/userutils 6.0.1 → 6.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +6 -0
- package/README.md +27 -6
- package/dist/index.global.js +11 -4
- package/dist/index.js +10 -3
- package/dist/index.mjs +10 -3
- package/dist/lib/misc.d.ts +4 -1
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
# @sv443-network/userutils
|
|
2
2
|
|
|
3
|
+
## 6.1.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- a11ed77: Added parameter to switch `debounce()` to trigger on the rising edge, instead of just the falling edge [(see docs)](https://github.com/Sv443-Network/UserUtils#debounce)
|
|
8
|
+
|
|
3
9
|
## 6.0.1
|
|
4
10
|
|
|
5
11
|
### Patch Changes
|
package/README.md
CHANGED
|
@@ -47,7 +47,7 @@ View the documentation of previous major releases:
|
|
|
47
47
|
- [DataStore](#datastore) - class that manages a sync & async persistent JSON database, including data migration
|
|
48
48
|
- [autoPlural()](#autoplural) - automatically pluralize a string
|
|
49
49
|
- [pauseFor()](#pausefor) - pause the execution of a function for a given amount of time
|
|
50
|
-
- [debounce()](#debounce) - call a function only once, after a given
|
|
50
|
+
- [debounce()](#debounce) - call a function only once in a series of calls, after or before a given timeout
|
|
51
51
|
- [fetchAdvanced()](#fetchadvanced) - wrapper around the fetch API with a timeout option
|
|
52
52
|
- [insertValues()](#insertvalues) - insert values into a string at specified placeholders
|
|
53
53
|
- [compress()](#compress) - compress a string with Gzip or Deflate
|
|
@@ -1180,22 +1180,43 @@ async function run() {
|
|
|
1180
1180
|
### debounce()
|
|
1181
1181
|
Usage:
|
|
1182
1182
|
```ts
|
|
1183
|
-
debounce(func: Function, timeout?: number): Function
|
|
1183
|
+
debounce(func: Function, timeout?: number, edge?: "falling" | "rising"): Function
|
|
1184
1184
|
```
|
|
1185
1185
|
|
|
1186
|
-
|
|
1187
|
-
This is very useful for functions that are called repeatedly, like event listeners, to remove
|
|
1188
|
-
All passed
|
|
1189
|
-
|
|
1186
|
+
Returns a debounced wrapper function, meaning that the given `func` will only be called once after or before a given amount of time.
|
|
1187
|
+
This is very useful for functions that are called repeatedly, like event listeners, to remove a substantial amount of unnecessary calls.
|
|
1188
|
+
All parameters passed to the returned function will be passed along to the input `func`
|
|
1189
|
+
|
|
1190
|
+
The `timeout` will default to 300ms if left undefined.
|
|
1191
|
+
|
|
1192
|
+
The `edge` ("falling" by default) determines if the function should be called after the timeout has passed or before it.
|
|
1193
|
+
In simpler terms, this results in "falling" edge functions being called once at the very end of a sequence of calls, and "rising" edge functions being called once at the beginning and possibly multiple times following that, but at the very least they're spaced apart by what's passed in `timeout`.
|
|
1194
|
+
|
|
1195
|
+
This diagram can hopefully help bring the difference across:
|
|
1196
|
+
<details><summary><b>Click to view the diagram</b></summary>
|
|
1197
|
+
|
|
1198
|
+

|
|
1199
|
+
|
|
1200
|
+
</details>
|
|
1201
|
+
|
|
1202
|
+
<br>
|
|
1190
1203
|
|
|
1191
1204
|
<details><summary><b>Example - click to view</b></summary>
|
|
1192
1205
|
|
|
1193
1206
|
```ts
|
|
1194
1207
|
import { debounce } from "@sv443-network/userutils";
|
|
1195
1208
|
|
|
1209
|
+
// uses "falling" edge by default:
|
|
1196
1210
|
window.addEventListener("resize", debounce((event) => {
|
|
1197
1211
|
console.log("Window was resized:", event);
|
|
1198
1212
|
}, 500)); // 500ms timeout
|
|
1213
|
+
|
|
1214
|
+
// using "rising" edge:
|
|
1215
|
+
const myFunc = debounce((event) => {
|
|
1216
|
+
console.log("Body was scrolled:", event);
|
|
1217
|
+
}, 100, "rising"); // 100ms timeout
|
|
1218
|
+
|
|
1219
|
+
document.body.addEventListener("scroll", myFunc);
|
|
1199
1220
|
```
|
|
1200
1221
|
|
|
1201
1222
|
</details>
|
package/dist/index.global.js
CHANGED
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
// ==UserLibrary==
|
|
9
9
|
// @name UserUtils
|
|
10
10
|
// @description Library with various utilities for userscripts - register listeners for when CSS selectors exist, intercept events, manage persistent user configurations, modify the DOM more easily and more
|
|
11
|
-
// @version 6.0
|
|
11
|
+
// @version 6.1.0
|
|
12
12
|
// @license MIT
|
|
13
13
|
// @copyright Sv443 (https://github.com/Sv443)
|
|
14
14
|
|
|
@@ -432,11 +432,18 @@ var UserUtils = (function (exports) {
|
|
|
432
432
|
setTimeout(() => res(), time);
|
|
433
433
|
});
|
|
434
434
|
}
|
|
435
|
-
function debounce(func, timeout = 300) {
|
|
435
|
+
function debounce(func, timeout = 300, edge = "falling") {
|
|
436
436
|
let timer;
|
|
437
437
|
return function(...args) {
|
|
438
|
-
|
|
439
|
-
|
|
438
|
+
if (edge === "rising") {
|
|
439
|
+
if (!timer) {
|
|
440
|
+
func.apply(this, args);
|
|
441
|
+
timer = setTimeout(() => timer = void 0, timeout);
|
|
442
|
+
}
|
|
443
|
+
} else {
|
|
444
|
+
clearTimeout(timer);
|
|
445
|
+
timer = setTimeout(() => func.apply(this, args), timeout);
|
|
446
|
+
}
|
|
440
447
|
};
|
|
441
448
|
}
|
|
442
449
|
function fetchAdvanced(_0) {
|
package/dist/index.js
CHANGED
|
@@ -412,11 +412,18 @@ function pauseFor(time) {
|
|
|
412
412
|
setTimeout(() => res(), time);
|
|
413
413
|
});
|
|
414
414
|
}
|
|
415
|
-
function debounce(func, timeout = 300) {
|
|
415
|
+
function debounce(func, timeout = 300, edge = "falling") {
|
|
416
416
|
let timer;
|
|
417
417
|
return function(...args) {
|
|
418
|
-
|
|
419
|
-
|
|
418
|
+
if (edge === "rising") {
|
|
419
|
+
if (!timer) {
|
|
420
|
+
func.apply(this, args);
|
|
421
|
+
timer = setTimeout(() => timer = void 0, timeout);
|
|
422
|
+
}
|
|
423
|
+
} else {
|
|
424
|
+
clearTimeout(timer);
|
|
425
|
+
timer = setTimeout(() => func.apply(this, args), timeout);
|
|
426
|
+
}
|
|
420
427
|
};
|
|
421
428
|
}
|
|
422
429
|
function fetchAdvanced(_0) {
|
package/dist/index.mjs
CHANGED
|
@@ -410,11 +410,18 @@ function pauseFor(time) {
|
|
|
410
410
|
setTimeout(() => res(), time);
|
|
411
411
|
});
|
|
412
412
|
}
|
|
413
|
-
function debounce(func, timeout = 300) {
|
|
413
|
+
function debounce(func, timeout = 300, edge = "falling") {
|
|
414
414
|
let timer;
|
|
415
415
|
return function(...args) {
|
|
416
|
-
|
|
417
|
-
|
|
416
|
+
if (edge === "rising") {
|
|
417
|
+
if (!timer) {
|
|
418
|
+
func.apply(this, args);
|
|
419
|
+
timer = setTimeout(() => timer = void 0, timeout);
|
|
420
|
+
}
|
|
421
|
+
} else {
|
|
422
|
+
clearTimeout(timer);
|
|
423
|
+
timer = setTimeout(() => func.apply(this, args), timeout);
|
|
424
|
+
}
|
|
418
425
|
};
|
|
419
426
|
}
|
|
420
427
|
function fetchAdvanced(_0) {
|
package/dist/lib/misc.d.ts
CHANGED
|
@@ -26,8 +26,11 @@ export declare function pauseFor(time: number): Promise<void>;
|
|
|
26
26
|
/**
|
|
27
27
|
* Calls the passed {@linkcode func} after the specified {@linkcode timeout} in ms (defaults to 300).
|
|
28
28
|
* Any subsequent calls to this function will reset the timer and discard all previous calls.
|
|
29
|
+
* @param func The function to call after the timeout
|
|
30
|
+
* @param timeout The time in ms to wait before calling the function
|
|
31
|
+
* @param edge Whether to call the function at the very first call ("rising" edge) or the very last call ("falling" edge, default)
|
|
29
32
|
*/
|
|
30
|
-
export declare function debounce<TFunc extends (...args: TArgs[]) => void, TArgs = any>(func: TFunc, timeout?: number): (...args: TArgs[]) => void;
|
|
33
|
+
export declare function debounce<TFunc extends (...args: TArgs[]) => void, TArgs = any>(func: TFunc, timeout?: number, edge?: "rising" | "falling"): (...args: TArgs[]) => void;
|
|
31
34
|
/** Options for the `fetchAdvanced()` function */
|
|
32
35
|
export type FetchAdvancedOpts = Omit<RequestInit & Partial<{
|
|
33
36
|
/** Timeout in milliseconds after which the fetch call will be canceled with an AbortController signal */
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sv443-network/userutils",
|
|
3
3
|
"libName": "UserUtils",
|
|
4
|
-
"version": "6.0
|
|
4
|
+
"version": "6.1.0",
|
|
5
5
|
"description": "Library with various utilities for userscripts - register listeners for when CSS selectors exist, intercept events, manage persistent user configurations, modify the DOM more easily and more",
|
|
6
6
|
"main": "dist/index.js",
|
|
7
7
|
"module": "dist/index.mjs",
|