Optimize requests in webui a bit

This commit is contained in:
Igor Katson 2023-11-21 14:35:32 +00:00
parent 64209a2c7c
commit 012643442a
No known key found for this signature in database
GPG key ID: B4EC22B66D61A3F5
2 changed files with 48 additions and 19 deletions

File diff suppressed because one or more lines are too long

View file

@ -159,20 +159,26 @@ const Torrent = ({ torrent }) => {
let ctx = useContext(AppContext);
// Update details once
useEffect(() => {
if (detailsResponse === defaultDetails) {
return loopUntilSuccess(async () => {
await ctx.requests.getTorrentDetails(torrent.id).then(updateDetailsResponse);
}, 1000);
}
}, [detailsResponse]);
// Update stats forever.
const update = async () => {
return await Promise.all([
ctx.requests.getTorrentDetails(torrent.id).then((details) => {
updateDetailsResponse(details);
return details;
}),
ctx.requests.getTorrentStats(torrent.id).then((stats) => {
updateStatsResponse(stats);
return stats;
})
]).then(([_, stats]) => {
return torrentIsDone(stats) ? 10000 : 500;
const errorInterval = 10000;
const liveInterval = 500;
const finishedInterval = 5000;
return ctx.requests.getTorrentStats(torrent.id).then((stats) => {
updateStatsResponse(stats);
return torrentIsDone(stats) ? finishedInterval : liveInterval;
}, (e) => {
return 5000;
return errorInterval
})
};
@ -467,6 +473,29 @@ function customSetInterval(asyncCallback: any, interval: number) {
return clearCustomInterval;
}
function loopUntilSuccess(callback, interval: number) {
let timeoutId: number;
const executeCallback = async () => {
let retry = await callback().then(() => { false }, () => { true });
if (retry) {
scheduleNext();
}
}
let scheduleNext = (i?: number) => {
timeoutId = setTimeout(executeCallback, i !== undefined ? i : interval);
}
scheduleNext(0);
let clearCustomInterval = () => {
clearTimeout(timeoutId);
}
return clearCustomInterval;
}
// List all torrents on page load and set up auto-refresh
async function init(): Promise<void> {
await displayTorrents();