- From: Andrea Giammarchi <notifications@github.com>
- Date: Fri, 19 Oct 2018 03:02:44 -0700
- To: w3c/webcomponents <webcomponents@noreply.github.com>
- Cc: Subscribed <subscribed@noreply.github.com>
- Message-ID: <w3c/webcomponents/issues/551/431312625@github.com>
Right ... little variation that uses a `WeakMap` instead and it's also observing children for all occasions
```js
const HTMLParsedElement = (() => {
const DCL = 'DOMContentLoaded';
const init = new WeakMap;
const isParsed = el => {
do {
if (el.nextSibling)
return true;
} while (el = el.parentNode);
return false;
};
const cleanUp = (el, observer, onDCL) => {
observer.disconnect();
el.ownerDocument.removeEventListener(DCL, onDCL);
init.set(el, true);
parsedCallback(el);
};
const parsedCallback = el => el.parsedCallback();
return class HTMLParsedElement extends HTMLElement {
connectedCallback() {
if ('parsedCallback' in this && !init.has(this)) {
init.set(this, false);
if (document.readyState === 'complete' || isParsed(this))
Promise.resolve(this).then(parsedCallback);
else {
const onDCL = () => cleanUp(this, observer, onDCL);
this.ownerDocument.addEventListener(DCL, onDCL);
const observer = new MutationObserver((changes) => {
changes.some(record => {
if (isParsed(this)) {
cleanUp(this, observer, onDCL);
return true;
}
});
});
observer.observe(this.parentNode, {childList: true, subtree: true});
}
}
}
get parsed() {
return init.get(this) === true;
}
};
})();
```
I think I'll publish this one to npm.
--
You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub:
https://github.com/w3c/webcomponents/issues/551#issuecomment-431312625
Received on Friday, 19 October 2018 10:03:18 UTC