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

Switch to using competition to setup app paths

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-03-29 10:57:30 +03:00
parent b8fd1299ee
commit 3782b36809
No known key found for this signature in database
GPG Key ID: 8C6CFB2FFFE8F68A
7 changed files with 142 additions and 100 deletions

View File

@ -0,0 +1,34 @@
/**
* 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 { AppPaths } from "./app-path-injection-token";
const appPathsStateInjectable = getInjectable({
id: "app-paths-state",
instantiate: () => {
let state: AppPaths;
return {
get: () =>{
if (!state) {
throw new Error("Tried to get app paths before state is setupped.");
}
return state;
},
set: (newState: AppPaths) => {
if (state) {
throw new Error("Tried to overwrite existing state of app paths.");
}
state = newState;
},
};
},
});
export default appPathsStateInjectable;

View File

@ -0,0 +1,15 @@
/**
* 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 { appPathsInjectionToken } from "./app-path-injection-token";
import appPathsStateInjectable from "./app-paths-state.injectable";
const appPathsInjectable = getInjectable({
id: "app-paths",
instantiate: (di) => di.inject(appPathsStateInjectable).get(),
injectionToken: appPathsInjectionToken,
});
export default appPathsInjectable;

View File

@ -40,7 +40,7 @@ describe("app-paths", () => {
recent: "some-recent", recent: "some-recent",
temp: "some-temp", temp: "some-temp",
videos: "some-videos", videos: "some-videos",
userData: "some-irrelevant", userData: "some-irrelevant-user-data",
}; };
mainDi.override( mainDi.override(

View File

@ -1,71 +0,0 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import type {
DiContainerForSetup } from "@ogre-tools/injectable";
import {
getInjectable,
} from "@ogre-tools/injectable";
import {
appPathsInjectionToken,
appPathsIpcChannel,
} from "../../common/app-paths/app-path-injection-token";
import registerChannelInjectable from "./register-channel/register-channel.injectable";
import { getAppPaths } from "./get-app-paths";
import getElectronAppPathInjectable from "./get-electron-app-path/get-electron-app-path.injectable";
import setElectronAppPathInjectable from "./set-electron-app-path/set-electron-app-path.injectable";
import appNameInjectable from "./app-name/app-name.injectable";
import directoryForIntegrationTestingInjectable from "./directory-for-integration-testing/directory-for-integration-testing.injectable";
import joinPathsInjectable from "../../common/path/join-paths.injectable";
const appPathsInjectable = getInjectable({
id: "app-paths",
setup: async (di) => {
const directoryForIntegrationTesting = await di.inject(
directoryForIntegrationTestingInjectable,
);
if (directoryForIntegrationTesting) {
await setupPathForAppDataInIntegrationTesting(di, directoryForIntegrationTesting);
}
await setupPathForUserData(di);
await registerAppPathsChannel(di);
},
instantiate: (di) =>
getAppPaths({ getAppPath: di.inject(getElectronAppPathInjectable) }),
injectionToken: appPathsInjectionToken,
});
export default appPathsInjectable;
const registerAppPathsChannel = async (di: DiContainerForSetup) => {
const registerChannel = await di.inject(registerChannelInjectable);
const appPaths = await di.inject(appPathsInjectable);
registerChannel(appPathsIpcChannel, () => appPaths);
};
const setupPathForUserData = async (di: DiContainerForSetup) => {
const setElectronAppPath = await di.inject(setElectronAppPathInjectable);
const appName = await di.inject(appNameInjectable);
const getAppPath = await di.inject(getElectronAppPathInjectable);
const joinPaths = await di.inject(joinPathsInjectable);
const appDataPath = getAppPath("appData");
setElectronAppPath("userData", joinPaths(appDataPath, appName));
};
// Todo: this kludge is here only until we have a proper place to setup integration testing.
const setupPathForAppDataInIntegrationTesting = async (di: DiContainerForSetup, appDataPath: string) => {
const setElectronAppPath = await di.inject(setElectronAppPathInjectable);
setElectronAppPath("appData", appDataPath);
};

View File

@ -0,0 +1,59 @@
/**
* 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 { AppPaths } from "../../common/app-paths/app-path-injection-token";
import getElectronAppPathInjectable from "./get-electron-app-path/get-electron-app-path.injectable";
import setElectronAppPathInjectable from "./set-electron-app-path/set-electron-app-path.injectable";
import appNameInjectable from "./app-name/app-name.injectable";
import directoryForIntegrationTestingInjectable from "./directory-for-integration-testing/directory-for-integration-testing.injectable";
import { setupableInjectionToken } from "../../common/setupable-injection-token/setupable-injection-token";
import appPathsStateInjectable from "../../common/app-paths/app-paths-state.injectable";
import { pathNames } from "../../common/app-paths/app-path-names";
import { fromPairs, map } from "lodash/fp";
import { pipeline } from "@ogre-tools/fp";
import { appPathsIpcChannel } from "../../common/app-paths/app-path-injection-token";
import registerChannelInjectable from "./register-channel/register-channel.injectable";
import joinPathsInjectable from "../../common/path/join-paths.injectable";
const setupAppPathsInjectable = getInjectable({
id: "setup-app-paths",
instantiate: (di) => {
const setElectronAppPath = di.inject(setElectronAppPathInjectable);
const appName = di.inject(appNameInjectable);
const getAppPath = di.inject(getElectronAppPathInjectable);
const appPathsState = di.inject(appPathsStateInjectable);
const registerChannel = di.inject(registerChannelInjectable);
const directoryForIntegrationTesting = di.inject(directoryForIntegrationTestingInjectable);
const joinPaths = di.inject(joinPathsInjectable);
return {
doSetup: () => {
if (directoryForIntegrationTesting) {
setElectronAppPath("appData", directoryForIntegrationTesting);
}
const appDataPath = getAppPath("appData");
setElectronAppPath("userData", joinPaths(appDataPath, appName));
const appPaths = pipeline(
pathNames,
map(name => [name, getAppPath(name)]),
fromPairs,
) as AppPaths;
appPathsState.set(appPaths);
registerChannel(appPathsIpcChannel, () => appPaths);
},
};
},
injectionToken: setupableInjectionToken,
});
export default setupAppPathsInjectable;

View File

@ -1,28 +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 { AppPaths } from "../../common/app-paths/app-path-injection-token";
import { appPathsInjectionToken, appPathsIpcChannel } from "../../common/app-paths/app-path-injection-token";
import getValueFromRegisteredChannelInjectable from "./get-value-from-registered-channel/get-value-from-registered-channel.injectable";
let syncAppPaths: AppPaths;
const appPathsInjectable = getInjectable({
id: "app-paths",
setup: async (di) => {
const getValueFromRegisteredChannel = await di.inject(
getValueFromRegisteredChannelInjectable,
);
syncAppPaths = await getValueFromRegisteredChannel(appPathsIpcChannel);
},
instantiate: () => syncAppPaths,
injectionToken: appPathsInjectionToken,
});
export default appPathsInjectable;

View File

@ -0,0 +1,33 @@
/**
* 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 { AppPaths, appPathsIpcChannel } from "../../common/app-paths/app-path-injection-token";
import getValueFromRegisteredChannelInjectable from "./get-value-from-registered-channel/get-value-from-registered-channel.injectable";
import { setupableInjectionToken } from "../../common/setupable-injection-token/setupable-injection-token";
import appPathsStateInjectable from "../../common/app-paths/app-paths-state.injectable";
let syncAppPaths: AppPaths;
const setupAppPathsInjectable = getInjectable({
id: "setup-app-paths",
instantiate: (di) => ({
doSetup: async () => {
const getValueFromRegisteredChannel = di.inject(
getValueFromRegisteredChannelInjectable,
);
syncAppPaths = await getValueFromRegisteredChannel(appPathsIpcChannel);
const appPathsState = di.inject(appPathsStateInjectable);
appPathsState.set(syncAppPaths);
},
}),
injectionToken: setupableInjectionToken,
});
export default setupAppPathsInjectable;