mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
Introduce creating initializer injectables for InitializableState
- Use this instead of the explicit init files for AppPaths and BuildVersion Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
parent
d539cf9d37
commit
89ada69cf4
@ -24,10 +24,11 @@ describe("InitializableState tests", () => {
|
|||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
initMock = asyncFn();
|
initMock = asyncFn();
|
||||||
stateInjectable = createInitializableState({
|
({ value: stateInjectable } = createInitializableState({
|
||||||
id: "my-state",
|
id: "my-state",
|
||||||
init: initMock,
|
init: initMock,
|
||||||
});
|
when: null as any,
|
||||||
|
}));
|
||||||
|
|
||||||
runInAction(() => {
|
runInAction(() => {
|
||||||
di.register(stateInjectable);
|
di.register(stateInjectable);
|
||||||
|
|||||||
@ -5,11 +5,13 @@
|
|||||||
|
|
||||||
import type { DiContainerForInjection, Injectable, InjectionToken } from "@ogre-tools/injectable";
|
import type { DiContainerForInjection, Injectable, InjectionToken } from "@ogre-tools/injectable";
|
||||||
import { getInjectable } from "@ogre-tools/injectable";
|
import { getInjectable } from "@ogre-tools/injectable";
|
||||||
|
import type { Runnable } from "../runnable/run-many-for";
|
||||||
|
|
||||||
export interface CreateInitializableStateArgs<T> {
|
export interface CreateInitializableStateArgs<T> {
|
||||||
id: string;
|
id: string;
|
||||||
init: (di: DiContainerForInjection) => Promise<T> | T;
|
init: (di: DiContainerForInjection) => Promise<T> | T;
|
||||||
injectionToken?: InjectionToken<InitializableState<T>, void>;
|
injectionToken?: InjectionToken<InitializableState<T>, void>;
|
||||||
|
when: InjectionToken<Runnable<void>, void>;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface InitializableState<T> {
|
export interface InitializableState<T> {
|
||||||
@ -21,10 +23,15 @@ export type InitializableStateValue<T> =
|
|||||||
| { set: false }
|
| { set: false }
|
||||||
| { set: true; value: T };
|
| { set: true; value: T };
|
||||||
|
|
||||||
export function createInitializableState<T>(args: CreateInitializableStateArgs<T>): Injectable<InitializableState<T>, unknown, void> {
|
export interface CreateInitializableStateResult<T> {
|
||||||
const { id, init, injectionToken } = args;
|
value: Injectable<InitializableState<T>, unknown, void>;
|
||||||
|
initializer: Injectable<Runnable<void>, Runnable<void>, void>;
|
||||||
|
}
|
||||||
|
|
||||||
return getInjectable({
|
export function createInitializableState<T>(args: CreateInitializableStateArgs<T>): CreateInitializableStateResult<T> {
|
||||||
|
const { id, init, injectionToken, when } = args;
|
||||||
|
|
||||||
|
const valueInjectable = getInjectable({
|
||||||
id,
|
id,
|
||||||
instantiate: (di) => {
|
instantiate: (di) => {
|
||||||
let box: InitializableStateValue<T> = {
|
let box: InitializableStateValue<T> = {
|
||||||
@ -59,4 +66,22 @@ export function createInitializableState<T>(args: CreateInitializableStateArgs<T
|
|||||||
},
|
},
|
||||||
injectionToken,
|
injectionToken,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const initializer = getInjectable({
|
||||||
|
id: `initialize-${id}`,
|
||||||
|
instantiate: (di) => {
|
||||||
|
const value = di.inject(valueInjectable);
|
||||||
|
|
||||||
|
return {
|
||||||
|
id: `initialize-${id}`,
|
||||||
|
run: () => value.init(),
|
||||||
|
};
|
||||||
|
},
|
||||||
|
injectionToken: when,
|
||||||
|
});
|
||||||
|
|
||||||
|
return {
|
||||||
|
value: valueInjectable,
|
||||||
|
initializer,
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@ -8,11 +8,15 @@ import { createInitializableState } from "../../common/initializable-state/creat
|
|||||||
import joinPathsInjectable from "../../common/path/join-paths.injectable";
|
import joinPathsInjectable from "../../common/path/join-paths.injectable";
|
||||||
import { object } from "../../common/utils";
|
import { object } from "../../common/utils";
|
||||||
import appNameInjectable from "../../common/vars/app-name.injectable";
|
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 directoryForIntegrationTestingInjectable from "./directory-for-integration-testing/directory-for-integration-testing.injectable";
|
||||||
import getElectronAppPathInjectable from "./get-electron-app-path/get-electron-app-path.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";
|
import setElectronAppPathInjectable from "./set-electron-app-path/set-electron-app-path.injectable";
|
||||||
|
|
||||||
const appPathsInjectable = createInitializableState({
|
const {
|
||||||
|
value: appPathsInjectable,
|
||||||
|
initializer: initAppPathsInjectable,
|
||||||
|
} = createInitializableState({
|
||||||
id: "app-paths",
|
id: "app-paths",
|
||||||
init: (di) => {
|
init: (di) => {
|
||||||
const setElectronAppPath = di.inject(setElectronAppPathInjectable);
|
const setElectronAppPath = di.inject(setElectronAppPathInjectable);
|
||||||
@ -34,6 +38,9 @@ const appPathsInjectable = createInitializableState({
|
|||||||
);
|
);
|
||||||
},
|
},
|
||||||
injectionToken: appPathsInjectionToken,
|
injectionToken: appPathsInjectionToken,
|
||||||
|
when: beforeElectronIsReadyInjectionToken,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
export { initAppPathsInjectable };
|
||||||
|
|
||||||
export default appPathsInjectable;
|
export default appPathsInjectable;
|
||||||
|
|||||||
@ -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;
|
|
||||||
@ -4,9 +4,13 @@
|
|||||||
*/
|
*/
|
||||||
import { createInitializableState } from "../../../common/initializable-state/create";
|
import { createInitializableState } from "../../../common/initializable-state/create";
|
||||||
import { buildVersionInjectionToken } from "../../../common/vars/build-semantic-version.injectable";
|
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";
|
import getBuildVersionInjectable from "./get-build-version.injectable";
|
||||||
|
|
||||||
const buildVersionInjectable = createInitializableState({
|
const {
|
||||||
|
value: buildVersionInjectable,
|
||||||
|
initializer: initializeBuildVersionInjectable,
|
||||||
|
} = createInitializableState({
|
||||||
id: "build-version",
|
id: "build-version",
|
||||||
init: (di) => {
|
init: (di) => {
|
||||||
const getBuildVersion = di.inject(getBuildVersionInjectable);
|
const getBuildVersion = di.inject(getBuildVersionInjectable);
|
||||||
@ -14,6 +18,9 @@ const buildVersionInjectable = createInitializableState({
|
|||||||
return getBuildVersion();
|
return getBuildVersion();
|
||||||
},
|
},
|
||||||
injectionToken: buildVersionInjectionToken,
|
injectionToken: buildVersionInjectionToken,
|
||||||
|
when: beforeApplicationIsLoadingInjectionToken,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
export { initializeBuildVersionInjectable };
|
||||||
|
|
||||||
export default buildVersionInjectable;
|
export default buildVersionInjectable;
|
||||||
|
|||||||
@ -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;
|
|
||||||
@ -7,8 +7,12 @@ import { appPathsChannel } from "../../common/app-paths/channel";
|
|||||||
import { appPathsInjectionToken } from "../../common/app-paths/token";
|
import { appPathsInjectionToken } from "../../common/app-paths/token";
|
||||||
import { createInitializableState } from "../../common/initializable-state/create";
|
import { createInitializableState } from "../../common/initializable-state/create";
|
||||||
import { requestFromChannelInjectionToken } from "../../common/utils/channel/request-from-channel-injection-token";
|
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",
|
id: "app-paths",
|
||||||
init: (di) => {
|
init: (di) => {
|
||||||
const requestFromChannel = di.inject(requestFromChannelInjectionToken);
|
const requestFromChannel = di.inject(requestFromChannelInjectionToken);
|
||||||
@ -16,6 +20,9 @@ const appPathsInjectable = createInitializableState({
|
|||||||
return requestFromChannel(appPathsChannel);
|
return requestFromChannel(appPathsChannel);
|
||||||
},
|
},
|
||||||
injectionToken: appPathsInjectionToken,
|
injectionToken: appPathsInjectionToken,
|
||||||
|
when: beforeFrameStartsInjectionToken,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
export { initAppPathsInjectable };
|
||||||
|
|
||||||
export default appPathsInjectable;
|
export default appPathsInjectable;
|
||||||
|
|||||||
@ -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;
|
|
||||||
26
src/renderer/vars/build-version.injectable.ts
Normal file
26
src/renderer/vars/build-version.injectable.ts
Normal file
@ -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;
|
||||||
@ -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;
|
|
||||||
@ -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;
|
|
||||||
Loading…
Reference in New Issue
Block a user