From 841077dd7810a2856b5c9870308a7d7783b973a7 Mon Sep 17 00:00:00 2001 From: Janne Savolainen Date: Mon, 16 May 2022 14:42:52 +0300 Subject: [PATCH] Split bloated dependency in smaller pieces Co-authored-by: Mikko Aspiala Signed-off-by: Janne Savolainen --- .../check-for-updates-tray-item.injectable.ts | 24 ++- .../check-for-updates.injectable.ts | 97 +++++++++++ .../download-update.injectable.ts | 38 +++++ ...application-update-tray-item.injectable.ts | 10 +- .../update-app/version-update.injectable.ts | 153 ------------------ ...update-should-happen-on-quit.injectable.ts | 8 +- 6 files changed, 162 insertions(+), 168 deletions(-) create mode 100644 src/main/update-app/check-for-updates/check-for-updates.injectable.ts create mode 100644 src/main/update-app/download-update/download-update.injectable.ts delete mode 100644 src/main/update-app/version-update.injectable.ts diff --git a/src/main/update-app/check-for-updates-tray-item.injectable.ts b/src/main/update-app/check-for-updates-tray-item.injectable.ts index 7dfd37124a..449e421ec9 100644 --- a/src/main/update-app/check-for-updates-tray-item.injectable.ts +++ b/src/main/update-app/check-for-updates-tray-item.injectable.ts @@ -6,12 +6,16 @@ import { getInjectable } from "@ogre-tools/injectable"; import { computed } from "mobx"; import updatingIsEnabledInjectable from "./updating-is-enabled.injectable"; import { trayMenuItemInjectionToken } from "../tray/tray-menu-item/tray-menu-item-injection-token"; -import versionUpdateInjectable from "./version-update.injectable"; import progressOfUpdateDownloadInjectable from "./progress-of-update-download.injectable"; import showApplicationWindowInjectable from "../start-main-application/lens-window/show-application-window.injectable"; import showNotificationInjectable from "../show-notification/show-notification.injectable"; import askBooleanInjectable from "../ask-boolean/ask-boolean.injectable"; import quitAndInstallUpdateInjectable from "../electron-app/features/quit-and-install-update.injectable"; +import discoveredVersionStateInjectable from "../../common/application-update/discovered-version/discovered-version-state.injectable"; +import downloadingUpdateStateInjectable from "../../common/application-update/downloading-update/downloading-update-state.injectable"; +import checkingForUpdatesStateInjectable from "../../common/application-update/checking-for-updates/checking-for-updates-state.injectable"; +import checkForUpdatesInjectable from "./check-for-updates/check-for-updates.injectable"; +import downloadUpdateInjectable from "./download-update/download-update.injectable"; const checkForUpdatesTrayItemInjectable = getInjectable({ id: "check-for-updates-tray-item", @@ -19,11 +23,15 @@ const checkForUpdatesTrayItemInjectable = getInjectable({ instantiate: (di) => { const showApplicationWindow = di.inject(showApplicationWindowInjectable); const updatingIsEnabled = di.inject(updatingIsEnabledInjectable); - const versionUpdate = di.inject(versionUpdateInjectable); const progressOfUpdateDownload = di.inject(progressOfUpdateDownloadInjectable); const showNotification = di.inject(showNotificationInjectable); const askBoolean = di.inject(askBooleanInjectable); const quitAndInstallUpdate = di.inject(quitAndInstallUpdateInjectable); + const discoveredVersionState = di.inject(discoveredVersionStateInjectable); + const downloadingUpdateState = di.inject(downloadingUpdateStateInjectable); + const checkingForUpdatesState = di.inject(checkingForUpdatesStateInjectable); + const checkForUpdates = di.inject(checkForUpdatesInjectable); + const downloadUpdate = di.inject(downloadUpdateInjectable); return { id: "check-for-updates", @@ -31,29 +39,29 @@ const checkForUpdatesTrayItemInjectable = getInjectable({ orderNumber: 30, label: computed(() => { - if (versionUpdate.downloading.get()) { - return `Downloading update ${versionUpdate.discoveredVersion.get()} (${progressOfUpdateDownload.value.get()}%)...`; + if (downloadingUpdateState.value.get()) { + return `Downloading update ${discoveredVersionState.value.get().version} (${progressOfUpdateDownload.value.get()}%)...`; } - if (versionUpdate.checking.get()) { + if (checkingForUpdatesState.value.get()) { return "Checking for updates..."; } return "Check for updates"; }), - enabled: computed(() => !versionUpdate.checking.get() && !versionUpdate.downloading.get()), + enabled: computed(() => !checkingForUpdatesState.value.get() && !downloadingUpdateState.value.get()), visible: computed(() => updatingIsEnabled), click: async () => { - const { updateWasDiscovered, version } = await versionUpdate.checkForUpdates(); + const { updateWasDiscovered, version } = await checkForUpdates(); if (updateWasDiscovered) { showNotification(`Download for version ${version} started...`); // Note: intentional orphan promise to make download happen in the background - versionUpdate.downloadUpdate().then(async ({ downloadWasSuccessful }) => { + downloadUpdate().then(async ({ downloadWasSuccessful }) => { if (!downloadWasSuccessful) { showNotification(`Download for update failed`); diff --git a/src/main/update-app/check-for-updates/check-for-updates.injectable.ts b/src/main/update-app/check-for-updates/check-for-updates.injectable.ts new file mode 100644 index 0000000000..b012edb96a --- /dev/null +++ b/src/main/update-app/check-for-updates/check-for-updates.injectable.ts @@ -0,0 +1,97 @@ +/** + * Copyright (c) OpenLens Authors. All rights reserved. + * Licensed under MIT License. See LICENSE in root directory for more information. + */ +import { getInjectable } from "@ogre-tools/injectable"; +import type { CheckForPlatformUpdates } from "../check-for-platform-updates/check-for-platform-updates.injectable"; +import checkForPlatformUpdatesInjectable from "../check-for-platform-updates/check-for-platform-updates.injectable"; +import type { UpdateChannel } from "../update-channels"; +import selectedUpdateChannelInjectable from "../selected-update-channel.injectable"; +import showNotificationInjectable from "../../show-notification/show-notification.injectable"; +import checkingForUpdatesStateInjectable from "../../../common/application-update/checking-for-updates/checking-for-updates-state.injectable"; +import discoveredVersionStateInjectable from "../../../common/application-update/discovered-version/discovered-version-state.injectable"; +import { runInAction } from "mobx"; + +const checkForUpdatesInjectable = getInjectable({ + id: "check-for-updates", + + instantiate: (di) => { + const selectedUpdateChannel = di.inject(selectedUpdateChannelInjectable); + const showNotification = di.inject(showNotificationInjectable); + + const checkForPlatformUpdates = di.inject( + checkForPlatformUpdatesInjectable, + ); + + const checkingForUpdatesState = di.inject( + checkingForUpdatesStateInjectable, + ); + + const discoveredVersionState = di.inject(discoveredVersionStateInjectable); + + return async () => { + runInAction(() => { + checkingForUpdatesState.set(true); + }); + + const checkForUpdatesStartingFromChannel = + checkForUpdatesStartingFromChannelFor(checkForPlatformUpdates); + + showNotification("Checking for updates..."); + + const { updateWasDiscovered, version, actualUpdateChannel } = + await checkForUpdatesStartingFromChannel(selectedUpdateChannel.value.get()); + + if (!updateWasDiscovered) { + showNotification("No new updates available"); + } + + runInAction(() => { + if (!updateWasDiscovered) { + discoveredVersionState.set(null); + } else { + discoveredVersionState.set({ + version, + updateChannel: actualUpdateChannel, + }); + } + + checkingForUpdatesState.set(false); + }); + + return { updateWasDiscovered, version }; + }; + }, +}); + +export default checkForUpdatesInjectable; + +const checkForUpdatesStartingFromChannelFor = ( + checkForPlatformUpdates: CheckForPlatformUpdates, +) => { + const _recursiveCheck = async ( + updateChannel: UpdateChannel, + ): Promise<{ + updateWasDiscovered: boolean; + version?: string; + actualUpdateChannel?: UpdateChannel; + }> => { + const result = await checkForPlatformUpdates(updateChannel); + + if (result.updateWasDiscovered) { + return { + updateWasDiscovered: true, + version: result.version, + actualUpdateChannel: updateChannel, + }; + } + + if (updateChannel.moreStableUpdateChannel) { + return await _recursiveCheck(updateChannel.moreStableUpdateChannel); + } + + return { updateWasDiscovered: false }; + }; + + return _recursiveCheck; +}; diff --git a/src/main/update-app/download-update/download-update.injectable.ts b/src/main/update-app/download-update/download-update.injectable.ts new file mode 100644 index 0000000000..8ebc7b609c --- /dev/null +++ b/src/main/update-app/download-update/download-update.injectable.ts @@ -0,0 +1,38 @@ +/** + * Copyright (c) OpenLens Authors. All rights reserved. + * Licensed under MIT License. See LICENSE in root directory for more information. + */ +import { getInjectable } from "@ogre-tools/injectable"; +import downloadPlatformUpdateInjectable from "../download-platform-update/download-platform-update.injectable"; +import downloadingUpdateStateInjectable from "../../../common/application-update/downloading-update/downloading-update-state.injectable"; +import discoveredVersionStateInjectable from "../../../common/application-update/discovered-version/discovered-version-state.injectable"; +import { runInAction } from "mobx"; + +const downloadUpdateInjectable = getInjectable({ + id: "download-update", + instantiate: (di) => { + const downloadPlatformUpdate = di.inject(downloadPlatformUpdateInjectable); + const downloadingUpdateState = di.inject(downloadingUpdateStateInjectable); + const discoveredVersionState = di.inject(discoveredVersionStateInjectable); + + return async () => { + runInAction(() => { + downloadingUpdateState.set(true); + }); + + const { downloadWasSuccessful } = await downloadPlatformUpdate(); + + runInAction(() => { + if (!downloadWasSuccessful) { + discoveredVersionState.set(null); + } + + downloadingUpdateState.set(false); + }); + + return { downloadWasSuccessful }; + }; + }, +}); + +export default downloadUpdateInjectable; diff --git a/src/main/update-app/install-application-update-tray-item.injectable.ts b/src/main/update-app/install-application-update-tray-item.injectable.ts index c0bacc3b2d..67b3e19254 100644 --- a/src/main/update-app/install-application-update-tray-item.injectable.ts +++ b/src/main/update-app/install-application-update-tray-item.injectable.ts @@ -6,14 +6,16 @@ import { getInjectable } from "@ogre-tools/injectable"; import { computed } from "mobx"; import { trayMenuItemInjectionToken } from "../tray/tray-menu-item/tray-menu-item-injection-token"; import quitAndInstallUpdateInjectable from "../electron-app/features/quit-and-install-update.injectable"; -import versionUpdateInjectable from "./version-update.injectable"; +import discoveredVersionStateInjectable from "../../common/application-update/discovered-version/discovered-version-state.injectable"; +import downloadingUpdateStateInjectable from "../../common/application-update/downloading-update/downloading-update-state.injectable"; const installApplicationUpdateTrayItemInjectable = getInjectable({ id: "install-update-tray-item", instantiate: (di) => { const quitAndInstallUpdate = di.inject(quitAndInstallUpdateInjectable); - const versionUpdate = di.inject(versionUpdateInjectable); + const discoveredVersionState = di.inject(discoveredVersionStateInjectable); + const downloadingUpdateState = di.inject(downloadingUpdateStateInjectable); return { id: "install-update", @@ -21,7 +23,7 @@ const installApplicationUpdateTrayItemInjectable = getInjectable({ orderNumber: 50, label: computed(() => { - const versionToBeInstalled = versionUpdate.discoveredVersion.get(); + const versionToBeInstalled = discoveredVersionState.value.get().version; return `Install update ${versionToBeInstalled}`; }), @@ -29,7 +31,7 @@ const installApplicationUpdateTrayItemInjectable = getInjectable({ enabled: computed(() => true), visible: computed( - () => versionUpdate.discoveredVersion.get() && !versionUpdate.downloading.get(), + () => discoveredVersionState.value.get() && !downloadingUpdateState.value.get(), ), click: () => { diff --git a/src/main/update-app/version-update.injectable.ts b/src/main/update-app/version-update.injectable.ts deleted file mode 100644 index 1a9669e11b..0000000000 --- a/src/main/update-app/version-update.injectable.ts +++ /dev/null @@ -1,153 +0,0 @@ -/** - * Copyright (c) OpenLens Authors. All rights reserved. - * Licensed under MIT License. See LICENSE in root directory for more information. - */ -import { getInjectable } from "@ogre-tools/injectable"; -import type { IComputedValue } from "mobx"; -import { computed, runInAction } from "mobx"; -import selectedUpdateChannelInjectable from "./selected-update-channel.injectable"; -import downloadPlatformUpdateInjectable from "./download-platform-update/download-platform-update.injectable"; -import type { CheckForPlatformUpdates } from "./check-for-platform-updates/check-for-platform-updates.injectable"; -import checkForPlatformUpdatesInjectable from "./check-for-platform-updates/check-for-platform-updates.injectable"; -import type { UpdateChannel } from "./update-channels"; -import showNotificationInjectable from "../show-notification/show-notification.injectable"; -import checkingForUpdatesStateInjectable from "../../common/application-update/checking-for-updates/checking-for-updates-state.injectable"; -import type { SyncBox } from "../../common/sync-box/create-sync-box.injectable"; -import downloadingUpdateStateInjectable from "../../common/application-update/downloading-update/downloading-update-state.injectable"; -import discoveredVersionStateInjectable from "../../common/application-update/discovered-version/discovered-version-state.injectable"; - -const versionUpdateInjectable = getInjectable({ - id: "version-update", - - instantiate: (di) => { - const selectedUpdateChannel = di.inject(selectedUpdateChannelInjectable); - const downloadPlatformUpdate = di.inject(downloadPlatformUpdateInjectable); - const checkForPlatformUpdates = di.inject(checkForPlatformUpdatesInjectable); - const showNotification = di.inject(showNotificationInjectable); - const checkingForUpdatesState = di.inject(checkingForUpdatesStateInjectable); - const downloadingUpdateState = di.inject(downloadingUpdateStateInjectable); - const discoveredVersionState = di.inject(discoveredVersionStateInjectable); - - return { - discoveredVersion: computed(() => { - const discoveredVersion = discoveredVersionState.value.get(); - - return discoveredVersion?.version; - }), - - discoveredFromUpdateChannel: computed(() => { - const discoveredVersion = discoveredVersionState.value.get(); - - return discoveredVersion?.updateChannel; - }), - - downloading: computed(() => downloadingUpdateState.value.get()), - checking: computed(() => checkingForUpdatesState.value.get()), - - checkForUpdates: checkForUpdatesFor( - checkForPlatformUpdates, - discoveredVersionState, - selectedUpdateChannel.value, - showNotification, - checkingForUpdatesState, - ), - - downloadUpdate: downloadUpdateFor( - downloadPlatformUpdate, - downloadingUpdateState, - discoveredVersionState, - ), - }; - }, -}); - -export default versionUpdateInjectable; - -const downloadUpdateFor = - ( - downloadPlatformUpdate: () => Promise<{ downloadWasSuccessful: boolean }>, - downloadingUpdateState: SyncBox, - discoveredVersionState: SyncBox<{ version: string; updateChannel: UpdateChannel }>, - ) => - async () => { - runInAction(() => { - downloadingUpdateState.set(true); - }); - - const { downloadWasSuccessful } = await downloadPlatformUpdate(); - - runInAction(() => { - if (!downloadWasSuccessful) { - discoveredVersionState.set(null); - } - - downloadingUpdateState.set(false); - }); - - return { downloadWasSuccessful }; - }; - -const checkForUpdatesFor = - ( - checkForPlatformUpdates: CheckForPlatformUpdates, - discoveredVersionState: SyncBox<{ version: string; updateChannel: UpdateChannel }>, - selectedUpdateChannel: IComputedValue, - showNotification: (message: string) => void, - checkingForUpdatesState: SyncBox, - ) => - async () => { - runInAction(() => { - checkingForUpdatesState.set(true); - }); - - const checkForUpdatesStartingFromChannel = - checkForUpdatesStartingFromChannelFor(checkForPlatformUpdates); - - showNotification("Checking for updates..."); - - const { updateWasDiscovered, version, actualUpdateChannel } = await checkForUpdatesStartingFromChannel( - selectedUpdateChannel.get(), - ); - - if (!updateWasDiscovered) { - showNotification("No new updates available"); - } - - runInAction(() => { - discoveredVersionState.set({ version, updateChannel: actualUpdateChannel }); - checkingForUpdatesState.set(false); - }); - - return { updateWasDiscovered, version }; - }; - - -const checkForUpdatesStartingFromChannelFor = ( - checkForPlatformUpdates: CheckForPlatformUpdates, -) => { - const _recursiveCheck = async ( - updateChannel: UpdateChannel, - ): Promise<{ - updateWasDiscovered: boolean; - version?: string; - actualUpdateChannel?: UpdateChannel; - }> => { - const result = await checkForPlatformUpdates(updateChannel); - - if (result.updateWasDiscovered) { - return { - updateWasDiscovered: true, - version: result.version, - actualUpdateChannel: updateChannel, - }; - } - - if (updateChannel.moreStableUpdateChannel) { - return await _recursiveCheck(updateChannel.moreStableUpdateChannel); - } - - return { updateWasDiscovered: false }; - }; - - return _recursiveCheck; -}; diff --git a/src/main/update-app/watch-if-update-should-happen-on-quit/watch-if-update-should-happen-on-quit.injectable.ts b/src/main/update-app/watch-if-update-should-happen-on-quit/watch-if-update-should-happen-on-quit.injectable.ts index 2f91597146..8fb73908a3 100644 --- a/src/main/update-app/watch-if-update-should-happen-on-quit/watch-if-update-should-happen-on-quit.injectable.ts +++ b/src/main/update-app/watch-if-update-should-happen-on-quit/watch-if-update-should-happen-on-quit.injectable.ts @@ -6,24 +6,26 @@ import { getInjectable } from "@ogre-tools/injectable"; import { autorun } from "mobx"; import { getStartableStoppable } from "../../../common/utils/get-startable-stoppable"; import setUpdateOnQuitInjectable from "../../electron-app/features/set-update-on-quit.injectable"; -import versionUpdateInjectable from "../version-update.injectable"; import selectedUpdateChannelInjectable from "../selected-update-channel.injectable"; import type { UpdateChannel } from "../update-channels"; +import discoveredVersionStateInjectable from "../../../common/application-update/discovered-version/discovered-version-state.injectable"; const watchIfUpdateShouldHappenOnQuitInjectable = getInjectable({ id: "watch-if-update-should-happen-on-quit", instantiate: (di) => { const setUpdateOnQuit = di.inject(setUpdateOnQuitInjectable); - const versionUpdate = di.inject(versionUpdateInjectable); const selectedUpdateChannel = di.inject(selectedUpdateChannelInjectable); + const discoveredVersionState = di.inject(discoveredVersionStateInjectable); return getStartableStoppable("watch-if-update-should-happen-on-quit", () => autorun(() => { const sufficientlyStableUpdateChannels = getSufficientlyStableUpdateChannels(selectedUpdateChannel.value.get()); - const updateIsDiscoveredFromChannel = versionUpdate.discoveredFromUpdateChannel.get(); + const discoveredVersion = discoveredVersionState.value.get(); + + const updateIsDiscoveredFromChannel = discoveredVersion?.updateChannel; const updateOnQuit = sufficientlyStableUpdateChannels.includes(updateIsDiscoveredFromChannel);