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

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 <sebastian@malton.name>
This commit is contained in:
Sebastian Malton 2022-11-01 09:56:24 -04:00
parent 4326c5ab86
commit e5fa99414b
13 changed files with 81 additions and 96 deletions

View File

@ -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<string>;
readonly directoryForKubeConfigs: InitializableState<string>;
readonly logger: Logger;
readonly detectorRegistry: DetectorRegistry;
createKubeconfigManager: (cluster: Cluster) => KubeconfigManager;

View File

@ -23,53 +23,46 @@ export interface CreateDependentInitializableStateResult<T> {
export function createDependentInitializableState<T>(args: CreateDependentInitializableStateArgs<T>): CreateDependentInitializableStateResult<T> {
const { id, init, injectionToken, initAfter } = args;
let box: InitializableStateValue<T> = {
set: false,
};
let initCalled = false;
const valueInjectable = getInjectable({
id,
instantiate: (di) => {
let box: InitializableStateValue<T> = {
set: false,
};
let initCalled = false;
instantiate: (): InitializableState<T> => ({
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,
}));

View File

@ -16,7 +16,6 @@ export interface CreateInitializableStateArgs<T> {
export interface InitializableState<T> {
get: () => T;
init: () => Promise<void>;
}
export type InitializableStateValue<T> =
@ -31,52 +30,45 @@ export interface CreateInitializableStateResult<T> {
export function createInitializableState<T>(args: CreateInitializableStateArgs<T>): CreateInitializableStateResult<T> {
const { id, init, injectionToken, when } = args;
let box: InitializableStateValue<T> = {
set: false,
};
let initCalled = false;
const valueInjectable = getInjectable({
id,
instantiate: (di) => {
let box: InitializableStateValue<T> = {
set: false,
};
let initCalled = false;
instantiate: (): InitializableState<T> => ({
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,
});

View File

@ -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<string>;
readonly extensionPackageRootDirectory: InitializableState<string>;
readonly staticFilesDirectory: string;
readonly logger: Logger;
readonly isProduction: boolean;

View File

@ -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<string>;
readonly extensionPackageRootDirectory: InitializableState<string>;
}
/**

View File

@ -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<string, string>; // extension names to paths
}
interface Dependencies {
readonly directoryForExtensionData: LazyInitializableState<string>;
readonly directoryForExtensionData: InitializableState<string>;
}
export class FileSystemProvisionerStore extends BaseStore<FSProvisionModel> {

View File

@ -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<string>;
defaultPathForGeneralBinaries: InitializableState<string>;
}
const NonInjectedKubectlDirectoryForBinaries = observer(

View File

@ -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<string>;
defaultPathForKubectlBinaries: InitializableState<string>;
}
const NonInjectedKubectlPathToBinary = observer(

View File

@ -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<string>;
readonly directoryForKubeConfigs: InitializableState<string>;
readonly logger: Logger;
readonly kubeconfigSyncs: ObservableMap<string, KubeconfigSyncValue>;
watchKubeconfigFileChanges: WatchKubeconfigFileChanges;

View File

@ -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<string>;
readonly directoryForTemp: InitializableState<string>;
readonly logger: Logger;
readonly lensProxyPort: { get: () => number };
joinPaths: JoinPaths;

View File

@ -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<string>;
readonly directoryForKubectlBinaries: InitializableState<string>;
readonly normalizedDownloadPlatform: "darwin" | "linux" | "windows";
readonly normalizedDownloadArch: "amd64" | "arm64" | "386";
readonly kubectlBinaryName: string;

View File

@ -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<string>;
readonly directoryForBinaries: InitializableState<string>;
readonly userStore: UserStore;
modifyTerminalShellEnv: ModifyTerminalShellEnv;
joinPaths: JoinPaths;

View File

@ -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<string, any> };
logger: Logger;
directoryForLensLocalStorage: LazyInitializableState<string>;
directoryForLensLocalStorage: InitializableState<string>;
readJsonFile: (filePath: string) => Promise<JsonValue>;
writeJsonFile: (filePath: string, contentObject: JsonObject) => Promise<void>;
joinPaths: JoinPaths;