From 89ada69cf465094389780eb2377a92255e9f5872 Mon Sep 17 00:00:00 2001 From: Sebastian Malton Date: Tue, 1 Nov 2022 09:09:58 -0400 Subject: [PATCH] Introduce creating initializer injectables for InitializableState - Use this instead of the explicit init files for AppPaths and BuildVersion Signed-off-by: Sebastian Malton --- src/common/initializable-state/create.test.ts | 5 +-- src/common/initializable-state/create.ts | 33 ++++++++++++++++--- src/main/app-paths/impl.injectable.ts | 9 ++++- src/main/app-paths/init.injectable.ts | 22 ------------- .../build-version/build-version.injectable.ts | 9 ++++- .../vars/build-version/init.injectable.ts | 22 ------------- src/renderer/app-paths/impl.injectable.ts | 9 ++++- src/renderer/app-paths/init.injectable.ts | 22 ------------- src/renderer/vars/build-version.injectable.ts | 26 +++++++++++++++ .../build-version/build-version.injectable.ts | 19 ----------- .../vars/build-version/init.injectable.ts | 22 ------------- 11 files changed, 82 insertions(+), 116 deletions(-) delete mode 100644 src/main/app-paths/init.injectable.ts delete mode 100644 src/main/vars/build-version/init.injectable.ts delete mode 100644 src/renderer/app-paths/init.injectable.ts create mode 100644 src/renderer/vars/build-version.injectable.ts delete mode 100644 src/renderer/vars/build-version/build-version.injectable.ts delete mode 100644 src/renderer/vars/build-version/init.injectable.ts diff --git a/src/common/initializable-state/create.test.ts b/src/common/initializable-state/create.test.ts index 38980b0a41..4f5fd397cb 100644 --- a/src/common/initializable-state/create.test.ts +++ b/src/common/initializable-state/create.test.ts @@ -24,10 +24,11 @@ describe("InitializableState tests", () => { beforeEach(() => { initMock = asyncFn(); - stateInjectable = createInitializableState({ + ({ value: stateInjectable } = createInitializableState({ id: "my-state", init: initMock, - }); + when: null as any, + })); runInAction(() => { di.register(stateInjectable); diff --git a/src/common/initializable-state/create.ts b/src/common/initializable-state/create.ts index 1016680272..02bc816fe2 100644 --- a/src/common/initializable-state/create.ts +++ b/src/common/initializable-state/create.ts @@ -5,11 +5,13 @@ import type { DiContainerForInjection, Injectable, InjectionToken } from "@ogre-tools/injectable"; import { getInjectable } from "@ogre-tools/injectable"; +import type { Runnable } from "../runnable/run-many-for"; export interface CreateInitializableStateArgs { id: string; init: (di: DiContainerForInjection) => Promise | T; injectionToken?: InjectionToken, void>; + when: InjectionToken, void>; } export interface InitializableState { @@ -19,12 +21,17 @@ export interface InitializableState { export type InitializableStateValue = | { set: false } - | { set: true; value: T } ; + | { set: true; value: T }; -export function createInitializableState(args: CreateInitializableStateArgs): Injectable, unknown, void> { - const { id, init, injectionToken } = args; +export interface CreateInitializableStateResult { + value: Injectable, unknown, void>; + initializer: Injectable, Runnable, void>; +} - return getInjectable({ +export function createInitializableState(args: CreateInitializableStateArgs): CreateInitializableStateResult { + const { id, init, injectionToken, when } = args; + + const valueInjectable = getInjectable({ id, instantiate: (di) => { let box: InitializableStateValue = { @@ -59,4 +66,22 @@ export function createInitializableState(args: CreateInitializableStateArgs { + const value = di.inject(valueInjectable); + + return { + id: `initialize-${id}`, + run: () => value.init(), + }; + }, + injectionToken: when, + }); + + return { + value: valueInjectable, + initializer, + }; } diff --git a/src/main/app-paths/impl.injectable.ts b/src/main/app-paths/impl.injectable.ts index 70aeedda29..a16c334be2 100644 --- a/src/main/app-paths/impl.injectable.ts +++ b/src/main/app-paths/impl.injectable.ts @@ -8,11 +8,15 @@ import { createInitializableState } from "../../common/initializable-state/creat import joinPathsInjectable from "../../common/path/join-paths.injectable"; import { object } from "../../common/utils"; import appNameInjectable from "../../common/vars/app-name.injectable"; +import { beforeElectronIsReadyInjectionToken } from "../start-main-application/runnable-tokens/before-electron-is-ready-injection-token"; 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({ +const { + value: appPathsInjectable, + initializer: initAppPathsInjectable, +} = createInitializableState({ id: "app-paths", init: (di) => { const setElectronAppPath = di.inject(setElectronAppPathInjectable); @@ -34,6 +38,9 @@ const appPathsInjectable = createInitializableState({ ); }, injectionToken: appPathsInjectionToken, + when: beforeElectronIsReadyInjectionToken, }); +export { initAppPathsInjectable }; + export default appPathsInjectable; diff --git a/src/main/app-paths/init.injectable.ts b/src/main/app-paths/init.injectable.ts deleted file mode 100644 index c6bec4917a..0000000000 --- a/src/main/app-paths/init.injectable.ts +++ /dev/null @@ -1,22 +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 "../../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/vars/build-version/build-version.injectable.ts b/src/main/vars/build-version/build-version.injectable.ts index 5e560282b9..47a71a6f8c 100644 --- a/src/main/vars/build-version/build-version.injectable.ts +++ b/src/main/vars/build-version/build-version.injectable.ts @@ -4,9 +4,13 @@ */ import { createInitializableState } from "../../../common/initializable-state/create"; import { buildVersionInjectionToken } from "../../../common/vars/build-semantic-version.injectable"; +import { beforeApplicationIsLoadingInjectionToken } from "../../start-main-application/runnable-tokens/before-application-is-loading-injection-token"; import getBuildVersionInjectable from "./get-build-version.injectable"; -const buildVersionInjectable = createInitializableState({ +const { + value: buildVersionInjectable, + initializer: initializeBuildVersionInjectable, +} = createInitializableState({ id: "build-version", init: (di) => { const getBuildVersion = di.inject(getBuildVersionInjectable); @@ -14,6 +18,9 @@ const buildVersionInjectable = createInitializableState({ return getBuildVersion(); }, injectionToken: buildVersionInjectionToken, + when: beforeApplicationIsLoadingInjectionToken, }); +export { initializeBuildVersionInjectable }; + export default buildVersionInjectable; diff --git a/src/main/vars/build-version/init.injectable.ts b/src/main/vars/build-version/init.injectable.ts deleted file mode 100644 index f53e735a47..0000000000 --- a/src/main/vars/build-version/init.injectable.ts +++ /dev/null @@ -1,22 +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 { beforeApplicationIsLoadingInjectionToken } from "../../start-main-application/runnable-tokens/before-application-is-loading-injection-token"; -import buildVersionInjectable from "./build-version.injectable"; - -const initializeBuildVersionInjectable = getInjectable({ - id: "initialize-build-version", - instantiate: (di) => { - const buildVersion = di.inject(buildVersionInjectable); - - return { - id: "initialize-build-version", - run: () => buildVersion.init(), - }; - }, - injectionToken: beforeApplicationIsLoadingInjectionToken, -}); - -export default initializeBuildVersionInjectable; diff --git a/src/renderer/app-paths/impl.injectable.ts b/src/renderer/app-paths/impl.injectable.ts index 0be10da475..bef082b847 100644 --- a/src/renderer/app-paths/impl.injectable.ts +++ b/src/renderer/app-paths/impl.injectable.ts @@ -7,8 +7,12 @@ 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"; +import { beforeFrameStartsInjectionToken } from "../before-frame-starts/before-frame-starts-injection-token"; -const appPathsInjectable = createInitializableState({ +const { + value: appPathsInjectable, + initializer: initAppPathsInjectable, +} = createInitializableState({ id: "app-paths", init: (di) => { const requestFromChannel = di.inject(requestFromChannelInjectionToken); @@ -16,6 +20,9 @@ const appPathsInjectable = createInitializableState({ return requestFromChannel(appPathsChannel); }, injectionToken: appPathsInjectionToken, + when: beforeFrameStartsInjectionToken, }); +export { initAppPathsInjectable }; + export default appPathsInjectable; diff --git a/src/renderer/app-paths/init.injectable.ts b/src/renderer/app-paths/init.injectable.ts deleted file mode 100644 index 329185f7ac..0000000000 --- a/src/renderer/app-paths/init.injectable.ts +++ /dev/null @@ -1,22 +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 { 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/vars/build-version.injectable.ts b/src/renderer/vars/build-version.injectable.ts new file mode 100644 index 0000000000..cc45587f52 --- /dev/null +++ b/src/renderer/vars/build-version.injectable.ts @@ -0,0 +1,26 @@ +/** + * Copyright (c) OpenLens Authors. All rights reserved. + * Licensed under MIT License. See LICENSE in root directory for more information. + */ +import { createInitializableState } from "../../common/initializable-state/create"; +import { requestFromChannelInjectionToken } from "../../common/utils/channel/request-from-channel-injection-token"; +import { buildVersionChannel, buildVersionInjectionToken } from "../../common/vars/build-semantic-version.injectable"; +import { beforeFrameStartsInjectionToken } from "../before-frame-starts/before-frame-starts-injection-token"; + +const { + value: buildVersionInjectable, + initializer: initializeBuildVersionInjectable, +} = createInitializableState({ + id: "build-version", + init: (di) => { + const requestFromChannel = di.inject(requestFromChannelInjectionToken); + + return requestFromChannel(buildVersionChannel); + }, + injectionToken: buildVersionInjectionToken, + when: beforeFrameStartsInjectionToken, +}); + +export { initializeBuildVersionInjectable }; + +export default buildVersionInjectable; diff --git a/src/renderer/vars/build-version/build-version.injectable.ts b/src/renderer/vars/build-version/build-version.injectable.ts deleted file mode 100644 index a63a4102b0..0000000000 --- a/src/renderer/vars/build-version/build-version.injectable.ts +++ /dev/null @@ -1,19 +0,0 @@ -/** - * Copyright (c) OpenLens Authors. All rights reserved. - * Licensed under MIT License. See LICENSE in root directory for more information. - */ -import { createInitializableState } from "../../../common/initializable-state/create"; -import { requestFromChannelInjectionToken } from "../../../common/utils/channel/request-from-channel-injection-token"; -import { buildVersionChannel, buildVersionInjectionToken } from "../../../common/vars/build-semantic-version.injectable"; - -const buildVersionInjectable = createInitializableState({ - id: "build-version", - init: (di) => { - const requestFromChannel = di.inject(requestFromChannelInjectionToken); - - return requestFromChannel(buildVersionChannel); - }, - injectionToken: buildVersionInjectionToken, -}); - -export default buildVersionInjectable; diff --git a/src/renderer/vars/build-version/init.injectable.ts b/src/renderer/vars/build-version/init.injectable.ts deleted file mode 100644 index 25aeb8d2bc..0000000000 --- a/src/renderer/vars/build-version/init.injectable.ts +++ /dev/null @@ -1,22 +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 { beforeFrameStartsInjectionToken } from "../../before-frame-starts/before-frame-starts-injection-token"; -import buildVersionInjectable from "./build-version.injectable"; - -const initializeBuildVersionInjectable = getInjectable({ - id: "initialize-build-version", - instantiate: (di) => { - const buildVersion = di.inject(buildVersionInjectable); - - return { - id: "initialize-build-version", - run: () => buildVersion.init(), - }; - }, - injectionToken: beforeFrameStartsInjectionToken, -}); - -export default initializeBuildVersionInjectable;