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" /> <CircularProgress color="primary" />
</Backdrop> </Backdrop>
{ {/*
archived.length > 0 ? archived.length > 0 ?
<Grid container spacing={{ xs: 2, md: 2 }} columns={{ xs: 4, sm: 8, md: 12 }} pt={2}> <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> </Grid>
: null : null
} */}
</Container> </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. * Update the server ip address state and localstorage whenever the input value changes.
* Validate the ip-addr then set.s * Validate the ip-addr then set.s
* @param e Input change event * @param event Input change event
*/ */
const handleAddrChange = (event: any) => { const handleAddrChange = (event: any) => {
const $serverAddr = of(event) const $serverAddr = of(event)

View File

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

View File

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