UNPKG

@ideal-postcodes/jsutil

Version:

Browser Address Autocomplete for api.ideal-postcodes.co.uk

57 lines (56 loc) 1.61 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.watchChange = exports.watchMutation = exports.watchTimer = void 0; const debounce_1 = require("./debounce"); const watchTimer = ({ bind, interval = 1000 }) => { let timer = null; const start = () => { timer = window.setInterval(() => { try { bind(); } catch (e) { stop(); } }, interval); return timer; }; const stop = () => { if (timer === null) return; window.clearInterval(timer); timer = null; }; return { start, stop }; }; exports.watchTimer = watchTimer; const watchMutation = ({ bind, interval = 1000, target = window.document, observerConfig = { subtree: true, childList: true, }, }) => { const observer = new MutationObserver((0, debounce_1.debounce)(() => { try { bind(); } catch (e) { stop(); } }, interval)); const start = () => { observer.observe(target, observerConfig); return null; }; const stop = () => observer.disconnect(); return { start, stop }; }; exports.watchMutation = watchMutation; const watchChange = (options) => { if (!window) return (0, exports.watchTimer)(options); if (!window.MutationObserver) return (0, exports.watchTimer)(options); if (options.mutationObserver) return (0, exports.watchMutation)(options); return (0, exports.watchTimer)(options); }; exports.watchChange = watchChange;