Optimize requests in webui a bit
This commit is contained in:
parent
64209a2c7c
commit
012643442a
2 changed files with 48 additions and 19 deletions
14
crates/librqbit/webui/dist/app.js
vendored
14
crates/librqbit/webui/dist/app.js
vendored
File diff suppressed because one or more lines are too long
|
|
@ -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();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue