Skip to content

Commit

Permalink
start queue only after calling main function
Browse files Browse the repository at this point in the history
  • Loading branch information
gijo-varghese committed Sep 12, 2019
1 parent d3da0c3 commit 1ae7b16
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
26 changes: 15 additions & 11 deletions flying-pages.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,16 @@ const observer = new IntersectionObserver(entries => {
});

// Queue that process requests based on max RPS (requests per second)
const queue = setInterval(() => {
Array.from(toPrefetch)
.slice(0, window.FPConfig.maxRPS)
.forEach(url => {
prefetchWithTimeout(url);
alreadyPrefetched.add(url);
toPrefetch.delete(url);
});
}, 1000);
const startQueue = () =>
setInterval(() => {
Array.from(toPrefetch)
.slice(0, window.FPConfig.maxRPS)
.forEach(url => {
prefetchWithTimeout(url);
alreadyPrefetched.add(url);
toPrefetch.delete(url);
});
}, 1000);

let hoverTimer = null;

Expand Down Expand Up @@ -129,8 +130,8 @@ const stopPreloading = () => {
// Find all links are remove it from observer (viewport)
document.querySelectorAll("a").forEach(e => observer.unobserve(e));

// Stop processing queue
clearInterval(queue);
// Clear pending links in queue
toPrefetch.clear();

// Remove event listeners for mouse hover
document.removeEventListener("mouseover", mouseOverListener, true);
Expand All @@ -152,6 +153,9 @@ function flyingPages(options = {}) {
// Combine default options with received options to create the new config and set the config in window for easy access
window.FPConfig = Object.assign(defaultOptions, options);

// Start Queue
startQueue();

// Start preloading links in viewport on idle callback, with a delay
requestIdleCallback(() =>
setTimeout(
Expand Down
2 changes: 1 addition & 1 deletion flying-pages.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 1ae7b16

Please sign in to comment.