Skip to content

Commit

Permalink
tidy up
Browse files Browse the repository at this point in the history
  • Loading branch information
Rich-Harris committed Nov 27, 2022
1 parent cfe78b2 commit e3428a2
Showing 1 changed file with 18 additions and 34 deletions.
52 changes: 18 additions & 34 deletions packages/kit/src/runtime/client/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -1188,59 +1188,43 @@ export function create_client({ target, base }) {

clearTimeout(mousemove_timeout);
mousemove_timeout = setTimeout(() => {
prepare_and_preload(target, 2);
preload(target, 2);
}, 20);
});

target.addEventListener(
'touchstart',
(event) => {
prepare_and_preload(/** @type {Element} */ (event.composedPath()[0]), 1);
},
{ passive: true }
);
/** @param {Event} event */
function tap(event) {
preload(/** @type {Element} */ (event.composedPath()[0]), 1);
}

target.addEventListener('keydown', (event) => {
if (event.key === 'Enter' && event.target === document.activeElement) {
prepare_and_preload(/** @type {Element} */ (event.composedPath()[0]), 1);
}
});
target.addEventListener('mousedown', tap);
target.addEventListener('touchstart', tap, { passive: true });

const observer = new IntersectionObserver(
(entries, observer) => {
(entries) => {
for (const entry of entries) {
if (entry.isIntersecting) {
preload_code(new URL(/** @type {HTMLAnchorElement} */ (entry.target).href).pathname);
observer.unobserve(entry.target);
}
}
},
{
threshold: 0
}
{ threshold: 0 }
);

/**
* @param {Element} element
* @param {number} priority
*/
function prepare_and_preload(element, priority) {
const { url, options, has } = find_anchor(element, base);

const ignore =
!url ||
is_external_url(url, base) ||
options.reload ||
has.rel_external ||
has.target ||
has.download;

if (ignore) return;

if (priority <= options.preload_data) {
preload_data(url);
} else if (priority <= options.preload_code) {
preload_code(url.pathname);
function preload(element, priority) {
const { url, options, external } = find_anchor(element, base);

if (!external) {
if (priority <= options.preload_data) {
preload_data(/** @type {URL} */ (url));
} else if (priority <= options.preload_code) {
preload_code(/** @type {URL} */ (url).pathname);
}
}
}

Expand Down

0 comments on commit e3428a2

Please sign in to comment.