[Updater > Module] Self-rewrite

This commit is contained in:
Ducko 2022-03-15 19:11:54 +00:00
parent 49dbd48a05
commit 388bbf548d
2 changed files with 371 additions and 813 deletions

View file

@ -120,7 +120,7 @@ const CHECKING_FOR_UPDATES = 'checking-for-updates';
const events = exports.events = new (require('events').EventEmitter)(); const events = exports.events = new (require('events').EventEmitter)();
let progressState = 'downloading';
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(st) { constructor(st) {
this.stateId = st ? 'installing' : 'downloading'; this.stateId = st ? 'installing' : 'downloading';
@ -136,7 +136,7 @@ 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') this.progress.set(id, percent); if (state !== 'Waiting') this.progress.set(id, percent);
@ -144,11 +144,40 @@ class UIProgress { // Generic class to track updating and sent states to splash
} }
send() { send() {
if (this.progress.size > 0 && this.progress.size > this.done.size) { if (this.stateId === 'downloading') {
console.log(this.progress);
// console.log([...this.progress.values()]);
// if (this.progress.size > 0 && this.progress.size > this.done.size) {
splashState = {
// current: this.done.size + 1,
// total: this.total.size,
progress: [...this.progress.values()].reduce((a, x) => a + x, 0) / this.total.size
};
sendState(this.stateId);
return true;
}
} */
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 ((newUpdater && this.progress.size > 0 && this.progress.size > this.done.size) || (!newUpdater && progressState === this.stateId)) {
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;
splashState = { splashState = {
current: this.done.size + 1, current: this.done.size + 1,
total: this.total.size, total: this.total.size,
progress: [...this.progress.values()].reduce((a, x) => a + x, 0) / this.total.size progress
}; };
sendState(this.stateId); sendState(this.stateId);
@ -212,18 +241,12 @@ const updateUntilCurrent = async () => {
const initModuleUpdater = () => { // "Old" (not v2 / new, win32 only) const initModuleUpdater = () => { // "Old" (not v2 / new, win32 only)
const add = (event, listener) => { const add = (event, listener) => {
modulesListeners[event] = listener; modulesListeners[event] = listener;
moduleUpdater.events.addListener(event, listener); moduleUpdater.events.on(event, listener);
}; };
const addBasic = (ev, key, ui = ev) => add(ev, (e) => {
splashState[key] = e[key];
sendState(ui);
});
const callbackCheck = () => moduleUpdater.checkForUpdates(); const callbackCheck = () => moduleUpdater.checkForUpdates();
const downloads = new UIProgress(0); const downloads = new UIProgress(0), installs = new UIProgress(1);
const installs = new UIProgress(1);
const handleFail = () => { const handleFail = () => {
scheduleNextUpdate(); scheduleNextUpdate();
@ -235,16 +258,6 @@ const initModuleUpdater = () => { // "Old" (not v2 / new, win32 only)
sendState(CHECKING_FOR_UPDATES); sendState(CHECKING_FOR_UPDATES);
}); });
let currentId, currentTotal = 1;
const updateTotal = (newTotal) => {
for (let i = currentTotal; i <= newTotal; i++) {
downloads.record(i, 'Waiting', 0);
installs.record(i, 'Waiting', 0);
}
currentTotal = newTotal;
};
add('update-check-finished', ({ succeeded, updateCount }) => { add('update-check-finished', ({ succeeded, updateCount }) => {
v1_timeoutStop(); v1_timeoutStop();
@ -259,30 +272,27 @@ const initModuleUpdater = () => { // "Old" (not v2 / new, win32 only)
} }
}); });
add('downloading-module', ({ current, total }) => { add('downloading-module', ({ name }) => {
v1_timeoutStop(); v1_timeoutStop();
if (total !== currentTotal) updateTotal(total); downloads.record(name, 'Waiting');
currentId = current; installs.record(name, 'Waiting');
}); });
add('downloading-modules-finished', ({ failed }) => { add('downloading-modules-finished', ({ failed }) => {
progressState = 'installing';
if (failed > 0) { if (failed > 0) {
handleFail(); handleFail();
} else { } else if (restartRequired) moduleUpdater.quitAndInstallUpdates();
process.nextTick(() => moduleUpdater[restartRequired ? 'quitAndInstallUpdates' : 'installPendingUpdates']()); // process.nextTick(() => moduleUpdater[restartRequired ? 'quitAndInstallUpdates' : 'installPendingUpdates']());
}
}); });
add('installing-module', ({ current }) => { add('installing-module', ({ name }) => {
currentId = current; installs.record(name, 'Waiting');
installs.record(currentId, '', 0);
installs.send();
}); });
const segmentCallback = (tracker) => (({ name }) => { const segmentCallback = (tracker) => (({ name }) => {
tracker.record(currentId, 'Complete', 100); tracker.record(name, 'Complete');
if (name === 'host') restartRequired = true; if (name === 'host') restartRequired = true;
}); });
@ -292,15 +302,19 @@ const initModuleUpdater = () => { // "Old" (not v2 / new, win32 only)
add('installing-modules-finished', callbackCheck); add('installing-modules-finished', callbackCheck);
add('no-pending-updates', callbackCheck); add('no-pending-updates', callbackCheck);
const progressCallback = (tracker) => (({ progress }) => { // add('downloading-module-progress', progressCallback(downloads));
tracker.record(currentId, '', progress); add('downloading-module-progress', ({ name, recv, total }) => {
tracker.send(); downloads.record(name, '', recv, total);
});
// add('installing-module-progress', progressCallback(installs));
add('installing-module-progress', ({ name, entries, total }) => {
installs.record(name, '', entries, total);
}); });
add('downloading-module-progress', progressCallback(downloads)); add('update-manually', (e) => {
add('installing-module-progress', progressCallback(installs)); splashState.newVersion = e.newVersion;
sendState('update-manually');
addBasic('update-manually', 'newVersion'); });
}; };
const v1_timeoutStart = () => !updateTimeout && (updateTimeout = setTimeout(scheduleNextUpdate, 10000)); const v1_timeoutStart = () => !updateTimeout && (updateTimeout = setTimeout(scheduleNextUpdate, 10000));

File diff suppressed because it is too large Load diff