[Updater > Module] Minor rewrite to integrate -progress events to not be separate

This commit is contained in:
Ducko 2022-04-21 18:37:31 +01:00
parent d3fe05b22e
commit 042e495b7b
2 changed files with 15 additions and 32 deletions

View file

@ -185,15 +185,12 @@ const initOld = () => { // "Old" (not v2 / new, win32 only)
installs.reset(); installs.reset();
downloads.reset(); downloads.reset();
if (!succeeded) { if (!succeeded) handleFail();
handleFail(); else if (updateCount === 0) launchMain();
} else if (updateCount === 0) {
launchMain();
}
}); });
on('downloading-module', ({ name }) => { on('downloading-module', ({ name, cur, total }) => {
downloads.record(name, 'Waiting'); downloads.record(name, '', cur, total);
installs.record(name, 'Waiting'); installs.record(name, 'Waiting');
}); });
@ -203,8 +200,8 @@ const initOld = () => { // "Old" (not v2 / new, win32 only)
if (failed > 0) handleFail(); if (failed > 0) handleFail();
}); });
on('installing-module', ({ name }) => { on('installing-module', ({ name, cur, total }) => {
installs.record(name, 'Waiting'); installs.record(name, '', cur, total);
}); });
const segmentCallback = (tracker) => (({ name }) => { const segmentCallback = (tracker) => (({ name }) => {
@ -217,11 +214,6 @@ const initOld = () => { // "Old" (not v2 / new, win32 only)
on('installing-modules-finished', check); on('installing-modules-finished', check);
const progressCallback = (tracker) => ({ name, cur, total }) => tracker.record(name, '', cur, total);
on('downloading-module-progress', progressCallback(downloads));
on('installing-module-progress', progressCallback(installs));
on('update-manually', e => { on('update-manually', e => {
splashState.newVersion = e.newVersion; splashState.newVersion = e.newVersion;

View file

@ -171,32 +171,30 @@ const checkModules = async () => {
const downloadModule = async (name, ver) => { const downloadModule = async (name, ver) => {
downloading.total++; downloading.total++;
events.emit('downloading-module', {
name
});
const url = baseUrl + '/' + name + '/' + ver;
const path = join(downloadPath, name + '-' + ver + '.zip'); const path = join(downloadPath, name + '-' + ver + '.zip');
const stream = fs.createWriteStream(path); const file = fs.createWriteStream(path);
// log('Modules', 'Downloading', `${name}@${ver}`); // log('Modules', 'Downloading', `${name}@${ver}`);
let success, total, cur = 0; let success, total, cur = 0;
request({ url, qs }).on('response', (res) => { request({
url: baseUrl + '/' + name + '/' + ver,
qs
}).on('response', (res) => {
success = res.statusCode === 200; success = res.statusCode === 200;
total = parseInt(res.headers['content-length'] ?? 1, 10); total = parseInt(res.headers['content-length'] ?? 1, 10);
res.pipe(stream); res.pipe(file);
res.on('data', c => { res.on('data', c => {
cur += c.length; cur += c.length;
events.emit('downloading-module-progress', { name, cur, total }); events.emit('downloading-module', { name, cur, total });
}); });
}); });
await new Promise((res) => stream.on('close', res)); await new Promise((res) => file.on('close', res));
if (!installed[name]) installed[name] = {}; if (!installed[name]) installed[name] = {};
@ -223,9 +221,6 @@ const downloadModule = async (name, ver) => {
const installModule = async (name, ver, path) => { const installModule = async (name, ver, path) => {
installing.total++; installing.total++;
events.emit('installing-module', {
name
});
// log('Modules', 'Installing', `${name}@${ver}`); // log('Modules', 'Installing', `${name}@${ver}`);
@ -275,11 +270,7 @@ const installModule = async (name, ver, path) => {
if (!y.includes('inflating')) return; if (!y.includes('inflating')) return;
cur++; cur++;
events.emit('installing-module-progress', { events.emit('installing-module', { name, cur, total });
name,
cur,
total
});
})); }));
proc.on('close', () => { proc.on('close', () => {