[Splash > Various] Major clean up / rewriting to simplify source

This commit is contained in:
Ducko 2022-03-13 15:53:33 +00:00
parent 1071c811da
commit c510e44aaa
3 changed files with 38 additions and 68 deletions

View file

@ -2,7 +2,7 @@
<div id="text">Starting...</div> <div id="text">Starting...</div>
<div id="bar-container"><div id="bar-fill"></div></div> <div id="barContainer"><div id="barFill"></div></div>
<div id="debug"></div> <div id="debug"></div>
@ -64,7 +64,7 @@
width: 100%; width: 100%;
} }
#bar-container, #bar-fill { #barContainer, #barFill {
width: 180px; width: 180px;
height: 8px; height: 8px;
@ -73,14 +73,14 @@
visibility: hidden; visibility: hidden;
} }
#bar-container { #barContainer {
background-color: var(--background-secondary); background-color: var(--background-secondary);
position: relative; position: relative;
margin-top: 12px; margin-top: 12px;
} }
#bar-fill { #barFill {
background-color: var(--brand-experiment); background-color: var(--brand-experiment);
width: 0; width: 0;
} }
@ -98,29 +98,20 @@
</style> </style>
<script> <script>
const text = document.querySelector('#text');
const barContainer = document.querySelector('#bar-container');
const barFill = document.querySelector('#bar-fill');
Splash.onState(({ status, current, total, progress, seconds }) => { Splash.onState(({ status, current, total, progress, seconds }) => {
let statusText = status.replaceAll('-', ' '); let statusText = status.replaceAll('-', ' ');
let showProgress = false; let showProgress = false;
switch (status) { switch (status) {
case 'downloading-updates': // X of Y states case 'downloading': // X of Y states
case 'installing-updates': case 'installing':
statusText = statusText.slice(0, -1) + ` ${current} of ${total}`; // Remove "s" and concat statusText = statusText + ` update ${current} of ${total}`; // Remove "s" and concat
showProgress = true; showProgress = true;
break; break;
case 'update-failure': // Custom for update failure, include retry and reorder text case 'fail': // Custom for update failure, include retry and reorder text
statusText = `Update Failed - Retrying in ${seconds}`; statusText = `Update Failed - Retrying in ${seconds}`;
break; break;
case 'launching':
statusText = 'Starting...';
break;
} }
text.textContent = statusText; text.textContent = statusText;

View file

@ -37,13 +37,13 @@ exports.initSplash = (startMinimized = false) => {
launchMainWindow(); launchMainWindow();
setTimeout(() => { setTimeout(() => {
events.emit(APP_SHOULD_SHOW); events.emit('APP_SHOULD_SHOW');
}, 100); }, 100);
}, 300); }, 300);
}; };
exports.focusWindow = () => splashWindow?.focus?.(); exports.focusWindow = () => splashWindow?.focus?.();
exports.pageReady = () => destroySplash() || process.nextTick(() => events.emit(APP_SHOULD_SHOW)); exports.pageReady = () => destroySplash() || process.nextTick(() => events.emit('APP_SHOULD_SHOW'));
const destroySplash = () => { const destroySplash = () => {
log('Splash', 'Destroy'); log('Splash', 'Destroy');
@ -67,10 +67,10 @@ const launchMainWindow = () => {
for (const e in modulesListeners) moduleUpdater.events.removeListener(e, modulesListeners[e]); // Remove updater v1 listeners for (const e in modulesListeners) moduleUpdater.events.removeListener(e, modulesListeners[e]); // Remove updater v1 listeners
if (!launchedMainWindow && splashWindow != null) { if (!launchedMainWindow && splashWindow != null) {
sendState(LAUNCHING); sendState('starting');
launchedMainWindow = true; launchedMainWindow = true;
events.emit(APP_SHOULD_LAUNCH); events.emit('APP_SHOULD_LAUNCH');
} }
}; };
@ -117,32 +117,14 @@ const launchSplashWindow = (startMinimized) => {
const CHECKING_FOR_UPDATES = 'checking-for-updates'; const CHECKING_FOR_UPDATES = 'checking-for-updates';
const UPDATE_CHECK_FINISHED = 'update-check-finished';
const UPDATE_FAILURE = 'update-failure';
const LAUNCHING = 'launching';
const DOWNLOADING_MODULE = 'downloading-module';
const DOWNLOADING_UPDATES = 'downloading-updates';
const DOWNLOADING_MODULES_FINISHED = 'downloading-modules-finished';
const DOWNLOADING_MODULE_PROGRESS = 'downloading-module-progress';
const DOWNLOADED_MODULE = 'downloaded-module';
const NO_PENDING_UPDATES = 'no-pending-updates';
const INSTALLING_MODULE = 'installing-module';
const INSTALLING_UPDATES = 'installing-updates';
const INSTALLED_MODULE = 'installed-module';
const INSTALLING_MODULE_PROGRESS = 'installing-module-progress';
const INSTALLING_MODULES_FINISHED = 'installing-modules-finished';
const UPDATE_MANUALLY = 'update-manually';
const APP_SHOULD_LAUNCH = 'APP_SHOULD_LAUNCH';
const APP_SHOULD_SHOW = 'APP_SHOULD_SHOW';
const events = new (require('events').EventEmitter)();
exports.APP_SHOULD_LAUNCH = APP_SHOULD_LAUNCH; const events = exports.events = new (require('events').EventEmitter)();
exports.APP_SHOULD_SHOW = APP_SHOULD_SHOW;
exports.events = events;
class UIProgress { // Generic class to track updating and sent states to splash class UIProgress { // Generic class to track updating and sent states to splash
constructor(stateId) { constructor(st) {
this.stateId = stateId; this.stateId = st ? 'installing' : 'downloading';
this.reset(); this.reset();
} }
@ -157,12 +139,9 @@ class UIProgress { // Generic class to track updating and sent states to splash
record(id, state, percent) { record(id, state, percent) {
this.total.add(id); this.total.add(id);
if (state !== 'Waiting') { if (state !== 'Waiting') this.progress.set(id, percent);
this.progress.set(id, percent);
if (state === 'Complete') this.done.add(id); if (state === 'Complete') this.done.add(id);
} }
}
send() { send() {
if (this.progress.size > 0 && this.progress.size > this.done.size) { if (this.progress.size > 0 && this.progress.size > this.done.size) {
@ -190,8 +169,8 @@ const updateUntilCurrent = async () => {
try { try {
let installedAnything = false; let installedAnything = false;
const downloads = new UIProgress(DOWNLOADING_UPDATES); const downloads = new UIProgress(0);
const installs = new UIProgress(INSTALLING_UPDATES); const installs = new UIProgress(1);
await newUpdater.updateToLatestWithOptions(retryOptions, ({ task, state, percent }) => { await newUpdater.updateToLatestWithOptions(retryOptions, ({ task, state, percent }) => {
const download = task.HostDownload || task.ModuleDownload; const download = task.HostDownload || task.ModuleDownload;
@ -224,7 +203,7 @@ const updateUntilCurrent = async () => {
} }
} catch (e) { } catch (e) {
log('Splash', 'Update failed', e); log('Splash', 'Update failed', e);
sendState(UPDATE_FAILURE); sendState('fail');
await new Promise(res => scheduleNextUpdate(res)); await new Promise(res => scheduleNextUpdate(res));
} }
} }
@ -243,12 +222,12 @@ const initModuleUpdater = () => { // "Old" (not v2 / new, win32 only)
const callbackCheck = () => moduleUpdater.checkForUpdates(); const callbackCheck = () => moduleUpdater.checkForUpdates();
const downloads = new UIProgress(DOWNLOADING_UPDATES); const downloads = new UIProgress(0);
const installs = new UIProgress(INSTALLING_UPDATES); const installs = new UIProgress(1);
const handleFail = () => { const handleFail = () => {
scheduleNextUpdate(); scheduleNextUpdate();
sendState(UPDATE_FAILURE); sendState('fail');
}; };
add(CHECKING_FOR_UPDATES, () => { add(CHECKING_FOR_UPDATES, () => {
@ -266,7 +245,7 @@ const initModuleUpdater = () => { // "Old" (not v2 / new, win32 only)
currentTotal = newTotal; currentTotal = newTotal;
}; };
add(UPDATE_CHECK_FINISHED, ({ succeeded, updateCount }) => { add('update-check-finished', ({ succeeded, updateCount }) => {
v1_timeoutStop(); v1_timeoutStop();
installs.reset(); installs.reset();
@ -280,14 +259,14 @@ const initModuleUpdater = () => { // "Old" (not v2 / new, win32 only)
} }
}); });
add(DOWNLOADING_MODULE, ({ current, total }) => { add('downloading-module', ({ current, total }) => {
v1_timeoutStop(); v1_timeoutStop();
if (total !== currentTotal) updateTotal(total); if (total !== currentTotal) updateTotal(total);
currentId = current; currentId = current;
}); });
add(DOWNLOADING_MODULES_FINISHED, ({ failed }) => { add('downloading-modules-finished', ({ failed }) => {
if (failed > 0) { if (failed > 0) {
handleFail(); handleFail();
} else { } else {
@ -295,7 +274,7 @@ const initModuleUpdater = () => { // "Old" (not v2 / new, win32 only)
} }
}); });
add(INSTALLING_MODULE, ({ current }) => { add('installing-module', ({ current }) => {
currentId = current; currentId = current;
installs.record(currentId, '', 0); installs.record(currentId, '', 0);
@ -307,21 +286,21 @@ const initModuleUpdater = () => { // "Old" (not v2 / new, win32 only)
if (name === 'host') restartRequired = true; if (name === 'host') restartRequired = true;
}); });
add(DOWNLOADED_MODULE, segmentCallback(downloads)); add('downloaded-module', segmentCallback(downloads));
add(INSTALLED_MODULE, segmentCallback(installs)); add('installed-module', segmentCallback(installs));
add(INSTALLING_MODULES_FINISHED, callbackCheck); add('installing-modules-finished', callbackCheck);
add(NO_PENDING_UPDATES, callbackCheck); add('no-pending-updates', callbackCheck);
const progressCallback = (tracker) => (({ progress }) => { const progressCallback = (tracker) => (({ progress }) => {
tracker.record(currentId, '', progress); tracker.record(currentId, '', progress);
tracker.send(); tracker.send();
}); });
add(DOWNLOADING_MODULE_PROGRESS, progressCallback(downloads)); add('downloading-module-progress', progressCallback(downloads));
add(INSTALLING_MODULE_PROGRESS, progressCallback(installs)); add('installing-module-progress', progressCallback(installs));
addBasic(UPDATE_MANUALLY, 'newVersion'); addBasic('update-manually', 'newVersion');
}; };
const v1_timeoutStart = () => !updateTimeout && (updateTimeout = setTimeout(scheduleNextUpdate, 10000)); const v1_timeoutStart = () => !updateTimeout && (updateTimeout = setTimeout(scheduleNextUpdate, 10000));

View file

@ -22,8 +22,8 @@ exports.update = (startMin, done, show) => {
} }
splash.initSplash(startMin); splash.initSplash(startMin);
splash.events.once(splash.APP_SHOULD_LAUNCH, done); splash.events.once('APP_SHOULD_LAUNCH', done);
splash.events.once(splash.APP_SHOULD_SHOW, show); splash.events.once('APP_SHOULD_SHOW', show);
}; };
exports.focusSplash = () => splash.focusWindow(); exports.focusSplash = () => splash.focusWindow();