1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00

integrated with new and improved autoupdate code

Signed-off-by: Jim Ehrismann <jehrismann@mirantis.com>
This commit is contained in:
Jim Ehrismann 2022-06-08 22:00:45 -04:00
parent c2c231a585
commit 7cab54e6de
5 changed files with 64 additions and 24 deletions

View File

@ -3,10 +3,11 @@
* Licensed under MIT License. See LICENSE in root directory for more information. * Licensed under MIT License. See LICENSE in root directory for more information.
*/ */
import { getInjectable } from "@ogre-tools/injectable"; import { getInjectable } from "@ogre-tools/injectable";
import type { JsonObject } from "type-fest";
import createSyncBoxInjectable from "../../utils/sync-box/create-sync-box.injectable"; import createSyncBoxInjectable from "../../utils/sync-box/create-sync-box.injectable";
import { syncBoxInjectionToken } from "../../utils/sync-box/sync-box-injection-token"; import { syncBoxInjectionToken } from "../../utils/sync-box/sync-box-injection-token";
export interface ProgressOfDownload { export interface ProgressOfDownload extends JsonObject {
percentage: number; percentage: number;
} }

View File

@ -5,16 +5,18 @@
import { getInjectable } from "@ogre-tools/injectable"; import { getInjectable } from "@ogre-tools/injectable";
import { makeObservable, observable } from "mobx"; import { makeObservable, observable } from "mobx";
export type AutoUpdateStateName = "checking" | "available" | "not-available" | "done" | "downloading" | "idle"; export type AutoUpdateStateName = "checking" | "available" | "not-available" | "done" | "downloading" | "download-failed" | "download-succeeded" | "idle";
let timerId: NodeJS.Timeout;
export class AutoUpdateState { export class AutoUpdateState {
@observable private _state: AutoUpdateStateName; @observable private _state: AutoUpdateStateName;
private timerId: NodeJS.Timeout; @observable private _version: string | undefined = undefined;
constructor(state: AutoUpdateStateName = "idle") { constructor(state: AutoUpdateStateName = "idle") {
makeObservable(this); makeObservable(this);
this.name = state; this._state = state;
} }
get name() : AutoUpdateStateName { get name() : AutoUpdateStateName {
@ -27,9 +29,18 @@ export class AutoUpdateState {
this.triggerIdle(); this.triggerIdle();
} }
get version() : string | undefined {
return this._version;
}
set version(version: string | undefined ) {
this._version = version;
this.triggerIdle();
}
private triggerIdle(): void { private triggerIdle(): void {
clearTimeout(this.timerId); clearTimeout(timerId);
this.timerId = null;
switch(this.name) { switch(this.name) {
case "checking": case "checking":
@ -40,7 +51,9 @@ export class AutoUpdateState {
case "done": case "done":
case "not-available": case "not-available":
this.timerId = setTimeout(() => this.name = "idle", 5000); case "download-failed":
case "download-succeeded":
timerId = setTimeout(() => this.name = "idle", 5000);
break; break;
} }
} }

View File

@ -5,39 +5,46 @@
import { getInjectable } from "@ogre-tools/injectable"; import { getInjectable } from "@ogre-tools/injectable";
import type { ApplicationUpdateStatusChannel, ApplicationUpdateStatusEventId } from "../../common/application-update/application-update-status-channel.injectable"; import type { ApplicationUpdateStatusChannel, ApplicationUpdateStatusEventId } from "../../common/application-update/application-update-status-channel.injectable";
import applicationUpdateStatusChannelInjectable from "../../common/application-update/application-update-status-channel.injectable"; import applicationUpdateStatusChannelInjectable from "../../common/application-update/application-update-status-channel.injectable";
import showInfoNotificationInjectable from "../components/notifications/show-info-notification.injectable"; //import showInfoNotificationInjectable from "../components/notifications/show-info-notification.injectable";
import type { MessageChannelListener } from "../../common/utils/channel/message-channel-listener-injection-token"; import type { MessageChannelListener } from "../../common/utils/channel/message-channel-listener-injection-token";
import { messageChannelListenerInjectionToken } from "../../common/utils/channel/message-channel-listener-injection-token"; import { messageChannelListenerInjectionToken } from "../../common/utils/channel/message-channel-listener-injection-token";
import AutoUpdateStateInjectable from "../../common/auto-update/auto-update-state.injectable";
const applicationUpdateStatusListenerInjectable = getInjectable({ const applicationUpdateStatusListenerInjectable = getInjectable({
id: "application-update-status-listener", id: "application-update-status-listener",
instantiate: (di): MessageChannelListener<ApplicationUpdateStatusChannel> => { instantiate: (di): MessageChannelListener<ApplicationUpdateStatusChannel> => {
const channel = di.inject(applicationUpdateStatusChannelInjectable); const channel = di.inject(applicationUpdateStatusChannelInjectable);
const showInfoNotification = di.inject(showInfoNotificationInjectable); //const showInfoNotification = di.inject(showInfoNotificationInjectable);
const autoUpdateState = di.inject(AutoUpdateStateInjectable);
const eventHandlers: Record<ApplicationUpdateStatusEventId, { handle: (version?: string) => void }> = { const eventHandlers: Record<ApplicationUpdateStatusEventId, { handle: (version?: string) => void }> = {
"checking-for-updates": { "checking-for-updates": {
handle: () => { handle: () => {
showInfoNotification("Checking for updates..."); //showInfoNotification("Checking for updates...");
autoUpdateState.name = "checking";
}, },
}, },
"no-updates-available": { "no-updates-available": {
handle: () => { handle: () => {
showInfoNotification("No new updates available"); //showInfoNotification("No new updates available");
autoUpdateState.name = "not-available";
}, },
}, },
"download-for-update-started": { "download-for-update-started": {
handle: (version) => { handle: (version) => {
showInfoNotification(`Download for version ${version} started...`); //showInfoNotification(`Download for version ${version} started...`);
autoUpdateState.name = "downloading";
autoUpdateState.version = version;
}, },
}, },
"download-for-update-failed": { "download-for-update-failed": {
handle: () => { handle: () => {
showInfoNotification("Download of update failed"); //showInfoNotification("Download of update failed");
autoUpdateState.name = "download-failed";
}, },
}, },
}; };

View File

@ -8,25 +8,39 @@ import React from "react";
import AutoUpdateStateInjectable from "../../../common/auto-update/auto-update-state.injectable"; import AutoUpdateStateInjectable from "../../../common/auto-update/auto-update-state.injectable";
import type { AutoUpdateState } from "../../../common/auto-update/auto-update-state.injectable"; import type { AutoUpdateState } from "../../../common/auto-update/auto-update-state.injectable";
import { Spinner } from "../spinner"; import { Spinner } from "../spinner";
import progressOfUpdateDownloadInjectable, { ProgressOfDownload } from "../../../common/application-update/progress-of-update-download/progress-of-update-download.injectable";
import type { SyncBox } from "../../../common/utils/sync-box/sync-box-injection-token";
interface Dependencies { interface Dependencies {
state: AutoUpdateState; state: AutoUpdateState;
progressOfUpdateDownload: SyncBox<ProgressOfDownload>;
} }
const checking = () => <><Spinner/><div>{"Checking for updates"}</div></>; const checking = () => <><Spinner/><div>{"Checking for updates..."}</div></>;
const available = () => <div>{"Update is available"}</div>; const available = () => <div>{"Update is available"}</div>;
const notAvailable = () => <div>{"No new updates available"}</div>;
const downloading = (state: AutoUpdateState, percentDone: number) => {
const {version } = state;
const notAvailable = () => { if ( percentDone === 0 ) {
return <><div>{`Download for version ${version} started `}</div><Spinner/></>;
}
return <div>{"Update is currently not available"}</div>; if ( percentDone < 100 ) {
return <div>{`Download for version ${version} ${percentDone}%...`}</div>;
}
state.name = "download-succeeded";
return null;
}; };
const downloading = () => <div>{"Downloading update"}</div>;
const done = () => <div>{"Done checking for updates"}</div>; const done = () => <div>{"Done checking for updates"}</div>;
const downloadFailed = (version: string | undefined) => <div>{`Download for version ${version} failed`}</div>;
const downloadSucceeded = (version: string | undefined) => <div>{`Download for version ${version} complete`}</div>;
const idle = () => <></>; const idle = () => <></>;
export const NonInjectedAutoUpdateComponent = observer(({ state }: Dependencies) => { export const NonInjectedAutoUpdateComponent = observer(({ state, progressOfUpdateDownload }: Dependencies) => {
switch(state.name) { switch(state.name) {
case "checking": case "checking":
@ -39,13 +53,19 @@ export const NonInjectedAutoUpdateComponent = observer(({ state }: Dependencies)
return notAvailable(); return notAvailable();
case "downloading": case "downloading":
return downloading(); const roundedPercentage = Math.round(progressOfUpdateDownload.value.get().percentage);
return downloading(state, roundedPercentage);
case "done": case "done":
return done(); return done();
default: case "download-failed":
case "idle": return downloadFailed(state.version);
case "download-succeeded":
return downloadSucceeded(state.version);
case "idle":
return idle(); return idle();
} }
@ -54,6 +74,7 @@ export const NonInjectedAutoUpdateComponent = observer(({ state }: Dependencies)
export const AutoUpdateComponent = withInjectables<Dependencies>(NonInjectedAutoUpdateComponent, { export const AutoUpdateComponent = withInjectables<Dependencies>(NonInjectedAutoUpdateComponent, {
getProps: (di, props) => ({ getProps: (di, props) => ({
state: di.inject(AutoUpdateStateInjectable), state: di.inject(AutoUpdateStateInjectable),
progressOfUpdateDownload: di.inject(progressOfUpdateDownloadInjectable),
...props, ...props,
}), }),
}); });

View File

@ -5,14 +5,12 @@
import { getInjectable } from "@ogre-tools/injectable"; import { getInjectable } from "@ogre-tools/injectable";
import { registerIpcListeners } from "./register-listeners"; import { registerIpcListeners } from "./register-listeners";
import listNamespacesForbiddenHandlerInjectable from "./list-namespaces-forbidden-handler.injectable"; import listNamespacesForbiddenHandlerInjectable from "./list-namespaces-forbidden-handler.injectable";
import AutoUpdateStateInjectable from "../../common/auto-update/auto-update-state.injectable";
const registerIpcListenersInjectable = getInjectable({ const registerIpcListenersInjectable = getInjectable({
id: "register-ipc-listeners", id: "register-ipc-listeners",
instantiate: (di) => registerIpcListeners({ instantiate: (di) => registerIpcListeners({
listNamespacesForbiddenHandler: di.inject(listNamespacesForbiddenHandlerInjectable), listNamespacesForbiddenHandler: di.inject(listNamespacesForbiddenHandlerInjectable),
updateState: di.inject(AutoUpdateStateInjectable),
}), }),
}); });