Code refactoring

This commit is contained in:
2022-06-23 07:44:25 +02:00
parent 1248332fb4
commit ed127d68b1
4 changed files with 22 additions and 20 deletions

View File

@@ -25,7 +25,7 @@ export default function archivedDownloads() {
>
<CircularProgress color="primary" />
</Backdrop>
{
{/*
archived.length > 0 ?
<Grid container spacing={{ xs: 2, md: 2 }} columns={{ xs: 4, sm: 8, md: 12 }} pt={2}>
{
@@ -40,7 +40,7 @@ export default function archivedDownloads() {
}
</Grid>
: null
}
*/}
</Container>
);
}

View File

@@ -40,7 +40,7 @@ export default function Settings({ socket }: Props) {
/**
* Update the server ip address state and localstorage whenever the input value changes.
* Validate the ip-addr then set.s
* @param e Input change event
* @param event Input change event
*/
const handleAddrChange = (event: any) => {
const $serverAddr = of(event)

View File

@@ -15,13 +15,11 @@ const log = Logger.instance;
*/
class Process {
private url: string;
private params: Array<string>;
public readonly url: string;
public readonly params: Array<string>;
private settings: ISettings;
private stdout: Readable;
private pid: number;
private info: any;
private lock: boolean;
private exePath = join(__dirname, 'yt-dlp');
constructor(url: string, params: Array<string>, settings: any) {
@@ -30,7 +28,6 @@ class Process {
this.settings = settings
this.stdout = undefined;
this.pid = undefined;
this.info = null;
}
/**
@@ -73,8 +70,6 @@ class Process {
try {
const buffer = Buffer.concat(stdoutChunks);
const json = JSON.parse(buffer.toString());
this.info = json;
this.lock = false;
resolve({
formats: json.formats.map((format: IDownloadInfoSection) => {
return {

View File

@@ -1,6 +1,6 @@
import { spawn } from 'child_process';
import { from, interval } from 'rxjs';
import { throttle } from 'rxjs/operators';
import { map, throttle } from 'rxjs/operators';
import { killProcess } from '../utils/procUtils';
import { Socket } from 'socket.io';
import { IPayload } from '../interfaces/IPayload';
@@ -57,18 +57,22 @@ export async function download(socket: Socket, payload: IPayload) {
p.start().then(downloader => {
pool.add(p)
let pid = downloader.getPid();
const pid = downloader.getPid();
p.getInfo().then(info => {
socket.emit('info', { pid: pid, info: info });
socket.emit('info', {
pid: pid,
info: info
});
});
from(downloader.getStdout()) // stdout as observable
.pipe(throttle(() => interval(500))) // discard events closer than 500ms
from(downloader.getStdout()) // stdout as observable
.pipe(
throttle(() => interval(500)), // discard events closer than 500ms
map(stdout => formatter(String(stdout), pid))
)
.subscribe({
next: (stdout) => {
socket.emit('progress', formatter(String(stdout), pid))
},
next: (stdout) => socket.emit('progress', stdout),
complete: () => {
downloader.kill().then(() => {
socket.emit('progress', {
@@ -82,6 +86,7 @@ export async function download(socket: Socket, payload: IPayload) {
socket.emit('progress', {
status: 'Done!', pid: pid
});
pool.remove(downloader);
}
});
});
@@ -118,7 +123,7 @@ export async function retrieveDownload(socket: Socket) {
socket.emit('pending-jobs', _poolSize)
const it = pool.iterator();
const tempWorkQueue = new Array();
const tempWorkQueue = new Array<Process>();
// sanitize
for (const entry of it) {
@@ -207,6 +212,8 @@ const formatter = (stdout: string, pid: number) => {
progress: '100',
}
default:
return { progress: '0' }
return {
progress: '0'
}
}
}