Skip to content

Commit 0173235

Browse files
committed
feat: change debounce edge in selectorobserver
1 parent c0824a3 commit 0173235

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

.changeset/tricky-keys-change.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@sv443-network/userutils": minor
3+
---
4+
5+
Add property to change the debounce edge in `SelectorObserver` instances

lib/SelectorObserver.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { debounce } from "./misc";
2+
13
/** Options for the `onSelector()` method of {@linkcode SelectorObserver} */
24
export type SelectorListenerOptions<TElem extends Element = HTMLElement> = SelectorOptionsOne<TElem> | SelectorOptionsAll<TElem>;
35

@@ -20,11 +22,18 @@ type SelectorOptionsCommon = {
2022
continuous?: boolean;
2123
/** Whether to debounce the listener to reduce calls to `querySelector` or `querySelectorAll` - set undefined or <=0 to disable (default) */
2224
debounce?: number;
25+
/** Whether to call the function at the very first call ("rising" edge) or the very last call ("falling" edge, default) */
26+
debounceEdge?: "rising" | "falling";
2327
};
2428

2529
export type SelectorObserverOptions = {
2630
/** If set, applies this debounce in milliseconds to all listeners that don't have their own debounce set */
2731
defaultDebounce?: number;
32+
/**
33+
* If set, applies this debounce edge to all listeners that don't have their own set.
34+
* Edge = Whether to call the function at the very first call ("rising" edge) or the very last call ("falling" edge, default)
35+
*/
36+
defaultDebounceEdge?: "rising" | "falling";
2837
/** Whether to disable the observer when no listeners are present - default is true */
2938
disableOnNoListeners?: boolean;
3039
/** Whether to ensure the observer is enabled when a new listener is added - default is true */
@@ -63,6 +72,7 @@ export class SelectorObserver {
6372

6473
const {
6574
defaultDebounce,
75+
defaultDebounceEdge,
6676
disableOnNoListeners,
6777
enableOnAddListener,
6878
...observerOptions
@@ -76,6 +86,7 @@ export class SelectorObserver {
7686

7787
this.customOptions = {
7888
defaultDebounce: defaultDebounce ?? 0,
89+
defaultDebounceEdge: defaultDebounceEdge ?? "rising",
7990
disableOnNoListeners: disableOnNoListeners ?? false,
8091
enableOnAddListener: enableOnAddListener ?? true,
8192
};
@@ -123,12 +134,8 @@ export class SelectorObserver {
123134
}
124135
}
125136

126-
private debounce<TArgs>(func: (...args: TArgs[]) => void, time: number): (...args: TArgs[]) => void {
127-
let timeout: number;
128-
return function(...args: TArgs[]) {
129-
clearTimeout(timeout);
130-
timeout = setTimeout(() => func.apply(this, args), time) as unknown as number;
131-
};
137+
private debounce<TArgs>(func: (...args: TArgs[]) => void, time: number, edge: "falling" | "rising" = "falling"): (...args: TArgs[]) => void {
138+
return debounce(func, time, edge);
132139
}
133140

134141
/**
@@ -146,6 +153,7 @@ export class SelectorObserver {
146153
options.listener = this.debounce(
147154
options.listener as ((arg: NodeListOf<Element> | Element) => void),
148155
(options.debounce || this.customOptions.defaultDebounce)!,
156+
(options.debounceEdge || this.customOptions.defaultDebounceEdge),
149157
);
150158
}
151159
if(this.listenerMap.has(selector))

0 commit comments

Comments
 (0)