@@ -25,27 +25,23 @@ import requestIdleCallback from './request-idle-callback.mjs';
2525 * @return {Promise } resolving with list of URLs found
2626 */
2727function fetchInViewportLinks ( el , options ) {
28- return new Promise ( ( resolve , reject ) => {
29- const urls = [ ] ;
30- const links = el . querySelectorAll ( 'a' ) ;
31- const observer = new IntersectionObserver ( entries => {
32- entries . forEach ( entry => {
33- // Link is in the view
34- if ( entry . intersectionRatio > 0 ) {
35- urls . push ( entry . target . href ) ;
36- } else {
37- // Link is out of the view
38- }
28+ const links = Array . from ( el . querySelectorAll ( 'a' ) ) ;
29+ const observer = new IntersectionObserver ( entries => {
30+ const urls = entries
31+ . filter ( entry => entry . isIntersecting )
32+ . map ( entry => {
33+ observer . unobserve ( entry . target ) ;
34+ return entry . target . href ;
3935 } ) ;
40- // prefetch() maintains a list of in-memory URLs
41- // previously fetched so we don't attempt a refetch
42- prefetchURLs ( urls , options . priority ) ;
43- resolve ( urls ) ;
44- } ) ;
45- links . forEach ( link => {
46- observer . observe ( link ) ;
47- } ) ;
36+ // prefetch() maintains a list of in-memory URLs
37+ // previously fetched so we don't attempt a refetch
38+ prefetchURLs ( urls , options . priority ) ;
4839 } ) ;
40+ links . forEach ( link => {
41+ observer . observe ( link ) ;
42+ } ) ;
43+ // Return a list of found URLs
44+ return links . map ( link => link . href ) ;
4945} ;
5046
5147/**
@@ -54,7 +50,7 @@ function fetchInViewportLinks(el, options) {
5450 * @param {Array } urls - Array of URLs to prefetch
5551 * @param {string } priority - "priority" of the request
5652 */
57- const prefetchURLs = function ( urls , priority ) {
53+ function prefetchURLs ( urls , priority ) {
5854 urls . forEach ( url => {
5955 prefetch ( url , priority ) ;
6056 } ) ;
@@ -75,24 +71,24 @@ const prefetchURLs = function (urls, priority) {
7571 * @return {Object } Promise
7672 */
7773export default function ( options ) {
78- return new Promise ( ( resolve , reject ) => {
79- options = options || {
74+ options = {
75+ ... {
8076 priority : 'low' ,
8177 timeout : 2000 ,
82- } ;
83- const timeoutFn = options . timeoutFn || requestIdleCallback ;
84- timeoutFn ( ( ) => {
85- // Prefetch an array of URLs if supplied (as an override)
86- if ( options . urls !== undefined && options . urls . length > 0 ) {
87- prefetchURLs ( options . urls , options . priority ) ;
88- resolve ( options . urls ) ;
89- } else {
90- // Element to extract in-viewport links for
91- const el = options . el || document ;
92- fetchInViewportLinks ( el , options ) . then ( urls => {
93- resolve ( urls ) ;
94- } ) ;
95- }
96- } , { timeout : options . timeout } ) ;
97- } ) ;
78+ timeoutFn : requestIdleCallback ,
79+ el : document
80+ } ,
81+ ... options
82+ } ;
83+
84+ options . timeoutFn ( ( ) => {
85+ // Prefetch an array of URLs if supplied (as an override)
86+ if ( options . urls !== undefined && options . urls . length > 0 ) {
87+ prefetchURLs ( options . urls , options . priority ) ;
88+ return options . urls ;
89+ } else {
90+ // Element to extract in-viewport links for
91+ return fetchInViewportLinks ( options . el , options ) ;
92+ }
93+ } , { timeout : options . timeout } ) ;
9894}
0 commit comments