qsa-observer
A way to be notified about any element that was connected, or disconnected, from a container.
Differently from MutationObserver
- each element is granted to be passed along utilities, whenever it's observed via one, or more, selectors
- an element will pass through a
setup(element, selector)
utility, and per each matching selector - elements injected through
innerHTML
, within nested elements, will be parsed too - all elements that had a setup will pass through an
update(element, connected)
utility
Example
import QSAO from 'qsa-observer';
const query = ['div'];
const root = document;
const {
flush,
observer,
parse
} = QSAO({
query,
root,
handle(element, connected, selector, index) {
const event = connected ? 'connected' : 'disconnected';
console.log(element, event, selector, index);
}
});
const observeMore = selector => {
if (!query.includes(selector)) {
query.push(selector);
parse(root.querySelectorAll(selector));
}
};
setTimeout(observeMore, 1000, 'p');