Skip to content

Instantly share code, notes, and snippets.

@allenhwkim
Created September 20, 2021 02:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save allenhwkim/db759c0cf56427ea436029ebf4508aa9 to your computer and use it in GitHub Desktop.
Save allenhwkim/db759c0cf56427ea436029ebf4508aa9 to your computer and use it in GitHub Desktop.
fireDOMChange(el) and waitForIdle(el)
function fireDOMChange(el, options={attributes: false, childList: true, subtree: true}) {
const observer = new MutationObserver(list => el.dispatchEvent(new CustomEvent('dom-change', {detail: list})));
observer.observe(el, options);
}
function waitForIdle(el, options) {
options = Object.assign({
wait: 100, attributes: false, childList: true, subtree: true
}, options);
return new Promise(resolve => {
const observer = new MutationObserver(list => {
el.dispatchEvent(new CustomEvent('dom-changed', {detail: list, bubbles: true}));
});
observer.observe(el, options);
const domChangeListener = debounce(debounceFunc, options.wait)();
function debounceFunc() {
resolve(el);
observer.disconnect(el);
el.removeEventListener('dom-change', domChangeListener);
}
el.addEventListener('dom-change', domChangeListener);
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment