billy-herrington-utils 1.1.9 → 1.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/package.json +1 -1
- package/src/utils/dom/index.ts +14 -2
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "billy-herrington-utils",
|
|
3
3
|
"description": "daddy told us not to be ashamed of our utils",
|
|
4
|
-
"version": "1.
|
|
4
|
+
"version": "1.2.0",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"keywords": ["utils", "observer", "dom", "fetch", "arrays", "typescript"],
|
|
7
7
|
"author": "smartacephale atm.mormon@protonmail.com (https://github.com/smartacephale)",
|
package/src/utils/dom/index.ts
CHANGED
|
@@ -57,11 +57,22 @@ export function watchElementChildrenCount(element: HTMLElement | Element,
|
|
|
57
57
|
observer.observe(element, { childList: true });
|
|
58
58
|
}
|
|
59
59
|
|
|
60
|
-
export function watchDomChangesWithThrottle(
|
|
61
|
-
|
|
60
|
+
export function watchDomChangesWithThrottle(
|
|
61
|
+
element: HTMLElement | Element,
|
|
62
|
+
callback: () => void,
|
|
63
|
+
throttle = 1000,
|
|
64
|
+
times = Infinity,
|
|
65
|
+
options: Record<string, boolean> = { childList: true, subtree: true, attributes: true }
|
|
66
|
+
) {
|
|
62
67
|
let lastMutationTime: number;
|
|
63
68
|
let timeout: number;
|
|
69
|
+
let times_ = times;
|
|
64
70
|
const observer = new MutationObserver((_mutationList, _observer) => {
|
|
71
|
+
if (times_ !== Infinity && times_ < 1) {
|
|
72
|
+
observer.disconnect();
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
times_--;
|
|
65
76
|
const now = Date.now();
|
|
66
77
|
if (lastMutationTime && now - lastMutationTime < throttle) {
|
|
67
78
|
timeout && clearTimeout(timeout);
|
|
@@ -70,6 +81,7 @@ export function watchDomChangesWithThrottle(element: HTMLElement | Element, call
|
|
|
70
81
|
lastMutationTime = now;
|
|
71
82
|
});
|
|
72
83
|
observer.observe(element, options);
|
|
84
|
+
return observer;
|
|
73
85
|
}
|
|
74
86
|
|
|
75
87
|
export function downloader(options = { append: "", after: "", button: "", cbBefore: () => { } }) {
|