@sv443-network/userutils 6.0.1 → 6.2.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 +12 -0
- package/README.md +27 -6
- package/dist/index.global.js +18 -11
- package/dist/index.js +17 -10
- package/dist/index.mjs +17 -10
- package/dist/lib/SelectorObserver.d.ts +7 -0
- package/dist/lib/misc.d.ts +4 -1
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,17 @@
|
|
|
1
1
|
# @sv443-network/userutils
|
|
2
2
|
|
|
3
|
+
## 6.2.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- 0173235: Add property to change the debounce edge in `SelectorObserver` instances
|
|
8
|
+
|
|
9
|
+
## 6.1.0
|
|
10
|
+
|
|
11
|
+
### Minor Changes
|
|
12
|
+
|
|
13
|
+
- 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)
|
|
14
|
+
|
|
3
15
|
## 6.0.1
|
|
4
16
|
|
|
5
17
|
### 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.2.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) {
|
|
@@ -505,10 +512,12 @@ var UserUtils = (function (exports) {
|
|
|
505
512
|
this.observer = new MutationObserver(() => this.checkAllSelectors());
|
|
506
513
|
const _a = options, {
|
|
507
514
|
defaultDebounce,
|
|
515
|
+
defaultDebounceEdge,
|
|
508
516
|
disableOnNoListeners,
|
|
509
517
|
enableOnAddListener
|
|
510
518
|
} = _a, observerOptions = __objRest(_a, [
|
|
511
519
|
"defaultDebounce",
|
|
520
|
+
"defaultDebounceEdge",
|
|
512
521
|
"disableOnNoListeners",
|
|
513
522
|
"enableOnAddListener"
|
|
514
523
|
]);
|
|
@@ -518,6 +527,7 @@ var UserUtils = (function (exports) {
|
|
|
518
527
|
}, observerOptions);
|
|
519
528
|
this.customOptions = {
|
|
520
529
|
defaultDebounce: defaultDebounce != null ? defaultDebounce : 0,
|
|
530
|
+
defaultDebounceEdge: defaultDebounceEdge != null ? defaultDebounceEdge : "rising",
|
|
521
531
|
disableOnNoListeners: disableOnNoListeners != null ? disableOnNoListeners : false,
|
|
522
532
|
enableOnAddListener: enableOnAddListener != null ? enableOnAddListener : true
|
|
523
533
|
};
|
|
@@ -557,12 +567,8 @@ var UserUtils = (function (exports) {
|
|
|
557
567
|
this.disable();
|
|
558
568
|
}
|
|
559
569
|
}
|
|
560
|
-
debounce(func, time) {
|
|
561
|
-
|
|
562
|
-
return function(...args) {
|
|
563
|
-
clearTimeout(timeout);
|
|
564
|
-
timeout = setTimeout(() => func.apply(this, args), time);
|
|
565
|
-
};
|
|
570
|
+
debounce(func, time, edge = "falling") {
|
|
571
|
+
return debounce(func, time, edge);
|
|
566
572
|
}
|
|
567
573
|
/**
|
|
568
574
|
* Starts observing the children of the base element for changes to the given {@linkcode selector} according to the set {@linkcode options}
|
|
@@ -578,7 +584,8 @@ var UserUtils = (function (exports) {
|
|
|
578
584
|
if (options.debounce && options.debounce > 0 || this.customOptions.defaultDebounce && this.customOptions.defaultDebounce > 0) {
|
|
579
585
|
options.listener = this.debounce(
|
|
580
586
|
options.listener,
|
|
581
|
-
options.debounce || this.customOptions.defaultDebounce
|
|
587
|
+
options.debounce || this.customOptions.defaultDebounce,
|
|
588
|
+
options.debounceEdge || this.customOptions.defaultDebounceEdge
|
|
582
589
|
);
|
|
583
590
|
}
|
|
584
591
|
if (this.listenerMap.has(selector))
|
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) {
|
|
@@ -485,10 +492,12 @@ var SelectorObserver = class {
|
|
|
485
492
|
this.observer = new MutationObserver(() => this.checkAllSelectors());
|
|
486
493
|
const _a = options, {
|
|
487
494
|
defaultDebounce,
|
|
495
|
+
defaultDebounceEdge,
|
|
488
496
|
disableOnNoListeners,
|
|
489
497
|
enableOnAddListener
|
|
490
498
|
} = _a, observerOptions = __objRest(_a, [
|
|
491
499
|
"defaultDebounce",
|
|
500
|
+
"defaultDebounceEdge",
|
|
492
501
|
"disableOnNoListeners",
|
|
493
502
|
"enableOnAddListener"
|
|
494
503
|
]);
|
|
@@ -498,6 +507,7 @@ var SelectorObserver = class {
|
|
|
498
507
|
}, observerOptions);
|
|
499
508
|
this.customOptions = {
|
|
500
509
|
defaultDebounce: defaultDebounce != null ? defaultDebounce : 0,
|
|
510
|
+
defaultDebounceEdge: defaultDebounceEdge != null ? defaultDebounceEdge : "rising",
|
|
501
511
|
disableOnNoListeners: disableOnNoListeners != null ? disableOnNoListeners : false,
|
|
502
512
|
enableOnAddListener: enableOnAddListener != null ? enableOnAddListener : true
|
|
503
513
|
};
|
|
@@ -537,12 +547,8 @@ var SelectorObserver = class {
|
|
|
537
547
|
this.disable();
|
|
538
548
|
}
|
|
539
549
|
}
|
|
540
|
-
debounce(func, time) {
|
|
541
|
-
|
|
542
|
-
return function(...args) {
|
|
543
|
-
clearTimeout(timeout);
|
|
544
|
-
timeout = setTimeout(() => func.apply(this, args), time);
|
|
545
|
-
};
|
|
550
|
+
debounce(func, time, edge = "falling") {
|
|
551
|
+
return debounce(func, time, edge);
|
|
546
552
|
}
|
|
547
553
|
/**
|
|
548
554
|
* Starts observing the children of the base element for changes to the given {@linkcode selector} according to the set {@linkcode options}
|
|
@@ -558,7 +564,8 @@ var SelectorObserver = class {
|
|
|
558
564
|
if (options.debounce && options.debounce > 0 || this.customOptions.defaultDebounce && this.customOptions.defaultDebounce > 0) {
|
|
559
565
|
options.listener = this.debounce(
|
|
560
566
|
options.listener,
|
|
561
|
-
options.debounce || this.customOptions.defaultDebounce
|
|
567
|
+
options.debounce || this.customOptions.defaultDebounce,
|
|
568
|
+
options.debounceEdge || this.customOptions.defaultDebounceEdge
|
|
562
569
|
);
|
|
563
570
|
}
|
|
564
571
|
if (this.listenerMap.has(selector))
|
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) {
|
|
@@ -483,10 +490,12 @@ var SelectorObserver = class {
|
|
|
483
490
|
this.observer = new MutationObserver(() => this.checkAllSelectors());
|
|
484
491
|
const _a = options, {
|
|
485
492
|
defaultDebounce,
|
|
493
|
+
defaultDebounceEdge,
|
|
486
494
|
disableOnNoListeners,
|
|
487
495
|
enableOnAddListener
|
|
488
496
|
} = _a, observerOptions = __objRest(_a, [
|
|
489
497
|
"defaultDebounce",
|
|
498
|
+
"defaultDebounceEdge",
|
|
490
499
|
"disableOnNoListeners",
|
|
491
500
|
"enableOnAddListener"
|
|
492
501
|
]);
|
|
@@ -496,6 +505,7 @@ var SelectorObserver = class {
|
|
|
496
505
|
}, observerOptions);
|
|
497
506
|
this.customOptions = {
|
|
498
507
|
defaultDebounce: defaultDebounce != null ? defaultDebounce : 0,
|
|
508
|
+
defaultDebounceEdge: defaultDebounceEdge != null ? defaultDebounceEdge : "rising",
|
|
499
509
|
disableOnNoListeners: disableOnNoListeners != null ? disableOnNoListeners : false,
|
|
500
510
|
enableOnAddListener: enableOnAddListener != null ? enableOnAddListener : true
|
|
501
511
|
};
|
|
@@ -535,12 +545,8 @@ var SelectorObserver = class {
|
|
|
535
545
|
this.disable();
|
|
536
546
|
}
|
|
537
547
|
}
|
|
538
|
-
debounce(func, time) {
|
|
539
|
-
|
|
540
|
-
return function(...args) {
|
|
541
|
-
clearTimeout(timeout);
|
|
542
|
-
timeout = setTimeout(() => func.apply(this, args), time);
|
|
543
|
-
};
|
|
548
|
+
debounce(func, time, edge = "falling") {
|
|
549
|
+
return debounce(func, time, edge);
|
|
544
550
|
}
|
|
545
551
|
/**
|
|
546
552
|
* Starts observing the children of the base element for changes to the given {@linkcode selector} according to the set {@linkcode options}
|
|
@@ -556,7 +562,8 @@ var SelectorObserver = class {
|
|
|
556
562
|
if (options.debounce && options.debounce > 0 || this.customOptions.defaultDebounce && this.customOptions.defaultDebounce > 0) {
|
|
557
563
|
options.listener = this.debounce(
|
|
558
564
|
options.listener,
|
|
559
|
-
options.debounce || this.customOptions.defaultDebounce
|
|
565
|
+
options.debounce || this.customOptions.defaultDebounce,
|
|
566
|
+
options.debounceEdge || this.customOptions.defaultDebounceEdge
|
|
560
567
|
);
|
|
561
568
|
}
|
|
562
569
|
if (this.listenerMap.has(selector))
|
|
@@ -17,10 +17,17 @@ type SelectorOptionsCommon = {
|
|
|
17
17
|
continuous?: boolean;
|
|
18
18
|
/** Whether to debounce the listener to reduce calls to `querySelector` or `querySelectorAll` - set undefined or <=0 to disable (default) */
|
|
19
19
|
debounce?: number;
|
|
20
|
+
/** Whether to call the function at the very first call ("rising" edge) or the very last call ("falling" edge, default) */
|
|
21
|
+
debounceEdge?: "rising" | "falling";
|
|
20
22
|
};
|
|
21
23
|
export type SelectorObserverOptions = {
|
|
22
24
|
/** If set, applies this debounce in milliseconds to all listeners that don't have their own debounce set */
|
|
23
25
|
defaultDebounce?: number;
|
|
26
|
+
/**
|
|
27
|
+
* If set, applies this debounce edge to all listeners that don't have their own set.
|
|
28
|
+
* Edge = Whether to call the function at the very first call ("rising" edge) or the very last call ("falling" edge, default)
|
|
29
|
+
*/
|
|
30
|
+
defaultDebounceEdge?: "rising" | "falling";
|
|
24
31
|
/** Whether to disable the observer when no listeners are present - default is true */
|
|
25
32
|
disableOnNoListeners?: boolean;
|
|
26
33
|
/** Whether to ensure the observer is enabled when a new listener is added - default is true */
|
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.2.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",
|