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 {
matches,
observer,
parse
} = QSAO({
query,
root,
cache: new WeakSet,
setup(element, selector) {
this.cache.add(element);
console.log(element, selector, 'initialized');
this.update(element, true);
},
update(element, connected) {
console.log(element, connected ? 'connected' : 'disconnected');
}
});
const observeMore = selector => {
if (!query.includes(selector)) {
query.push(selector);
parse(root.querySelectorAll(selector));
}
};
setTimeout(observeMore, 1000, 'p');