Start installing updates automatically when quitting application

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

Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>
This commit is contained in:
Janne SavolainenandMikko Aspiala committed 2022-05-20 14:08:44 +03:00
1 parent e3579d82cb
commit 244f283998
6 files changed
+215 -11

No files matched your search

@@ -22,9 +22,11 @@ const versionUpdateInjectable = getInjectable({
const discoveredVersionState = observable.box<string>();
const downloadingState = observable.box<boolean>(false);
const checkingState = observable.box<boolean>(false);
const discoveredFromUpdateChannelState = observable.box<UpdateChannel>();
return {
discoveredVersion: computed(() => discoveredVersionState.get()),
discoveredFromUpdateChannel: computed(() => discoveredFromUpdateChannelState.get()),
downloading: computed(() => downloadingState.get()),
checking: computed(() => checkingState.get()),
@@ -33,6 +35,7 @@ const versionUpdateInjectable = getInjectable({
discoveredVersionState,
checkingState,
selectedUpdateChannel.value,
discoveredFromUpdateChannelState,
),
downloadUpdate: downloadUpdateFor(
@@ -68,6 +71,7 @@ const checkForUpdatesFor =
discoveredVersionState: IObservableValue<string>,
checkingState: IObservableValue<boolean>,
selectedUpdateChannel: IComputedValue<UpdateChannel>,
discoveredFromUpdateChannelState: IObservableValue<UpdateChannel>,
) =>
async () => {
runInAction(() => {
@@ -77,11 +81,12 @@ const checkForUpdatesFor =
const checkForUpdatesStartingFromChannel =
checkForUpdatesStartingFromChannelFor(checkForPlatformUpdates);
const { updateWasDiscovered, version } = await checkForUpdatesStartingFromChannel(
const { updateWasDiscovered, version, actualUpdateChannel } = await checkForUpdatesStartingFromChannel(
selectedUpdateChannel.get(),
);
runInAction(() => {
discoveredFromUpdateChannelState.set(actualUpdateChannel);
discoveredVersionState.set(version);
checkingState.set(false);
});
@@ -90,12 +95,24 @@ const checkForUpdatesFor =
};
const checkForUpdatesStartingFromChannelFor = (checkForPlatformUpdates: CheckForPlatformUpdates) => {
const _recursiveCheck = async (updateChannel: UpdateChannel): Promise<{ updateWasDiscovered: boolean; version?: string }> => {
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 };
return {
updateWasDiscovered: true,
version: result.version,
actualUpdateChannel: updateChannel,
};
}
if (updateChannel.moreStableUpdateChannel) {
@@ -0,0 +1,25 @@
/**
* 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 { onLoadOfApplicationInjectionToken } from "../../start-main-application/runnable-tokens/on-load-of-application-injection-token";
import watchIfUpdateShouldHappenOnQuitInjectable from "./watch-if-update-should-happen-on-quit.injectable";
const startWatchingIfUpdateShouldHappenOnQuitInjectable = getInjectable({
id: "start-watching-if-update-should-happen-on-quit",
instantiate: (di) => {
const watchIfUpdateShouldHappenOnQuit = di.inject(watchIfUpdateShouldHappenOnQuitInjectable);
return {
run: () => {
watchIfUpdateShouldHappenOnQuit.start();
},
};
},
injectionToken: onLoadOfApplicationInjectionToken,
});
export default startWatchingIfUpdateShouldHappenOnQuitInjectable;
@@ -0,0 +1,25 @@
/**
* 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 watchIfUpdateShouldHappenOnQuitInjectable from "./watch-if-update-should-happen-on-quit.injectable";
import { beforeQuitOfBackEndInjectionToken } from "../../start-main-application/runnable-tokens/before-quit-of-back-end-injection-token";
const stopWatchingIfUpdateShouldHappenOnQuitInjectable = getInjectable({
id: "stop-watching-if-update-should-happen-on-quit",
instantiate: (di) => {
const watchIfUpdateShouldHappenOnQuit = di.inject(watchIfUpdateShouldHappenOnQuitInjectable);
return {
run: () => {
watchIfUpdateShouldHappenOnQuit.stop();
},
};
},
injectionToken: beforeQuitOfBackEndInjectionToken,
});
export default stopWatchingIfUpdateShouldHappenOnQuitInjectable;
@@ -0,0 +1,48 @@
/**
* 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 { 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";
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);
return getStartableStoppable("watch-if-update-should-happen-on-quit", () =>
autorun(() => {
const sufficientlyStableUpdateChannels =
getSufficientlyStableUpdateChannels(selectedUpdateChannel.value.get());
const updateIsDiscoveredFromChannel = versionUpdate.discoveredFromUpdateChannel.get();
const updateOnQuit = sufficientlyStableUpdateChannels.includes(updateIsDiscoveredFromChannel);
setUpdateOnQuit(updateOnQuit);
}),
);
},
});
const getSufficientlyStableUpdateChannels = (updateChannel: UpdateChannel): UpdateChannel[] => {
if (!updateChannel.moreStableUpdateChannel) {
return [updateChannel];
}
return [
updateChannel,
...getSufficientlyStableUpdateChannels(updateChannel.moreStableUpdateChannel),
];
};
export default watchIfUpdateShouldHappenOnQuitInjectable;