Refactor the rqbit-web file by breaking it down into components and helper functions. Improve code organization and maintainability

This commit is contained in:
Artur Lozovski 2023-12-07 16:33:37 +00:00
parent a5e7a5a5f5
commit f978ad02fe
25 changed files with 1722 additions and 926 deletions

View file

@ -0,0 +1,29 @@
// Run a function with initial interval, then run it forever with the interval that the
// callback returns.
// Returns a callback to clear it.
export function customSetInterval(
asyncCallback: () => Promise<number>,
initialInterval: number
): () => void {
let timeoutId: number;
let currentInterval: number = initialInterval;
const executeCallback = async () => {
currentInterval = await asyncCallback();
if (currentInterval === null || currentInterval === undefined) {
throw "asyncCallback returned null or undefined";
}
scheduleNext();
};
let scheduleNext = () => {
timeoutId = setTimeout(executeCallback, currentInterval);
};
scheduleNext();
return () => {
clearTimeout(timeoutId);
};
}

View file

@ -0,0 +1,10 @@
export function formatBytes(bytes: number): string {
if (bytes === 0) return "0 Bytes";
const k = 1024;
const sizes = ["Bytes", "KB", "MB", "GB"];
const i = Math.floor(Math.log(bytes) / Math.log(k));
return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + " " + sizes[i];
}

View file

@ -0,0 +1,19 @@
export function formatSecondsToTime(seconds: number): string {
const hours = Math.floor(seconds / 3600);
const minutes = Math.floor((seconds % 3600) / 60);
const remainingSeconds = seconds % 60;
const formatUnit = (value: number, unit: string) =>
value > 0 ? `${value}${unit}` : "";
if (hours > 0) {
return `${formatUnit(hours, "h")} ${formatUnit(minutes, "m")}`.trim();
} else if (minutes > 0) {
return `${formatUnit(minutes, "m")} ${formatUnit(
remainingSeconds,
"s"
)}`.trim();
} else {
return `${formatUnit(remainingSeconds, "s")}`.trim();
}
}

View file

@ -0,0 +1,10 @@
import { TorrentStats } from "../api-types";
import { formatSecondsToTime } from "./formatSecondsToTime";
export function getCompletionETA(stats: TorrentStats): string {
let duration = stats?.live?.time_remaining?.duration?.secs;
if (duration == null) {
return "N/A";
}
return formatSecondsToTime(duration);
}

View file

@ -0,0 +1,10 @@
import { TorrentDetails } from "../api-types";
export function getLargestFileName(torrentDetails: TorrentDetails): string {
const largestFile = torrentDetails.files
.filter((f) => f.included)
.reduce((prev: any, current: any) =>
prev.length > current.length ? prev : current
);
return largestFile.name;
}

View file

@ -0,0 +1,27 @@
export function loopUntilSuccess<T>(
callback: () => Promise<T>,
interval: number
): () => void {
let timeoutId: number;
const executeCallback = async () => {
let retry = await callback().then(
() => false,
() => true
);
if (retry) {
scheduleNext();
}
};
let scheduleNext = (overrideInterval?: number) => {
timeoutId = setTimeout(
executeCallback,
overrideInterval !== undefined ? overrideInterval : interval
);
};
scheduleNext(0);
return () => clearTimeout(timeoutId);
}