OpenAsar/src/splash/index.js

237 lines
5.4 KiB
JavaScript
Raw Normal View History

const { app, ipcMain } = require('electron');
const moduleUpdater = require("../updater/moduleUpdater");
const updater = require("../updater/updater");
let splashState = {},
launched,
win;
exports.initSplash = (startMin) => {
const inst = updater.getUpdater();
if (inst) initNew(inst);
else initOld();
launchSplash(startMin);
if (process.env.OPENASAR_QUICKSTART || oaConfig.quickstart) setTimeout(() => {
destroySplash();
launchMain();
setTimeout(() => {
events.emit('APP_SHOULD_SHOW');
}, 100);
}, 300);
};
exports.focusWindow = () => win?.focus?.();
exports.pageReady = () => destroySplash() || process.nextTick(() => events.emit('APP_SHOULD_SHOW'));
const destroySplash = () => {
win?.setSkipTaskbar?.(true);
setTimeout(() => {
if (!win) return;
win.hide();
win.close();
win = null;
}, 100);
};
const launchMain = () => {
moduleUpdater.events.removeAllListeners(); // Remove updater v1 listeners
if (!launched && win != null) {
sendState('starting');
launched = true;
events.emit('APP_SHOULD_LAUNCH');
}
};
2022-02-17 20:47:15 +00:00
const sendState = (status) => {
try {
win.webContents.send('state', { status, ...splashState });
2022-02-17 20:47:15 +00:00
} catch (_e) {}
};
const launchSplash = (startMin) => {
win = require('../utils/win')({
width: 300,
height: process.platform === 'darwin' ? 300 : 350
}, 'splash');
if (process.platform !== 'darwin') win.on('closed', () => !launched && app.quit());
ipcMain.on('ss', launchMain);
ipcMain.on('sq', app.quit);
2022-04-17 21:58:03 +00:00
if (!startMin) win.once('ready-to-show', win.show);
};
2021-12-09 16:25:14 +00:00
const CHECKING_FOR_UPDATES = 'checking-for-updates';
const events = exports.events = new (require('events').EventEmitter)();
let toSend = 0; // Progress state to send for ModuleUpdater (0 = downloading, 1 = installing)
class UIProgress { // Generic class to track updating and sent states to splash
constructor(st) {
this.st = st;
this.reset();
}
reset() {
Object.assign(this, {
progress: new Map(),
done: new Set(),
total: new Set()
});
2021-12-09 16:25:14 +00:00
}
2022-03-15 19:11:54 +00:00
record(id, state, current, outOf) {
this.total.add(id);
if (current) this.progress.set(id, [ current, outOf ?? 100 ]);
if (state === 'Complete') this.done.add(id);
this.send();
}
send() {
if ((toSend === -1 && this.progress.size > 0 && this.progress.size > this.done.size) || toSend === this.st) {
2022-03-15 19:11:54 +00:00
const progress = [...this.progress.values()].reduce((a, x) => a + x[0], 0) / [...this.progress.values()].reduce((a, x) => a + x[1], 0) * 100;
if (progress > 100) return true;
2021-12-09 16:25:14 +00:00
splashState = {
current: this.done.size + 1,
total: this.total.size,
2022-03-15 19:11:54 +00:00
progress
2021-12-09 16:25:14 +00:00
};
sendState(this.st ? 'installing' : 'downloading');
2021-12-09 16:25:14 +00:00
return true;
}
}
}
const initNew = async (inst) => {
toSend = -1;
2021-12-09 16:25:14 +00:00
const retryOptions = {
skip_host_delta: false,
skip_module_delta: {}
};
2021-12-09 16:25:14 +00:00
while (true) {
sendState(CHECKING_FOR_UPDATES);
2021-12-09 16:25:14 +00:00
try {
let installedAnything = false;
const downloads = new UIProgress(0);
const installs = new UIProgress(1);
await inst.updateToLatestWithOptions(retryOptions, ({ task, state, percent }) => {
const download = task.HostDownload || task.ModuleDownload;
const install = task.HostInstall || task.ModuleInstall;
2021-12-09 16:25:14 +00:00
installedAnything = true;
const simpleRecord = (tracker, x) => tracker.record(x.package_sha256, state, percent);
if (download != null) simpleRecord(downloads, download);
if (!downloads.send()) installs.send();
if (install == null) return;
simpleRecord(installs, install);
if (task.HostInstall != null) {
retryOptions.skip_host_delta = true;
} else if (task.ModuleInstall != null) {
retryOptions.skip_module_delta[install.version.module.name] = true;
2021-12-09 16:25:14 +00:00
}
});
2021-12-09 16:25:14 +00:00
if (!installedAnything) {
await inst.startCurrentVersion();
inst.collectGarbage();
return launchMain();
2021-12-09 16:25:14 +00:00
}
} catch (e) {
log('Splash', e);
await new Promise(r => fail(r));
2021-12-09 16:25:14 +00:00
}
}
};
const initOld = () => { // "Old" (not v2 / new, win32 only)
const on = (k, v) => moduleUpdater.events.on(k, v);
const check = () => moduleUpdater.checkForUpdates();
2022-03-15 19:11:54 +00:00
const downloads = new UIProgress(0), installs = new UIProgress(1);
const handleFail = () => {
fail(check);
};
on('update-check-finished', ({ succeeded, updateCount }) => {
installs.reset();
downloads.reset();
if (!succeeded) handleFail();
else if (updateCount === 0) launchMain();
2021-12-09 16:25:14 +00:00
});
on('downloading-module', ({ name, cur, total }) => {
downloads.record(name, '', cur, total);
2022-03-15 19:11:54 +00:00
installs.record(name, 'Waiting');
2021-12-09 16:25:14 +00:00
});
on('downloading-modules-finished', ({ failed }) => {
toSend = 1;
if (failed > 0) handleFail();
2021-12-09 16:25:14 +00:00
});
on('installing-module', ({ name, cur, total }) => {
installs.record(name, '', cur, total);
});
const segment = (tracker) => (({ name }) => {
2022-03-15 19:11:54 +00:00
tracker.record(name, 'Complete');
if (name === 'host') moduleUpdater.quitAndInstallUpdates();
2021-12-09 16:25:14 +00:00
});
on('downloaded-module', segment(downloads));
on('installed-module', segment(installs));
on('installing-modules-finished', check);
on('update-manually', e => {
splashState.details = e.details;
sendState('manual');
2022-03-15 19:11:54 +00:00
});
sendState(CHECKING_FOR_UPDATES);
check();
};
const fail = (c) => {
const s = 10;
splashState.seconds = s;
sendState('fail');
setTimeout(c, s * 1000);
};