Skip to content

Commit

Permalink
golf: use Set & share prefetch caller;
Browse files Browse the repository at this point in the history
- 873 gz / 719 br
  • Loading branch information
lukeed committed Dec 10, 2018
1 parent 1037e38 commit d221c51
Showing 1 changed file with 9 additions and 10 deletions.
19 changes: 9 additions & 10 deletions src/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,16 @@
import prefetch from './prefetch.mjs';
import requestIdleCallback from './request-idle-callback.mjs';

const loaderFunctions = new Map();
const toPrefetch = new Set();

const observer = new IntersectionObserver(entries => {
entries.forEach(entry => {
if (entry.isIntersecting) {
const url = entry.target.href;
const fn = loaderFunctions.get(url);
if (fn) fn.call(null);
if (toPrefetch.has(url)) {
toPrefetch.delete(url);
prefetch(url, observer.priority);
}
}
});
});
Expand Down Expand Up @@ -56,16 +59,12 @@ export default function (options) {
return;
}

observer.priority = options.priority;

// If not, find all links and use IntersectionObserver.
Array.from(options.el.querySelectorAll('a'), link => {
observer.observe(link);

// Generate loader functions for each link
const uri = link.href;
loaderFunctions.set(uri, () => {
loaderFunctions.delete(uri);
prefetch(uri, options.priority);
});
toPrefetch.add(link.href);
});
}, {timeout: options.timeout});
}

0 comments on commit d221c51

Please sign in to comment.