update: fix stout bursts

This commit is contained in:
2021-11-29 19:07:59 +01:00
parent 8812593f38
commit f94e3daaab
3 changed files with 12 additions and 18 deletions

View File

@@ -1,5 +1,7 @@
const { spawn } = require('child_process');
const logger = require('./logger');
const { from, interval } = require('rxjs');
const { throttle } = require('rxjs/operators');
let settings;
try {
@@ -16,8 +18,15 @@ const download = (socket, url) => {
['-o', `${settings.download_path || 'downloads/'}%(title)s.%(ext)s`, url]
)
ytldp.stdout.on('data', data => {
socket.emit('progress', data.toString())
logger('download', `Fetching ${data.toString()}`)
// reactive programming magic
from(Promise.resolve(data)) // stout as promise => Observable
.pipe(throttle(() => interval(500))) // discard events closer than 500ms
.subscribe({
next: () => {
socket.emit('progress', data.toString()) // finally, emit
logger('download', `Fetching ${data.toString()}`)
}
})
})
ytldp.on('exit', () => {
socket.emit('progress', 'Done!')

View File

@@ -1,16 +0,0 @@
const https = require('https');
const path = require('path');
const fs = require('fs');
const isWindows = process.platform === 'win32';
const file = fs.createWriteStream(path.join(
__dirname, `yt-dlp${isWindows ? '.exe' : ''}`
));
https.get(
isWindows ?
'https://github.com/yt-dlp/yt-dlp/releases/download/2021.11.10.1/yt-dlp.exe' :
'https://github.com/yt-dlp/yt-dlp/releases/download/2021.11.10.1/yt-dlp',
res => res.pipe(file)
);

View File

@@ -18,6 +18,7 @@
"react": "^17.0.2",
"react-bootstrap": "^2.0.2",
"react-dom": "^17.0.2",
"rxjs": "^7.4.0",
"socket.io": "^4.3.2",
"socket.io-client": "^4.3.2"
},