UNPKG

@dvcol/svelte-utils

Version:

Svelte library for common utility functions and constants

27 lines (26 loc) 788 B
const callbacks = new WeakMap(); let observer; export const resize = (target, parameters) => { if (!observer) { observer = new ResizeObserver(entries => entries.forEach((entry, i) => { const callback = callbacks.get(entry.target); callback?.(entry, i, entries); })); } function destroy() { observer.unobserve(target); callbacks.delete(target); } function update(options) { destroy(); const cb = typeof options === 'function' ? options : options.callback; const opt = typeof options === 'function' ? undefined : options.options; callbacks.set(target, cb); observer.observe(target, opt); } update(parameters); return { destroy, update, }; };