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

Split bloated dependency in smaller pieces

Co-authored-by: Mikko Aspiala <mikko.aspiala@gmail.com>

Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>
This commit is contained in:
Janne Savolainen 2022-05-16 14:42:52 +03:00
parent 3d2c940a7c
commit 841077dd78
No known key found for this signature in database
GPG Key ID: 8C6CFB2FFFE8F68A
6 changed files with 162 additions and 168 deletions

View File

@ -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`);

View File

@ -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;
};

View File

@ -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;

View File

@ -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: () => {

View File

@ -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<boolean>,
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<UpdateChannel>,
showNotification: (message: string) => void,
checkingForUpdatesState: SyncBox<boolean>,
) =>
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;
};

View File

@ -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);