From 042e495b7b0c98dba0944eee92a82e95a4a4d6cc Mon Sep 17 00:00:00 2001 From: Oj Date: Thu, 21 Apr 2022 18:37:31 +0100 Subject: [PATCH] [Updater > Module] Minor rewrite to integrate -progress events to not be separate --- src/splash/index.js | 20 ++++++-------------- src/updater/moduleUpdater.js | 27 +++++++++------------------ 2 files changed, 15 insertions(+), 32 deletions(-) diff --git a/src/splash/index.js b/src/splash/index.js index 7574dd5..d15b28e 100644 --- a/src/splash/index.js +++ b/src/splash/index.js @@ -185,15 +185,12 @@ const initOld = () => { // "Old" (not v2 / new, win32 only) installs.reset(); downloads.reset(); - if (!succeeded) { - handleFail(); - } else if (updateCount === 0) { - launchMain(); - } + if (!succeeded) handleFail(); + else if (updateCount === 0) launchMain(); }); - on('downloading-module', ({ name }) => { - downloads.record(name, 'Waiting'); + on('downloading-module', ({ name, cur, total }) => { + downloads.record(name, '', cur, total); installs.record(name, 'Waiting'); }); @@ -203,8 +200,8 @@ const initOld = () => { // "Old" (not v2 / new, win32 only) if (failed > 0) handleFail(); }); - on('installing-module', ({ name }) => { - installs.record(name, 'Waiting'); + on('installing-module', ({ name, cur, total }) => { + installs.record(name, '', cur, total); }); const segmentCallback = (tracker) => (({ name }) => { @@ -217,11 +214,6 @@ const initOld = () => { // "Old" (not v2 / new, win32 only) 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 => { splashState.newVersion = e.newVersion; diff --git a/src/updater/moduleUpdater.js b/src/updater/moduleUpdater.js index 52a27ea..f6ec4c4 100644 --- a/src/updater/moduleUpdater.js +++ b/src/updater/moduleUpdater.js @@ -171,32 +171,30 @@ const checkModules = async () => { const downloadModule = async (name, ver) => { downloading.total++; - events.emit('downloading-module', { - name - }); - - const url = baseUrl + '/' + name + '/' + ver; const path = join(downloadPath, name + '-' + ver + '.zip'); - const stream = fs.createWriteStream(path); + const file = fs.createWriteStream(path); // log('Modules', 'Downloading', `${name}@${ver}`); let success, total, cur = 0; - request({ url, qs }).on('response', (res) => { + request({ + url: baseUrl + '/' + name + '/' + ver, + qs + }).on('response', (res) => { success = res.statusCode === 200; total = parseInt(res.headers['content-length'] ?? 1, 10); - res.pipe(stream); + res.pipe(file); res.on('data', c => { 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] = {}; @@ -223,9 +221,6 @@ const downloadModule = async (name, ver) => { const installModule = async (name, ver, path) => { installing.total++; - events.emit('installing-module', { - name - }); // log('Modules', 'Installing', `${name}@${ver}`); @@ -275,11 +270,7 @@ const installModule = async (name, ver, path) => { if (!y.includes('inflating')) return; cur++; - events.emit('installing-module-progress', { - name, - cur, - total - }); + events.emit('installing-module', { name, cur, total }); })); proc.on('close', () => {