[Splash > Backend] Rewrite old updater to use same new UIProgress class for UI tracking (#22)
This commit is contained in:
parent
f1aed17167
commit
cc77907845
|
@ -141,7 +141,12 @@ exports.APP_SHOULD_SHOW = APP_SHOULD_SHOW;
|
||||||
exports.events = events;
|
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() {
|
constructor(stateId) {
|
||||||
|
this.stateId = stateId;
|
||||||
|
this.reset();
|
||||||
|
}
|
||||||
|
|
||||||
|
reset() {
|
||||||
Object.assign(this, {
|
Object.assign(this, {
|
||||||
progress: new Map(),
|
progress: new Map(),
|
||||||
done: new Set(),
|
done: new Set(),
|
||||||
|
@ -159,7 +164,7 @@ class UIProgress { // Generic class to track updating and sent states to splash
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
sendState(id) {
|
sendState() {
|
||||||
if (this.progress.size > 0 && this.progress.size > this.done.size) {
|
if (this.progress.size > 0 && this.progress.size > this.done.size) {
|
||||||
splashState = {
|
splashState = {
|
||||||
current: this.done.size + 1,
|
current: this.done.size + 1,
|
||||||
|
@ -167,7 +172,7 @@ class UIProgress { // Generic class to track updating and sent states to splash
|
||||||
progress: [...this.progress.values()].reduce((a, x) => a + x, 0) / this.total.size
|
progress: [...this.progress.values()].reduce((a, x) => a + x, 0) / this.total.size
|
||||||
};
|
};
|
||||||
|
|
||||||
sendState(id);
|
sendState(this.stateId);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -185,8 +190,8 @@ const updateUntilCurrent = async () => {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
let installedAnything = false;
|
let installedAnything = false;
|
||||||
const downloads = new UIProgress();
|
const downloads = new UIProgress(DOWNLOADING_UPDATES);
|
||||||
const installs = new UIProgress();
|
const installs = new UIProgress(INSTALLING_UPDATES);
|
||||||
|
|
||||||
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;
|
||||||
|
@ -196,7 +201,7 @@ const updateUntilCurrent = async () => {
|
||||||
|
|
||||||
if (download != null) downloads.record(download.package_sha256, state, percent);
|
if (download != null) downloads.record(download.package_sha256, state, percent);
|
||||||
|
|
||||||
if (!downloads.sendState(DOWNLOADING_UPDATES)) installs.sendState(INSTALLING_UPDATES);
|
if (!downloads.sendState()) installs.sendState();
|
||||||
|
|
||||||
if (install == null) return;
|
if (install == null) return;
|
||||||
|
|
||||||
|
@ -239,10 +244,8 @@ const initModuleUpdater = () => { // "Old" (not v2 / new, win32 only)
|
||||||
|
|
||||||
const callbackCheck = () => moduleUpdater.checkForUpdates();
|
const callbackCheck = () => moduleUpdater.checkForUpdates();
|
||||||
|
|
||||||
const callbackProgress = (e) => {
|
const downloads = new UIProgress(DOWNLOADING_UPDATES);
|
||||||
delete splashState.progress;
|
const installs = new UIProgress(INSTALLING_UPDATES);
|
||||||
if (e.name === 'host') restartRequired = true;
|
|
||||||
};
|
|
||||||
|
|
||||||
const handleFail = () => {
|
const handleFail = () => {
|
||||||
scheduleNextUpdate();
|
scheduleNextUpdate();
|
||||||
|
@ -254,9 +257,22 @@ 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();
|
||||||
|
|
||||||
|
installs.reset();
|
||||||
|
downloads.reset();
|
||||||
|
|
||||||
if (!succeeded) {
|
if (!succeeded) {
|
||||||
handleFail();
|
handleFail();
|
||||||
} else if (updateCount === 0) {
|
} else if (updateCount === 0) {
|
||||||
|
@ -269,8 +285,8 @@ const initModuleUpdater = () => { // "Old" (not v2 / new, win32 only)
|
||||||
add(DOWNLOADING_MODULE, ({ current, total }) => {
|
add(DOWNLOADING_MODULE, ({ current, total }) => {
|
||||||
v1_timeoutStop();
|
v1_timeoutStop();
|
||||||
|
|
||||||
splashState = { current, total };
|
if (total !== currentTotal) updateTotal(total);
|
||||||
sendState(DOWNLOADING_UPDATES);
|
currentId = current;
|
||||||
});
|
});
|
||||||
|
|
||||||
add(DOWNLOADING_MODULES_FINISHED, ({ failed }) => {
|
add(DOWNLOADING_MODULES_FINISHED, ({ failed }) => {
|
||||||
|
@ -287,19 +303,32 @@ const initModuleUpdater = () => { // "Old" (not v2 / new, win32 only)
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
add(INSTALLING_MODULE, ({ current, total }) => {
|
add(INSTALLING_MODULE, ({ current }) => {
|
||||||
splashState = { current, total };
|
currentId = current;
|
||||||
sendState(INSTALLING_UPDATES);
|
|
||||||
|
installs.record(currentId, '', 0);
|
||||||
|
installs.sendState();
|
||||||
});
|
});
|
||||||
|
|
||||||
add(DOWNLOADED_MODULE, callbackProgress);
|
const segmentCallback = (tracker) => (({ name }) => {
|
||||||
add(INSTALLED_MODULE, callbackProgress);
|
tracker.record(currentId, 'Complete', 100);
|
||||||
|
if (name === 'host') restartRequired = true;
|
||||||
|
});
|
||||||
|
|
||||||
|
add(DOWNLOADED_MODULE, segmentCallback(downloads));
|
||||||
|
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);
|
||||||
|
|
||||||
addBasic(DOWNLOADING_MODULE_PROGRESS, 'progress', DOWNLOADING_UPDATES);
|
const progressCallback = (tracker) => (({ progress }) => {
|
||||||
addBasic(INSTALLING_MODULE_PROGRESS, 'progress', INSTALLING_UPDATES);
|
tracker.record(currentId, '', progress);
|
||||||
|
tracker.sendState();
|
||||||
|
});
|
||||||
|
|
||||||
|
add(DOWNLOADING_MODULE_PROGRESS, progressCallback(downloads));
|
||||||
|
add(INSTALLING_MODULE_PROGRESS, progressCallback(installs));
|
||||||
|
|
||||||
addBasic(UPDATE_MANUALLY, 'newVersion');
|
addBasic(UPDATE_MANUALLY, 'newVersion');
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -312,4 +341,4 @@ const scheduleNextUpdate = (callback = moduleUpdater.checkForUpdates) => { // Us
|
||||||
const wait = Math.min(updateAttempt * 10, 60);
|
const wait = Math.min(updateAttempt * 10, 60);
|
||||||
splashState.seconds = wait;
|
splashState.seconds = wait;
|
||||||
setTimeout(callback, wait * 1000);
|
setTimeout(callback, wait * 1000);
|
||||||
};
|
};
|
Loading…
Reference in New Issue