noxy: add simple fetch queue and wait for meta data response
ci/woodpecker/push/woodpecker Pipeline was successful Details
ci/woodpecker/pr/woodpecker Pipeline was successful Details

Instead of just blindly fetch noxy requests, this adds a simple
fetch queue and waits until the pending response is done. It
continues with the next one in the queue  whichever seen first,
This is not in chronological order, but incoming event order
(incoming events from different relays are probably out of order).
OFF0 2 years ago
parent a1c2ca4d41
commit cd71da0a87
Signed by: offbyn
GPG Key ID: 94A2F643C51F37FA

@ -168,16 +168,23 @@ const getNoxyUrl = (type, url, id, relay) => {
return link; return link;
} }
function linkPreview(href, id, relay) { const fetchQue = [];
if ((/\.(gif|jpe?g|png)$/i).test(href)) { let fetchPending;
return elem('div', {}, const fetchNext = (href, id, relay) => {
[elem('img', {className: 'preview-image-only', loading: 'lazy', src: getNoxyUrl('data', href, id, relay).href})]
);
}
const noxy = getNoxyUrl('meta', href, id, relay); const noxy = getNoxyUrl('meta', href, id, relay);
const previewId = noxy.searchParams.toString(); const previewId = noxy.searchParams.toString();
fetch(noxy.href) if (fetchPending) {
.then(data => data.json ? data.json() : data) fetchQue.push({href, id, relay});
return previewId;
}
fetchPending = fetch(noxy.href)
.then(data => {
if (data.status === 200) {
return data.json();
}
// fetchQue.push({href, id, relay}); // could try one more time
return Promise.reject(data);
})
.then(meta => { .then(meta => {
const container = document.getElementById(previewId); const container = document.getElementById(previewId);
container.append(elem('a', {href, rel: 'noopener noreferrer', target: '_blank'}, [ container.append(elem('a', {href, rel: 'noopener noreferrer', target: '_blank'}, [
@ -189,7 +196,24 @@ function linkPreview(href, id, relay) {
])); ]));
container.classList.add('preview-loaded'); container.classList.add('preview-loaded');
}) })
.finally(() => {
fetchPending = false;
if (fetchQue.length) {
const {href, id, relay} = fetchQue.shift();
return fetchNext(href, id, relay);
}
})
.catch(console.warn); .catch(console.warn);
return previewId;
};
function linkPreview(href, id, relay) {
if ((/\.(gif|jpe?g|png)$/i).test(href)) {
return elem('div', {},
[elem('img', {className: 'preview-image-only', loading: 'lazy', src: getNoxyUrl('data', href, id, relay).href})]
);
}
const previewId = fetchNext(href, id, relay);
return elem('div', { return elem('div', {
className: 'preview', className: 'preview',
id: previewId id: previewId

Loading…
Cancel
Save