Skip to content

Commit e4923ee

Browse files
committed
Clean up promises
1 parent d1e825d commit e4923ee

2 files changed

Lines changed: 65 additions & 85 deletions

File tree

src/index.mjs

Lines changed: 34 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -25,27 +25,23 @@ import requestIdleCallback from './request-idle-callback.mjs';
2525
* @return {Promise} resolving with list of URLs found
2626
*/
2727
function 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
*/
7773
export 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
}

src/prefetch.mjs

Lines changed: 31 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -94,25 +94,17 @@ function xhrPrefetchStrategy(url) {
9494
* @return {Object} a Promise
9595
*/
9696
function highPriFetchStrategy(url) {
97-
return new Promise((resolve, reject) => {
98-
// TODO: Investigate using preload for high-priority
99-
// fetches. May have to sniff file-extension to provide
100-
// valid 'as' values. In the future, we may be able to
101-
// use Priority Hints here.
102-
if (self.fetch === undefined) {
103-
xhrPrefetchStrategy(url)
104-
.then(() => {
105-
resolve();
106-
});
107-
} else {
108-
// As of 2018, fetch() is high-priority in Chrome
109-
// and medium-priority in Safari.
110-
fetch(url, {credentials: `include`})
111-
.then(() => {
112-
resolve();
113-
});
114-
}
115-
});
97+
// TODO: Investigate using preload for high-priority
98+
// fetches. May have to sniff file-extension to provide
99+
// valid 'as' values. In the future, we may be able to
100+
// use Priority Hints here.
101+
if (self.fetch === undefined) {
102+
return xhrPrefetchStrategy(url);
103+
} else {
104+
// As of 2018, fetch() is high-priority in Chrome
105+
// and medium-priority in Safari.
106+
return fetch(url, {credentials: `include`});
107+
}
116108
};
117109

118110
const supportedPrefetchStrategy = support(`prefetch`)
@@ -125,40 +117,32 @@ const supportedPrefetchStrategy = support(`prefetch`)
125117
* @param {string} priority - preferred fetch priority (`low` or `high`)
126118
* @return {Object} a Promise
127119
*/
128-
function prefetcher(url, priority) {
129-
return new Promise(resolve => {
130-
if ('connection' in navigator) {
131-
// Don't prefetch if the user is on 2G..
132-
if (navigator.connection.effectiveType && /\slow-2g|2g/.test(navigator.connection.effectiveType)) {
133-
resolve();
134-
return;
135-
}
136-
// Don't prefetch if Save-Data is enabled..
137-
if (navigator.connection.saveData) {
138-
resolve();
139-
return;
140-
}
120+
async function prefetcher(url, priority) {
121+
if (preFetched[url]) {
122+
return;
123+
}
124+
125+
if ('connection' in navigator) {
126+
// Don't prefetch if the user is on 2G..
127+
if ((navigator.connection.effectiveType || "").includes("2g")) {
128+
return;
141129
}
142-
if (preFetched[url]) {
143-
resolve();
130+
// Don't prefetch if Save-Data is enabled..
131+
if (navigator.connection.saveData) {
144132
return;
145133
}
134+
}
135+
136+
try {
146137
if (priority && priority === `high`) {
147-
highPriFetchStrategy(url)
148-
.then(() => {
149-
resolve();
150-
preFetched[url] = true;
151-
})
152-
.catch(() => { });
138+
await highPriFetchStrategy(url);
153139
} else {
154-
supportedPrefetchStrategy(url)
155-
.then(() => {
156-
resolve();
157-
preFetched[url] = true;
158-
})
159-
.catch(() => { });
140+
await supportedPrefetchStrategy(url);
160141
};
161-
});
142+
preFetched[url] = true;
143+
} catch(e) {
144+
// Wanna do something?
145+
}
162146
};
163147

164148
export default prefetcher;

0 commit comments

Comments
 (0)