one
Version:
One is a new React Framework that makes Vite serve both native and web.
43 lines (42 loc) • 1.24 kB
JavaScript
function createPrefetchViewport() {
const done = /* @__PURE__ */new Set(),
nodes = /* @__PURE__ */new Map();
let io = null,
onPrefetch = null;
function getObserver() {
return io || (io = new IntersectionObserver(entries => {
for (const entry of entries) {
if (!entry.isIntersecting) continue;
const href = nodes.get(entry.target);
href && !done.has(href) && (done.add(href), onPrefetch?.(href));
}
}, {
threshold: 0.5
}
// fires when mostly visible
), io);
}
function start(prefetch) {
onPrefetch = prefetch;
}
function observe(el, href) {
return nodes.set(el, href), getObserver().observe(el), () => {
nodes.delete(el), io?.unobserve(el), done.delete(href);
};
}
return {
start,
observe,
done,
nodes
};
}
let instance = null;
function startPrefetchViewport(prefetch) {
instance || (instance = createPrefetchViewport()), instance.start(prefetch);
}
function observePrefetchViewport(el, href) {
return instance || (instance = createPrefetchViewport()), instance.observe(el, href);
}
export { createPrefetchViewport, observePrefetchViewport, startPrefetchViewport };
//# sourceMappingURL=prefetchViewport.mjs.map