one
Version:
One is a new React Framework that makes Vite serve both native and web.
128 lines (127 loc) • 3.9 kB
JavaScript
function createPrefetchIntent(options) {
var {
onPrefetch,
maxReach = 500,
perpWeight = 5,
minSpeed = 8
} = options,
done = /* @__PURE__ */new Set(),
rects = [],
px = 0,
py = 0,
vx = 0,
vy = 0,
moveCount = 0,
lastPrefetchMove = 0;
function setRects(newRects) {
rects = newRects.filter(function (r) {
return !done.has(r.h);
});
}
function move(x, y, dx, dy) {
moveCount++;
var smooth = moveCount < 3 ? 0.3 : 0.6;
vx = vx * smooth + dx * (1 - smooth), vy = vy * smooth + dy * (1 - smooth), px = x, py = y;
var speed = Math.sqrt(vx * vx + vy * vy);
if (!(speed < minSpeed)) {
var ux = vx / speed,
uy = vy / speed,
best = 1 / 0,
href = "",
_iteratorNormalCompletion = !0,
_didIteratorError = !1,
_iteratorError = void 0;
try {
for (var _iterator = rects[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = !0) {
var {
r,
h
} = _step.value,
cx = (r.left + r.right) / 2 - px,
cy = (r.top + r.bottom) / 2 - py,
dist = Math.sqrt(cx * cx + cy * cy),
along = cx * ux + cy * uy;
if (!(along < 0)) {
var perpX = cx - along * ux,
perpY = cy - along * uy,
perp = Math.sqrt(perpX * perpX + perpY * perpY),
baseRadius = Math.max(r.width, r.height) / 2 + 30,
distanceFactor = Math.max(0.2, 1 - dist / 1e3),
radius = baseRadius * distanceFactor + 20;
if (!(perp > radius)) {
var score = along + perp * perpWeight;
score < best && (best = score, href = h);
}
}
}
} catch (err) {
_didIteratorError = !0, _iteratorError = err;
} finally {
try {
!_iteratorNormalCompletion && _iterator.return != null && _iterator.return();
} finally {
if (_didIteratorError) throw _iteratorError;
}
}
href && best < maxReach && moveCount - lastPrefetchMove > 1 && (done.add(href), rects = rects.filter(function (r2) {
return r2.h !== href;
}), lastPrefetchMove = moveCount, onPrefetch(href));
}
}
var nodes = /* @__PURE__ */new Map();
function observe(el, href) {
return nodes.set(el, href), function () {
nodes.delete(el), done.delete(href);
};
}
return {
setRects,
move,
observe,
nodes,
done
};
}
var instance = null,
started = !1;
function startPrefetchIntent(onPrefetch) {
if (started) return instance;
started = !0, instance = createPrefetchIntent({
onPrefetch
});
var frame = 0,
px = 0,
py = 0;
function measure() {
if (!instance.nodes.size) return setTimeout(measure, 300);
var io = new IntersectionObserver(function (entries) {
io.disconnect(), instance.setRects(entries.filter(function (e) {
return e.isIntersecting;
}).map(function (e) {
return {
r: e.boundingClientRect,
h: instance.nodes.get(e.target)
};
}).filter(function (x) {
return x.h;
})), setTimeout(measure, 300);
});
instance.nodes.forEach(function (_, el) {
return io.observe(el);
});
}
return measure(), document.addEventListener("mousemove", function (e) {
if (!(++frame % 4)) {
var dx = e.clientX - px,
dy = e.clientY - py;
px = e.clientX, py = e.clientY, instance.move(px, py, dx, dy);
}
}, {
passive: !0
}), instance;
}
function observePrefetchIntent(el, href) {
return instance ? instance.observe(el, href) : function () {};
}
export { createPrefetchIntent, observePrefetchIntent, startPrefetchIntent };
//# sourceMappingURL=prefetchIntent.native.js.map