UNPKG

@dvcol/svelte-utils

Version:

Svelte library for common utility functions and constants

29 lines (28 loc) 740 B
/** * Watch for mutations on an element. * * @see https://github.com/whatwg/dom/issues/126 * @param node * @param parameters */ export const mutation = (node, parameters) => { let observer = null; function destroy() { observer?.disconnect(); observer = null; } function update(options) { destroy(); const cb = typeof options === 'function' ? options : options.callback; const opt = typeof options === 'function' ? { childList: true } : options.options; observer = new MutationObserver(mutations => { mutations.forEach(cb); }); observer.observe(node, opt); } update(parameters); return { update, destroy, }; };