Skip to content

Instantly share code, notes, and snippets.

@jameswomack
Created December 14, 2017 00:54
Show Gist options
  • Select an option

  • Save jameswomack/a246fe8a857397e77a044b9855697a66 to your computer and use it in GitHub Desktop.

Select an option

Save jameswomack/a246fe8a857397e77a044b9855697a66 to your computer and use it in GitHub Desktop.
Observing Undefined Custom Elements / Web Components
o = new MutationObserver(mutationRecords => {
const shouldCheck = mutationRecords.some(mutationRecord => mutationRecord.type === 'childList' && mutationRecord.addedNodes.length)
const addedNodes = mutationRecords.reduce((aN, mutationRecord) => aN.concat(...mutationRecord.addedNodes), [])
const undefinedNodes = document.querySelectorAll(':not(:defined)')
if (shouldCheck) {
console.info(undefinedNodes, addedNodes);
[...undefinedNodes].forEach(n => customElements.whenDefined(n.localName).then(() => console.info(`${n.localName} defined`)))
}
})
o.observe(document.body, { attributes: true, childList: true })
class FooDoozzz extends HTMLElement { connectedCallback () { this.textContent = 'FUUUUUCK' } }
document.body.appendChild(document.createElement('foo-doozzz'))
customElements.define('foo-doozzz', FooDoozzz)
document.body.appendChild(document.createElement('foo-doozzz'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment