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);
|
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 () => {
|
const update = async () => {
|
||||||
return await Promise.all([
|
const errorInterval = 10000;
|
||||||
ctx.requests.getTorrentDetails(torrent.id).then((details) => {
|
const liveInterval = 500;
|
||||||
updateDetailsResponse(details);
|
const finishedInterval = 5000;
|
||||||
return details;
|
|
||||||
}),
|
return ctx.requests.getTorrentStats(torrent.id).then((stats) => {
|
||||||
ctx.requests.getTorrentStats(torrent.id).then((stats) => {
|
updateStatsResponse(stats);
|
||||||
updateStatsResponse(stats);
|
return torrentIsDone(stats) ? finishedInterval : liveInterval;
|
||||||
return stats;
|
|
||||||
})
|
|
||||||
]).then(([_, stats]) => {
|
|
||||||
return torrentIsDone(stats) ? 10000 : 500;
|
|
||||||
}, (e) => {
|
}, (e) => {
|
||||||
return 5000;
|
return errorInterval
|
||||||
})
|
})
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -467,6 +473,29 @@ function customSetInterval(asyncCallback: any, interval: number) {
|
||||||
return clearCustomInterval;
|
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
|
// List all torrents on page load and set up auto-refresh
|
||||||
async function init(): Promise<void> {
|
async function init(): Promise<void> {
|
||||||
await displayTorrents();
|
await displayTorrents();
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue