one
Version:
One is a new React Framework that makes Vite serve both native and web.
150 lines (149 loc) • 4 kB
JavaScript
function createPrefetchIntent(options) {
var {
onPrefetch,
maxReach = 500,
perpWeight = 5,
minSpeed = 8
} = options;
var done = /* @__PURE__ */new Set();
var rects = [];
var px = 0,
py = 0,
vx = 0,
vy = 0;
var moveCount = 0;
var 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) return;
var ux = vx / speed;
var uy = vy / speed;
var best = Infinity;
var href = "";
var _iteratorNormalCompletion = true,
_didIteratorError = false,
_iteratorError = void 0;
try {
for (var _iterator = rects[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
var {
r,
h
} = _step.value;
var cx = (r.left + r.right) / 2 - px;
var cy = (r.top + r.bottom) / 2 - py;
var dist = Math.sqrt(cx * cx + cy * cy);
var along = cx * ux + cy * uy;
if (along < 0) continue;
var perpX = cx - along * ux;
var perpY = cy - along * uy;
var perp = Math.sqrt(perpX * perpX + perpY * perpY);
var baseRadius = Math.max(r.width, r.height) / 2 + 30;
var distanceFactor = Math.max(0.2, 1 - dist / 1e3);
var radius = baseRadius * distanceFactor + 20;
if (perp > radius) continue;
var score = along + perp * perpWeight;
if (score < best) {
best = score;
href = h;
}
}
} catch (err) {
_didIteratorError = true;
_iteratorError = err;
} finally {
try {
if (!_iteratorNormalCompletion && _iterator.return != null) {
_iterator.return();
}
} finally {
if (_didIteratorError) {
throw _iteratorError;
}
}
}
if (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) {
nodes.set(el, href);
return function () {
nodes.delete(el);
done.delete(href);
};
}
return {
setRects,
move,
observe,
nodes,
done
};
}
var instance = null;
var started = false;
function startPrefetchIntent(onPrefetch) {
if (started) return instance;
started = true;
instance = createPrefetchIntent({
onPrefetch
});
var frame = 0;
var 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);
});
}
measure();
document.addEventListener("mousemove", function (e) {
if (++frame % 4) return;
var dx = e.clientX - px;
var dy = e.clientY - py;
px = e.clientX;
py = e.clientY;
instance.move(px, py, dx, dy);
}, {
passive: true
});
return instance;
}
function observePrefetchIntent(el, href) {
if (!instance) return function () {};
return instance.observe(el, href);
}
export { createPrefetchIntent, observePrefetchIntent, startPrefetchIntent };
//# sourceMappingURL=prefetchIntent.native.js.map