forked from nostr/nostrweb
noxy: add simple fetch queue and wait for meta data response
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).
parent
a580bc67b2
commit
b321acde02
40
src/main.js
40
src/main.js
|
@ -168,16 +168,23 @@ const getNoxyUrl = (type, url, id, relay) => {
|
|||
return link;
|
||||
}
|
||||
|
||||
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 fetchQue = [];
|
||||
let fetchPending;
|
||||
const fetchNext = (href, id, relay) => {
|
||||
const noxy = getNoxyUrl('meta', href, id, relay);
|
||||
const previewId = noxy.searchParams.toString();
|
||||
fetch(noxy.href)
|
||||
.then(data => data.json ? data.json() : data)
|
||||
if (fetchPending) {
|
||||
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 => {
|
||||
const container = document.getElementById(previewId);
|
||||
container.append(elem('a', {href, rel: 'noopener noreferrer', target: '_blank'}, [
|
||||
|
@ -189,7 +196,24 @@ function linkPreview(href, id, relay) {
|
|||
]));
|
||||
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);
|
||||
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', {
|
||||
className: 'preview',
|
||||
id: previewId
|
||||
|
|
Loading…
Reference in New Issue