UNPKG

one

Version:

One is a React framework for web, iOS, and Android apps.

51 lines (49 loc) • 1.2 kB
function createPrefetchViewport() { const done = /* @__PURE__ */ new Set(); const nodes = /* @__PURE__ */ new Map(); let io = null; let onPrefetch = null; function getObserver() { if (io) return io; io = new IntersectionObserver((entries) => { for (const entry of entries) { if (!entry.isIntersecting) continue; const href = nodes.get(entry.target); if (href && !done.has(href)) { done.add(href); onPrefetch?.(href); } } }, { threshold: .5 }); return io; } function start(prefetch) { onPrefetch = prefetch; } function observe(el, href) { nodes.set(el, href); getObserver().observe(el); return () => { nodes.delete(el); io?.unobserve(el); done.delete(href); }; } return { start, observe, done, nodes }; } let instance = null; function startPrefetchViewport(prefetch) { if (!instance) instance = createPrefetchViewport(); instance.start(prefetch); } function observePrefetchViewport(el, href) { if (!instance) instance = createPrefetchViewport(); return instance.observe(el, href); } export { createPrefetchViewport, observePrefetchViewport, startPrefetchViewport }; //# sourceMappingURL=prefetchViewport.mjs.map