From e5fa99414b0c13df41c1c9cfa78c10d833945dea Mon Sep 17 00:00:00 2001 From: Sebastian Malton Date: Tue, 1 Nov 2022 09:56:24 -0400 Subject: [PATCH] Remove usages of LazyInitializableState - Internalize the initializtion of InitializableState as it is no longer needed to be exposed since the initInjectables are created together Signed-off-by: Sebastian Malton --- src/common/cluster/cluster.ts | 4 +- .../initializable-state/create-dependent.ts | 67 +++++++++---------- src/common/initializable-state/create.ts | 66 ++++++++---------- .../extension-discovery.ts | 4 +- .../extension-installer.ts | 4 +- .../file-system-provisioner-store.ts | 4 +- .../kubectl-directory-for-binaries.tsx | 4 +- .../kubectl-path-to-binary.tsx | 4 +- .../kubeconfig-sync/manager.ts | 4 +- .../kubeconfig-manager/kubeconfig-manager.ts | 4 +- src/main/kubectl/kubectl.ts | 4 +- .../local-shell-session.ts | 4 +- .../utils/create-storage/create-storage.ts | 4 +- 13 files changed, 81 insertions(+), 96 deletions(-) diff --git a/src/common/cluster/cluster.ts b/src/common/cluster/cluster.ts index 24a2004ba1..9f07345b17 100644 --- a/src/common/cluster/cluster.ts +++ b/src/common/cluster/cluster.ts @@ -25,10 +25,10 @@ import assert from "assert"; import type { Logger } from "../logger"; import type { BroadcastMessage } from "../ipc/broadcast-message.injectable"; import type { LoadConfigfromFile } from "../kube-helpers/load-config-from-file.injectable"; -import type { LazyInitializableState } from "../initializable-state/create-lazy"; +import type { InitializableState } from "../initializable-state/create"; export interface ClusterDependencies { - readonly directoryForKubeConfigs: LazyInitializableState; + readonly directoryForKubeConfigs: InitializableState; readonly logger: Logger; readonly detectorRegistry: DetectorRegistry; createKubeconfigManager: (cluster: Cluster) => KubeconfigManager; diff --git a/src/common/initializable-state/create-dependent.ts b/src/common/initializable-state/create-dependent.ts index 3010fdeca1..4c682b4500 100644 --- a/src/common/initializable-state/create-dependent.ts +++ b/src/common/initializable-state/create-dependent.ts @@ -23,53 +23,46 @@ export interface CreateDependentInitializableStateResult { export function createDependentInitializableState(args: CreateDependentInitializableStateArgs): CreateDependentInitializableStateResult { const { id, init, injectionToken, initAfter } = args; + let box: InitializableStateValue = { + set: false, + }; + let initCalled = false; + const valueInjectable = getInjectable({ id, - instantiate: (di) => { - let box: InitializableStateValue = { - set: false, - }; - let initCalled = false; + instantiate: (): InitializableState => ({ + get: () => { + if (!initCalled) { + throw new Error(`InitializableState(${id}) has not been initialized yet`); + } - return { - init: async () => { - if (initCalled) { - throw new Error(`Cannot initialize InitializableState(${id}) more than once`); - } + if (box.set === false) { + throw new Error(`InitializableState(${id}) has not finished initializing`); + } - initCalled = true; - box = { - set: true, - value: await init(di), - }; - }, - get: () => { - if (!initCalled) { - throw new Error(`InitializableState(${id}) has not been initialized yet`); - } - - if (box.set === false) { - throw new Error(`InitializableState(${id}) has not finished initializing`); - } - - return box.value; - }, - }; - }, + return box.value; + }, + }), injectionToken, }); const initializers = initAfter.map(runnableInjectable => getInjectable({ id: `initialize-${id}`, - instantiate: (di) => { - const value = di.inject(valueInjectable); + instantiate: (di) => ({ + id: `initialize-${id}`, + run: async () => { + if (initCalled) { + throw new Error(`Cannot initialize InitializableState(${id}) more than once`); + } - return { - id: `initialize-${id}`, - run: () => value.init(), - runAfter: di.inject(runnableInjectable), - }; - }, + initCalled = true; + box = { + set: true, + value: await init(di), + }; + }, + runAfter: di.inject(runnableInjectable), + }), injectionToken: runnableInjectable.injectionToken, })); diff --git a/src/common/initializable-state/create.ts b/src/common/initializable-state/create.ts index 02bc816fe2..9226b2b121 100644 --- a/src/common/initializable-state/create.ts +++ b/src/common/initializable-state/create.ts @@ -16,7 +16,6 @@ export interface CreateInitializableStateArgs { export interface InitializableState { get: () => T; - init: () => Promise; } export type InitializableStateValue = @@ -31,52 +30,45 @@ export interface CreateInitializableStateResult { export function createInitializableState(args: CreateInitializableStateArgs): CreateInitializableStateResult { const { id, init, injectionToken, when } = args; + let box: InitializableStateValue = { + set: false, + }; + let initCalled = false; + const valueInjectable = getInjectable({ id, - instantiate: (di) => { - let box: InitializableStateValue = { - set: false, - }; - let initCalled = false; + instantiate: (): InitializableState => ({ + get: () => { + if (!initCalled) { + throw new Error(`InitializableState(${id}) has not been initialized yet`); + } - return { - init: async () => { - if (initCalled) { - throw new Error(`Cannot initialize InitializableState(${id}) more than once`); - } + if (box.set === false) { + throw new Error(`InitializableState(${id}) has not finished initializing`); + } - initCalled = true; - box = { - set: true, - value: await init(di), - }; - }, - get: () => { - if (!initCalled) { - throw new Error(`InitializableState(${id}) has not been initialized yet`); - } - - if (box.set === false) { - throw new Error(`InitializableState(${id}) has not finished initializing`); - } - - return box.value; - }, - }; - }, + return box.value; + }, + }), injectionToken, }); const initializer = getInjectable({ id: `initialize-${id}`, - instantiate: (di) => { - const value = di.inject(valueInjectable); + instantiate: (di) => ({ + id: `initialize-${id}`, + run: async () => { + if (initCalled) { + throw new Error(`Cannot initialize InitializableState(${id}) more than once`); + } - return { - id: `initialize-${id}`, - run: () => value.init(), - }; - }, + initCalled = true; + box = { + set: true, + value: await init(di), + }; + }, + }), injectionToken: when, }); diff --git a/src/extensions/extension-discovery/extension-discovery.ts b/src/extensions/extension-discovery/extension-discovery.ts index 4460956729..c2d8243460 100644 --- a/src/extensions/extension-discovery/extension-discovery.ts +++ b/src/extensions/extension-discovery/extension-discovery.ts @@ -32,13 +32,13 @@ import type { GetDirnameOfPath } from "../../common/path/get-dirname.injectable" import type { GetRelativePath } from "../../common/path/get-relative-path.injectable"; import type { RemovePath } from "../../common/fs/remove-path.injectable"; import type TypedEventEmitter from "typed-emitter"; -import type { LazyInitializableState } from "../../common/initializable-state/create-lazy"; +import type { InitializableState } from "../../common/initializable-state/create"; interface Dependencies { readonly extensionLoader: ExtensionLoader; readonly extensionsStore: ExtensionsStore; readonly extensionInstallationStateStore: ExtensionInstallationStateStore; - readonly extensionPackageRootDirectory: LazyInitializableState; + readonly extensionPackageRootDirectory: InitializableState; readonly staticFilesDirectory: string; readonly logger: Logger; readonly isProduction: boolean; diff --git a/src/extensions/extension-installer/extension-installer.ts b/src/extensions/extension-installer/extension-installer.ts index 011b829681..b764f13ef8 100644 --- a/src/extensions/extension-installer/extension-installer.ts +++ b/src/extensions/extension-installer/extension-installer.ts @@ -9,12 +9,12 @@ import fs from "fs-extra"; import path from "path"; import logger from "../../main/logger"; import type { PackageJson } from "type-fest"; -import type { LazyInitializableState } from "../../common/initializable-state/create-lazy"; +import type { InitializableState } from "../../common/initializable-state/create"; const logModule = "[EXTENSION-INSTALLER]"; interface Dependencies { - readonly extensionPackageRootDirectory: LazyInitializableState; + readonly extensionPackageRootDirectory: InitializableState; } /** diff --git a/src/extensions/extension-loader/file-system-provisioner-store/file-system-provisioner-store.ts b/src/extensions/extension-loader/file-system-provisioner-store/file-system-provisioner-store.ts index 16ac84854e..74613ade86 100644 --- a/src/extensions/extension-loader/file-system-provisioner-store/file-system-provisioner-store.ts +++ b/src/extensions/extension-loader/file-system-provisioner-store/file-system-provisioner-store.ts @@ -11,14 +11,14 @@ import path from "path"; import { BaseStore } from "../../../common/base-store"; import type { LensExtensionId } from "../../lens-extension"; import { getOrInsertWith, toJS } from "../../../common/utils"; -import type { LazyInitializableState } from "../../../common/initializable-state/create-lazy"; +import type { InitializableState } from "../../../common/initializable-state/create"; interface FSProvisionModel { extensions: Record; // extension names to paths } interface Dependencies { - readonly directoryForExtensionData: LazyInitializableState; + readonly directoryForExtensionData: InitializableState; } export class FileSystemProvisionerStore extends BaseStore { diff --git a/src/features/preferences/renderer/preference-items/kubernetes/kubectl/kubectl-directory-for-binaries/kubectl-directory-for-binaries.tsx b/src/features/preferences/renderer/preference-items/kubernetes/kubectl/kubectl-directory-for-binaries/kubectl-directory-for-binaries.tsx index b81d6cb349..0569d62776 100644 --- a/src/features/preferences/renderer/preference-items/kubernetes/kubectl/kubectl-directory-for-binaries/kubectl-directory-for-binaries.tsx +++ b/src/features/preferences/renderer/preference-items/kubernetes/kubectl/kubectl-directory-for-binaries/kubectl-directory-for-binaries.tsx @@ -10,11 +10,11 @@ import userStoreInjectable from "../../../../../../../common/user-store/user-sto import { observer } from "mobx-react"; import { Input, InputValidators } from "../../../../../../../renderer/components/input"; import directoryForBinariesInjectable from "../../../../../../../common/app-paths/directory-for-binaries.injectable"; -import type { LazyInitializableState } from "../../../../../../../common/initializable-state/create-lazy"; +import type { InitializableState } from "../../../../../../../common/initializable-state/create"; interface Dependencies { userStore: UserStore; - defaultPathForGeneralBinaries: LazyInitializableState; + defaultPathForGeneralBinaries: InitializableState; } const NonInjectedKubectlDirectoryForBinaries = observer( diff --git a/src/features/preferences/renderer/preference-items/kubernetes/kubectl/kubectl-path-to-binary/kubectl-path-to-binary.tsx b/src/features/preferences/renderer/preference-items/kubernetes/kubectl/kubectl-path-to-binary/kubectl-path-to-binary.tsx index da70a65cc9..99af23265c 100644 --- a/src/features/preferences/renderer/preference-items/kubernetes/kubectl/kubectl-path-to-binary/kubectl-path-to-binary.tsx +++ b/src/features/preferences/renderer/preference-items/kubernetes/kubectl/kubectl-path-to-binary/kubectl-path-to-binary.tsx @@ -10,11 +10,11 @@ import userStoreInjectable from "../../../../../../../common/user-store/user-sto import { observer } from "mobx-react"; import { Input, InputValidators } from "../../../../../../../renderer/components/input"; import directoryForKubectlBinariesInjectable from "../../../../../../../common/app-paths/directory-for-kubectl-binaries.injectable"; -import type { LazyInitializableState } from "../../../../../../../common/initializable-state/create-lazy"; +import type { InitializableState } from "../../../../../../../common/initializable-state/create"; interface Dependencies { userStore: UserStore; - defaultPathForKubectlBinaries: LazyInitializableState; + defaultPathForKubectlBinaries: InitializableState; } const NonInjectedKubectlPathToBinary = observer( diff --git a/src/main/catalog-sources/kubeconfig-sync/manager.ts b/src/main/catalog-sources/kubeconfig-sync/manager.ts index 9fd4187a53..5ef1f5de3c 100644 --- a/src/main/catalog-sources/kubeconfig-sync/manager.ts +++ b/src/main/catalog-sources/kubeconfig-sync/manager.ts @@ -11,10 +11,10 @@ import { iter } from "../../../common/utils"; import type { KubeconfigSyncValue } from "../../../common/user-store"; import type { Logger } from "../../../common/logger"; import type { WatchKubeconfigFileChanges } from "./watch-file-changes.injectable"; -import type { LazyInitializableState } from "../../../common/initializable-state/create-lazy"; +import type { InitializableState } from "../../../common/initializable-state/create"; interface KubeconfigSyncManagerDependencies { - readonly directoryForKubeConfigs: LazyInitializableState; + readonly directoryForKubeConfigs: InitializableState; readonly logger: Logger; readonly kubeconfigSyncs: ObservableMap; watchKubeconfigFileChanges: WatchKubeconfigFileChanges; diff --git a/src/main/kubeconfig-manager/kubeconfig-manager.ts b/src/main/kubeconfig-manager/kubeconfig-manager.ts index 89a3f4a6fc..c15ebb16a9 100644 --- a/src/main/kubeconfig-manager/kubeconfig-manager.ts +++ b/src/main/kubeconfig-manager/kubeconfig-manager.ts @@ -15,10 +15,10 @@ import type { GetDirnameOfPath } from "../../common/path/get-dirname.injectable" import type { PathExists } from "../../common/fs/path-exists.injectable"; import type { DeleteFile } from "../../common/fs/delete-file.injectable"; import type { WriteFile } from "../../common/fs/write-file.injectable"; -import type { LazyInitializableState } from "../../common/initializable-state/create-lazy"; +import type { InitializableState } from "../../common/initializable-state/create"; export interface KubeconfigManagerDependencies { - readonly directoryForTemp: LazyInitializableState; + readonly directoryForTemp: InitializableState; readonly logger: Logger; readonly lensProxyPort: { get: () => number }; joinPaths: JoinPaths; diff --git a/src/main/kubectl/kubectl.ts b/src/main/kubectl/kubectl.ts index edcc288206..92ca29ea1e 100644 --- a/src/main/kubectl/kubectl.ts +++ b/src/main/kubectl/kubectl.ts @@ -17,12 +17,12 @@ import { noop } from "lodash/fp"; import type { JoinPaths } from "../../common/path/join-paths.injectable"; import type { GetDirnameOfPath } from "../../common/path/get-dirname.injectable"; import type { GetBasenameOfPath } from "../../common/path/get-basename.injectable"; -import type { LazyInitializableState } from "../../common/initializable-state/create-lazy"; +import type { InitializableState } from "../../common/initializable-state/create"; const initScriptVersionString = "# lens-initscript v3"; export interface KubectlDependencies { - readonly directoryForKubectlBinaries: LazyInitializableState; + readonly directoryForKubectlBinaries: InitializableState; readonly normalizedDownloadPlatform: "darwin" | "linux" | "windows"; readonly normalizedDownloadArch: "amd64" | "arm64" | "386"; readonly kubectlBinaryName: string; diff --git a/src/main/shell-session/local-shell-session/local-shell-session.ts b/src/main/shell-session/local-shell-session/local-shell-session.ts index 90b6bdd52d..0c80b09260 100644 --- a/src/main/shell-session/local-shell-session/local-shell-session.ts +++ b/src/main/shell-session/local-shell-session/local-shell-session.ts @@ -10,10 +10,10 @@ import type { ModifyTerminalShellEnv } from "../shell-env-modifier/modify-termin import type { JoinPaths } from "../../../common/path/join-paths.injectable"; import type { GetDirnameOfPath } from "../../../common/path/get-dirname.injectable"; import type { GetBasenameOfPath } from "../../../common/path/get-basename.injectable"; -import type { LazyInitializableState } from "../../../common/initializable-state/create-lazy"; +import type { InitializableState } from "../../../common/initializable-state/create"; export interface LocalShellSessionDependencies extends ShellSessionDependencies { - readonly directoryForBinaries: LazyInitializableState; + readonly directoryForBinaries: InitializableState; readonly userStore: UserStore; modifyTerminalShellEnv: ModifyTerminalShellEnv; joinPaths: JoinPaths; diff --git a/src/renderer/utils/create-storage/create-storage.ts b/src/renderer/utils/create-storage/create-storage.ts index 4b38ff009b..cf64eda4cf 100755 --- a/src/renderer/utils/create-storage/create-storage.ts +++ b/src/renderer/utils/create-storage/create-storage.ts @@ -11,12 +11,12 @@ import { StorageHelper } from "../storageHelper"; import type { JsonObject, JsonValue } from "type-fest"; import type { Logger } from "../../../common/logger"; import type { JoinPaths } from "../../../common/path/join-paths.injectable"; -import type { LazyInitializableState } from "../../../common/initializable-state/create-lazy"; +import type { InitializableState } from "../../../common/initializable-state/create"; interface Dependencies { storage: { initialized: boolean; loaded: boolean; data: Record }; logger: Logger; - directoryForLensLocalStorage: LazyInitializableState; + directoryForLensLocalStorage: InitializableState; readJsonFile: (filePath: string) => Promise; writeJsonFile: (filePath: string, contentObject: JsonObject) => Promise; joinPaths: JoinPaths;