one
Version:
One is a new React Framework that makes Vite serve both native and web.
54 lines (53 loc) • 1.32 kB
JavaScript
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: 0.5
}
// fires when mostly visible
);
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