Skip to content

Commit

Permalink
refactor: Making the intersect observer quicker
Browse files Browse the repository at this point in the history
Signed-off-by: Vincent Boutour <[email protected]>
  • Loading branch information
ViBiOh committed Nov 5, 2022
1 parent bf74698 commit be0d09c
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 45 deletions.
84 changes: 40 additions & 44 deletions cmd/fibr/static/scripts/async-image.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,34 @@ async function fetchThumbnail() {
throw new Error('unable to load thumbnails');
}

let lazyImageObserver;

if (typeof lazyLoadThumbnail !== 'undefined' && lazyLoadThumbnail) {
lazyImageObserver = new IntersectionObserver(async (entries, observer) => {
for (const entry of entries) {
if (!entry.isIntersecting) {
continue;
}

const lazyImage = entry.target;
if (window.webpHero) {
const response = await fetch(lazyImage.dataset.src, {
credentials: 'same-origin',
});
const content = await response.arrayBuffer();

const webpMachine = new webpHero.WebpMachine();
lazyImage.src = await webpMachine.decode(new Uint8Array(content));
webpMachine.clearCache();
} else {
lazyImage.src = lazyImage.dataset.src;
}

lazyImageObserver.unobserve(lazyImage);
}
});
}

for await (let chunk of readChunk(response)) {
const commaIndex = chunk.findIndex((element) => element === 44);
if (commaIndex === -1) {
Expand All @@ -123,6 +151,10 @@ async function fetchThumbnail() {
img.classList.add('thumbnail', 'full', 'block');

replaceContent(picture, img);

if (lazyImageObserver !== undefined) {
lazyImageObserver.observe(img);
}
}
}

Expand Down Expand Up @@ -155,12 +187,6 @@ document.addEventListener(
replaceContent(picture, generateThrobber(['throbber-white']));
});

try {
await fetchThumbnail();
} catch (e) {
console.error(e);
}

try {
await isWebPCompatible();
} catch (e) {
Expand All @@ -169,49 +195,19 @@ document.addEventListener(
'sha512-DA6h9H5Sqn55/uVn4JI4aSPFnAWoCQYYDXUnvjOAMNVx11///hX4QaFbQt5yWsrIm9hSI5fLJYfRWt3KXneSXQ==',
'anonymous',
);
}

try {
await fetchThumbnail();
} catch (e) {
console.error(e);
}

if (window.webpHero) {
const webpMachine = new webpHero.WebpMachine();
webpMachine.polyfillDocument();
webpMachine.clearCache();
}

window.dispatchEvent(new Event('thumbnail-done'));
},
false,
);

window.addEventListener('thumbnail-done', () => {
if (typeof lazyLoadThumbnail === 'undefined' || !lazyLoadThumbnail) {
return;
}

const lazyImageObserver = new IntersectionObserver(
async (entries, observer) => {
for (const entry of entries) {
if (!entry.isIntersecting) {
continue;
}

const lazyImage = entry.target;
if (window.webpHero) {
const response = await fetch(lazyImage.dataset.src, {
credentials: 'same-origin',
});
const content = await response.arrayBuffer();

const webpMachine = new webpHero.WebpMachine();
lazyImage.src = await webpMachine.decode(new Uint8Array(content));
webpMachine.clearCache();
} else {
lazyImage.src = lazyImage.dataset.src;
}

lazyImageObserver.unobserve(lazyImage);
}
},
);

document
.querySelectorAll('img.thumbnail')
.forEach((lazyImage) => lazyImageObserver.observe(lazyImage));
});
Loading

0 comments on commit be0d09c

Please sign in to comment.