From f4b1a87dac9e027176e5b789fbc0807fe622576a Mon Sep 17 00:00:00 2001 From: Sebastian Malton Date: Wed, 14 Sep 2022 11:48:58 -0400 Subject: [PATCH] Convert all injectables for AppPaths to be InitializableState - Simplifies the number of injectable and consolidates the state checking Signed-off-by: Sebastian Malton --- .../app-paths/app-paths-state.injectable.ts | 34 ------------ src/common/app-paths/app-paths.injectable.ts | 15 ------ src/common/app-paths/app-paths.test.ts | 8 +-- src/common/app-paths/channel.ts | 12 +++++ .../{app-path-injection-token.ts => token.ts} | 7 +-- .../throw-with-incorrect-hierarchy-for.ts | 2 +- ...ths-request-channel-listener.injectable.ts | 4 +- src/main/app-paths/get-app-paths.ts | 15 ------ src/main/app-paths/impl.injectable.ts | 39 ++++++++++++++ src/main/app-paths/init.injectable.ts | 22 ++++++++ .../app-paths/setup-app-paths.injectable.ts | 54 ------------------- src/renderer/app-paths/impl.injectable.ts | 21 ++++++++ src/renderer/app-paths/init.injectable.ts | 22 ++++++++ .../app-paths/setup-app-paths.injectable.ts | 31 ----------- 14 files changed, 127 insertions(+), 159 deletions(-) delete mode 100644 src/common/app-paths/app-paths-state.injectable.ts delete mode 100644 src/common/app-paths/app-paths.injectable.ts create mode 100644 src/common/app-paths/channel.ts rename src/common/app-paths/{app-path-injection-token.ts => token.ts} (61%) delete mode 100644 src/main/app-paths/get-app-paths.ts create mode 100644 src/main/app-paths/impl.injectable.ts create mode 100644 src/main/app-paths/init.injectable.ts delete mode 100644 src/main/app-paths/setup-app-paths.injectable.ts create mode 100644 src/renderer/app-paths/impl.injectable.ts create mode 100644 src/renderer/app-paths/init.injectable.ts delete mode 100644 src/renderer/app-paths/setup-app-paths.injectable.ts diff --git a/src/common/app-paths/app-paths-state.injectable.ts b/src/common/app-paths/app-paths-state.injectable.ts deleted file mode 100644 index 5487d428b2..0000000000 --- a/src/common/app-paths/app-paths-state.injectable.ts +++ /dev/null @@ -1,34 +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 "./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; diff --git a/src/common/app-paths/app-paths.injectable.ts b/src/common/app-paths/app-paths.injectable.ts deleted file mode 100644 index 803f9e1380..0000000000 --- a/src/common/app-paths/app-paths.injectable.ts +++ /dev/null @@ -1,15 +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 { 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; diff --git a/src/common/app-paths/app-paths.test.ts b/src/common/app-paths/app-paths.test.ts index ff4bd88988..5590551472 100644 --- a/src/common/app-paths/app-paths.test.ts +++ b/src/common/app-paths/app-paths.test.ts @@ -2,8 +2,8 @@ * Copyright (c) OpenLens Authors. All rights reserved. * Licensed under MIT License. See LICENSE in root directory for more information. */ -import type { AppPaths } from "./app-path-injection-token"; -import { appPathsInjectionToken } from "./app-path-injection-token"; +import type { AppPaths } from "./token"; +import { appPathsInjectionToken } from "./token"; import getElectronAppPathInjectable from "../../main/app-paths/get-electron-app-path/get-electron-app-path.injectable"; import type { PathName } from "./app-path-names"; import setElectronAppPathInjectable from "../../main/app-paths/set-electron-app-path/set-electron-app-path.injectable"; @@ -130,7 +130,7 @@ describe("app-paths", () => { }); it("given in renderer, when injecting path for app data, has integration specific app data path", () => { - const { appData, userData } = windowDi.inject(appPathsInjectionToken); + const { appData, userData } = windowDi.inject(appPathsInjectionToken).get(); expect({ appData, userData }).toEqual({ appData: "some-integration-testing-app-data", @@ -139,7 +139,7 @@ describe("app-paths", () => { }); it("given in main, when injecting path for app data, has integration specific app data path", () => { - const { appData, userData } = windowDi.inject(appPathsInjectionToken); + const { appData, userData } = windowDi.inject(appPathsInjectionToken).get(); expect({ appData, userData }).toEqual({ appData: "some-integration-testing-app-data", diff --git a/src/common/app-paths/channel.ts b/src/common/app-paths/channel.ts new file mode 100644 index 0000000000..fd11ad0dfb --- /dev/null +++ b/src/common/app-paths/channel.ts @@ -0,0 +1,12 @@ +/** + * Copyright (c) OpenLens Authors. All rights reserved. + * Licensed under MIT License. See LICENSE in root directory for more information. + */ +import type { AppPaths } from "./token"; +import type { RequestChannel } from "../utils/channel/request-channel-injection-token"; + +export type AppPathsChannel = RequestChannel; + +export const appPathsChannel: AppPathsChannel = { + id: "app-paths", +}; diff --git a/src/common/app-paths/app-path-injection-token.ts b/src/common/app-paths/token.ts similarity index 61% rename from src/common/app-paths/app-path-injection-token.ts rename to src/common/app-paths/token.ts index e29bcdbebf..9f431a53a8 100644 --- a/src/common/app-paths/app-path-injection-token.ts +++ b/src/common/app-paths/token.ts @@ -3,10 +3,11 @@ * Licensed under MIT License. See LICENSE in root directory for more information. */ import { getInjectionToken } from "@ogre-tools/injectable"; +import type { InitializableState } from "../initializable-state/create"; import type { PathName } from "./app-path-names"; export type AppPaths = Record; -export const appPathsInjectionToken = getInjectionToken({ id: "app-paths-token" }); - - +export const appPathsInjectionToken = getInjectionToken>({ + id: "app-paths-token", +}); diff --git a/src/common/runnable/throw-with-incorrect-hierarchy-for.ts b/src/common/runnable/throw-with-incorrect-hierarchy-for.ts index bddf8037c2..2caff5233e 100644 --- a/src/common/runnable/throw-with-incorrect-hierarchy-for.ts +++ b/src/common/runnable/throw-with-incorrect-hierarchy-for.ts @@ -5,7 +5,7 @@ import type { Runnable } from "./run-many-for"; import type { RunnableSync } from "./run-many-sync-for"; -export const throwWithIncorrectHierarchyFor = (injectionTokenId: string, allRunnables: Runnable[] | RunnableSync[]) => ( +export const throwWithIncorrectHierarchyFor = (injectionTokenId: string, allRunnables: (Runnable | RunnableSync)[]) => ( (runnable: Runnable | RunnableSync) => { if (runnable.runAfter && !allRunnables.includes(runnable.runAfter)) { throw new Error(`Tried to run runnable "${runnable.id}" after the runnable "${runnable.runAfter.id}" which does not share the "${injectionTokenId}" injection token.`); diff --git a/src/main/app-paths/app-paths-request-channel-listener.injectable.ts b/src/main/app-paths/app-paths-request-channel-listener.injectable.ts index 568d63f1ea..436f9f7082 100644 --- a/src/main/app-paths/app-paths-request-channel-listener.injectable.ts +++ b/src/main/app-paths/app-paths-request-channel-listener.injectable.ts @@ -3,15 +3,15 @@ * Licensed under MIT License. See LICENSE in root directory for more information. */ import { appPathsChannel } from "../../common/app-paths/app-paths-channel"; -import appPathsInjectable from "../../common/app-paths/app-paths.injectable"; import { getRequestChannelListenerInjectable } from "../utils/channel/channel-listeners/listener-tokens"; +import appPathsInjectable from "./impl.injectable"; const appPathsRequestChannelListenerInjectable = getRequestChannelListenerInjectable({ channel: appPathsChannel, handler: (di) => { const appPaths = di.inject(appPathsInjectable); - return () => appPaths; + return () => appPaths.get(); }, }); diff --git a/src/main/app-paths/get-app-paths.ts b/src/main/app-paths/get-app-paths.ts deleted file mode 100644 index 00aeff540c..0000000000 --- a/src/main/app-paths/get-app-paths.ts +++ /dev/null @@ -1,15 +0,0 @@ -/** - * Copyright (c) OpenLens Authors. All rights reserved. - * Licensed under MIT License. See LICENSE in root directory for more information. - */ -import { fromPairs } from "lodash/fp"; -import type { PathName } from "../../common/app-paths/app-path-names"; -import { pathNames } from "../../common/app-paths/app-path-names"; -import type { AppPaths } from "../../common/app-paths/app-path-injection-token"; - -interface Dependencies { - getAppPath: (name: PathName) => string; -} - -export const getAppPaths = ({ getAppPath }: Dependencies) => - fromPairs(pathNames.map((name) => [name, getAppPath(name)])) as AppPaths; diff --git a/src/main/app-paths/impl.injectable.ts b/src/main/app-paths/impl.injectable.ts new file mode 100644 index 0000000000..70aeedda29 --- /dev/null +++ b/src/main/app-paths/impl.injectable.ts @@ -0,0 +1,39 @@ +/** + * Copyright (c) OpenLens Authors. All rights reserved. + * Licensed under MIT License. See LICENSE in root directory for more information. + */ +import { pathNames } from "../../common/app-paths/app-path-names"; +import { appPathsInjectionToken } from "../../common/app-paths/token"; +import { createInitializableState } from "../../common/initializable-state/create"; +import joinPathsInjectable from "../../common/path/join-paths.injectable"; +import { object } from "../../common/utils"; +import appNameInjectable from "../../common/vars/app-name.injectable"; +import directoryForIntegrationTestingInjectable from "./directory-for-integration-testing/directory-for-integration-testing.injectable"; +import getElectronAppPathInjectable from "./get-electron-app-path/get-electron-app-path.injectable"; +import setElectronAppPathInjectable from "./set-electron-app-path/set-electron-app-path.injectable"; + +const appPathsInjectable = createInitializableState({ + id: "app-paths", + init: (di) => { + const setElectronAppPath = di.inject(setElectronAppPathInjectable); + const appName = di.inject(appNameInjectable); + const getAppPath = di.inject(getElectronAppPathInjectable); + const directoryForIntegrationTesting = di.inject(directoryForIntegrationTestingInjectable); + const joinPaths = di.inject(joinPathsInjectable); + + if (directoryForIntegrationTesting) { + setElectronAppPath("appData", directoryForIntegrationTesting); + } + + const appDataPath = getAppPath("appData"); + + setElectronAppPath("userData", joinPaths(appDataPath, appName)); + + return object.fromEntries( + pathNames.map(name => [name, getAppPath(name)] as const), + ); + }, + injectionToken: appPathsInjectionToken, +}); + +export default appPathsInjectable; diff --git a/src/main/app-paths/init.injectable.ts b/src/main/app-paths/init.injectable.ts new file mode 100644 index 0000000000..c6bec4917a --- /dev/null +++ b/src/main/app-paths/init.injectable.ts @@ -0,0 +1,22 @@ +/** + * 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 "../../common/app-paths/token"; +import { beforeElectronIsReadyInjectionToken } from "../start-main-application/runnable-tokens/before-electron-is-ready-injection-token"; + +const initAppPathsInjectable = getInjectable({ + id: "init-app-paths", + instantiate: (di) => { + const appPaths = di.inject(appPathsInjectionToken); + + return { + id: "init-app-paths", + run: () => appPaths.init(), + }; + }, + injectionToken: beforeElectronIsReadyInjectionToken, +}); + +export default initAppPathsInjectable; diff --git a/src/main/app-paths/setup-app-paths.injectable.ts b/src/main/app-paths/setup-app-paths.injectable.ts deleted file mode 100644 index 842eed89c7..0000000000 --- a/src/main/app-paths/setup-app-paths.injectable.ts +++ /dev/null @@ -1,54 +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 getElectronAppPathInjectable from "./get-electron-app-path/get-electron-app-path.injectable"; -import setElectronAppPathInjectable from "./set-electron-app-path/set-electron-app-path.injectable"; -import directoryForIntegrationTestingInjectable from "./directory-for-integration-testing/directory-for-integration-testing.injectable"; -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 joinPathsInjectable from "../../common/path/join-paths.injectable"; -import { beforeElectronIsReadyInjectionToken } from "../start-main-application/runnable-tokens/before-electron-is-ready-injection-token"; -import appNameInjectable from "../../common/vars/app-name.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 directoryForIntegrationTesting = di.inject(directoryForIntegrationTestingInjectable); - const joinPaths = di.inject(joinPathsInjectable); - - return { - id: "setup-app-paths", - run: () => { - 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); - }, - }; - }, - - injectionToken: beforeElectronIsReadyInjectionToken, -}); - -export default setupAppPathsInjectable; diff --git a/src/renderer/app-paths/impl.injectable.ts b/src/renderer/app-paths/impl.injectable.ts new file mode 100644 index 0000000000..0be10da475 --- /dev/null +++ b/src/renderer/app-paths/impl.injectable.ts @@ -0,0 +1,21 @@ +/** + * Copyright (c) OpenLens Authors. All rights reserved. + * Licensed under MIT License. See LICENSE in root directory for more information. + */ + +import { appPathsChannel } from "../../common/app-paths/channel"; +import { appPathsInjectionToken } from "../../common/app-paths/token"; +import { createInitializableState } from "../../common/initializable-state/create"; +import { requestFromChannelInjectionToken } from "../../common/utils/channel/request-from-channel-injection-token"; + +const appPathsInjectable = createInitializableState({ + id: "app-paths", + init: (di) => { + const requestFromChannel = di.inject(requestFromChannelInjectionToken); + + return requestFromChannel(appPathsChannel); + }, + injectionToken: appPathsInjectionToken, +}); + +export default appPathsInjectable; diff --git a/src/renderer/app-paths/init.injectable.ts b/src/renderer/app-paths/init.injectable.ts new file mode 100644 index 0000000000..329185f7ac --- /dev/null +++ b/src/renderer/app-paths/init.injectable.ts @@ -0,0 +1,22 @@ +/** + * 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 { beforeFrameStartsInjectionToken } from "../before-frame-starts/before-frame-starts-injection-token"; +import appPathsInjectable from "./impl.injectable"; + +const initAppPathsInjectable = getInjectable({ + id: "init-app-paths", + instantiate: (di) => { + const appPaths = di.inject(appPathsInjectable); + + return { + id: "init-app-paths", + run: () => appPaths.init(), + }; + }, + injectionToken: beforeFrameStartsInjectionToken, +}); + +export default initAppPathsInjectable; diff --git a/src/renderer/app-paths/setup-app-paths.injectable.ts b/src/renderer/app-paths/setup-app-paths.injectable.ts deleted file mode 100644 index 50ad4df34a..0000000000 --- a/src/renderer/app-paths/setup-app-paths.injectable.ts +++ /dev/null @@ -1,31 +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 appPathsStateInjectable from "../../common/app-paths/app-paths-state.injectable"; -import { beforeFrameStartsInjectionToken } from "../before-frame-starts/before-frame-starts-injection-token"; -import { appPathsChannel } from "../../common/app-paths/app-paths-channel"; -import { requestFromChannelInjectionToken } from "../../common/utils/channel/request-from-channel-injection-token"; - -const setupAppPathsInjectable = getInjectable({ - id: "setup-app-paths", - - instantiate: (di) => { - const requestFromChannel = di.inject(requestFromChannelInjectionToken); - const appPathsState = di.inject(appPathsStateInjectable); - - return { - id: "setup-app-paths", - run: async () => { - const appPaths = await requestFromChannel(appPathsChannel); - - appPathsState.set(appPaths); - }, - }; - }, - - injectionToken: beforeFrameStartsInjectionToken, -}); - -export default setupAppPathsInjectable;