Skip to content

Commit 79e9c8a

Browse files
committed
docs: SelectorObserver debounce documentation
1 parent 9868809 commit 79e9c8a

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

.github/assets/debounce.png

347 Bytes
Loading

README.md

+14-5
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ Additionally, there are the following extra options:
159159
- `disableOnNoListeners` - whether to disable the SelectorObserver when there are no listeners left (defaults to false)
160160
- `enableOnAddListener` - whether to enable the SelectorObserver when a new listener is added (defaults to true)
161161
- `defaultDebounce` - if set to a number, this debounce will be applied to every listener that doesn't have a custom debounce set (defaults to 0)
162+
- `defaultDebounceEdge` - can be set to "falling" (default) or "rising", to call the function at (rising) on the very first call and subsequent times after the given debounce time or (falling) the very last call after the debounce time passed with no new calls - [see `debounce()` for more info and a diagram](#debounce)
162163

163164
⚠️ Make sure to call `enable()` to actually start observing. This will need to be done after the DOM has loaded (when using `@run-at document-end` or after `DOMContentLoaded` has fired) **and** as soon as the `baseElement` or `baseElementSelector` is available.
164165

@@ -177,16 +178,19 @@ The listener will be called immediately if the selector already exists in the DO
177178
> This will also include elements that were already found in a previous listener call.
178179
> If set to false (default), querySelector() will be used and only the first matching element will be returned.
179180
180-
> If `options.continuous` is set to true, the listener will not be deregistered after it was called once (defaults to false).
181+
> If `options.continuous` is set to true, this listener will not be deregistered after it was called once (defaults to false).
181182
>
182-
> ⚠️ You should keep usage of this option to a minimum, as it will cause the listener to be called every time the selector is *checked for and found* and this can stack up quite quickly.
183+
> ⚠️ You should keep usage of this option to a minimum, as it will cause this listener to be called every time the selector is *checked for and found* and this can stack up quite quickly.
183184
> ⚠️ You should try to only use this option on SelectorObserver instances that are scoped really low in the DOM tree to prevent as many selector checks as possible from being triggered.
184185
> ⚠️ I also recommend always setting a debounce time (see constructor or below) if you use this option.
185186
186-
> If `options.debounce` is set to a number above 0, the listener will be debounced by that amount of milliseconds (defaults to 0).
187-
> E.g. if the debounce time is set to 200 and the selector is found twice within 100ms, only the last call of the listener will be executed.
187+
> If `options.debounce` is set to a number above 0, this listener will be debounced by that amount of milliseconds (defaults to 0).
188+
> E.g. if the debounce time is set to 200 and the selector is found twice within 100ms, only the last call of this listener will be executed.
189+
190+
> `options.debounceEdge` is set to "falling" by default, which means the debounce timer will start after the last call of this listener.
191+
> If set to "rising", the debounce timer will start after the first call of this listener.
188192
189-
> When using TypeScript, the generic `TElement` can be used to specify the type of the element(s) that the listener will return.
193+
> When using TypeScript, the generic `TElement` can be used to specify the type of the element(s) that this listener will return.
190194
> It will default to HTMLElement if left undefined.
191195
192196
<br>
@@ -262,6 +266,9 @@ document.addEventListener("DOMContentLoaded", () => {
262266
attributeFilter: ["class", "style", "data-whatever"],
263267
// debounce all listeners by 100ms unless specified otherwise:
264268
defaultDebounce: 100,
269+
// "rising" means listeners are called immediately and use the debounce as a timeout between subsequent calls - see the debounce() function for a better explanation
270+
defaultDebounceEdge: "rising",
271+
// other settings from the MutationObserver API can be set here too - see https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver/observe#options
265272
});
266273

267274
barObserver.addListener("#my-element", {
@@ -273,6 +280,8 @@ document.addEventListener("DOMContentLoaded", () => {
273280
barObserver.addListener("#my-other-element", {
274281
// set the debounce higher than provided by the defaultDebounce property:
275282
debounce: 250,
283+
// adjust the debounceEdge back to the default "falling" for this specific listener:
284+
debounceEdge: "falling",
276285
listener: (element) => {
277286
console.log("Other element's attributes changed:", element);
278287
},

0 commit comments

Comments
 (0)