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:
parent
4326c5ab86
commit
e5fa99414b
@ -25,10 +25,10 @@ import assert from "assert";
|
|||||||
import type { Logger } from "../logger";
|
import type { Logger } from "../logger";
|
||||||
import type { BroadcastMessage } from "../ipc/broadcast-message.injectable";
|
import type { BroadcastMessage } from "../ipc/broadcast-message.injectable";
|
||||||
import type { LoadConfigfromFile } from "../kube-helpers/load-config-from-file.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 {
|
export interface ClusterDependencies {
|
||||||
readonly directoryForKubeConfigs: LazyInitializableState<string>;
|
readonly directoryForKubeConfigs: InitializableState<string>;
|
||||||
readonly logger: Logger;
|
readonly logger: Logger;
|
||||||
readonly detectorRegistry: DetectorRegistry;
|
readonly detectorRegistry: DetectorRegistry;
|
||||||
createKubeconfigManager: (cluster: Cluster) => KubeconfigManager;
|
createKubeconfigManager: (cluster: Cluster) => KubeconfigManager;
|
||||||
|
|||||||
@ -23,53 +23,46 @@ export interface CreateDependentInitializableStateResult<T> {
|
|||||||
export function createDependentInitializableState<T>(args: CreateDependentInitializableStateArgs<T>): CreateDependentInitializableStateResult<T> {
|
export function createDependentInitializableState<T>(args: CreateDependentInitializableStateArgs<T>): CreateDependentInitializableStateResult<T> {
|
||||||
const { id, init, injectionToken, initAfter } = args;
|
const { id, init, injectionToken, initAfter } = args;
|
||||||
|
|
||||||
|
let box: InitializableStateValue<T> = {
|
||||||
|
set: false,
|
||||||
|
};
|
||||||
|
let initCalled = false;
|
||||||
|
|
||||||
const valueInjectable = getInjectable({
|
const valueInjectable = getInjectable({
|
||||||
id,
|
id,
|
||||||
instantiate: (di) => {
|
instantiate: (): InitializableState<T> => ({
|
||||||
let box: InitializableStateValue<T> = {
|
get: () => {
|
||||||
set: false,
|
if (!initCalled) {
|
||||||
};
|
throw new Error(`InitializableState(${id}) has not been initialized yet`);
|
||||||
let initCalled = false;
|
}
|
||||||
|
|
||||||
return {
|
if (box.set === false) {
|
||||||
init: async () => {
|
throw new Error(`InitializableState(${id}) has not finished initializing`);
|
||||||
if (initCalled) {
|
}
|
||||||
throw new Error(`Cannot initialize InitializableState(${id}) more than once`);
|
|
||||||
}
|
|
||||||
|
|
||||||
initCalled = true;
|
return box.value;
|
||||||
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;
|
|
||||||
},
|
|
||||||
};
|
|
||||||
},
|
|
||||||
injectionToken,
|
injectionToken,
|
||||||
});
|
});
|
||||||
|
|
||||||
const initializers = initAfter.map(runnableInjectable => getInjectable({
|
const initializers = initAfter.map(runnableInjectable => getInjectable({
|
||||||
id: `initialize-${id}`,
|
id: `initialize-${id}`,
|
||||||
instantiate: (di) => {
|
instantiate: (di) => ({
|
||||||
const value = di.inject(valueInjectable);
|
id: `initialize-${id}`,
|
||||||
|
run: async () => {
|
||||||
|
if (initCalled) {
|
||||||
|
throw new Error(`Cannot initialize InitializableState(${id}) more than once`);
|
||||||
|
}
|
||||||
|
|
||||||
return {
|
initCalled = true;
|
||||||
id: `initialize-${id}`,
|
box = {
|
||||||
run: () => value.init(),
|
set: true,
|
||||||
runAfter: di.inject(runnableInjectable),
|
value: await init(di),
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
|
runAfter: di.inject(runnableInjectable),
|
||||||
|
}),
|
||||||
injectionToken: runnableInjectable.injectionToken,
|
injectionToken: runnableInjectable.injectionToken,
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
|||||||
@ -16,7 +16,6 @@ export interface CreateInitializableStateArgs<T> {
|
|||||||
|
|
||||||
export interface InitializableState<T> {
|
export interface InitializableState<T> {
|
||||||
get: () => T;
|
get: () => T;
|
||||||
init: () => Promise<void>;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export type InitializableStateValue<T> =
|
export type InitializableStateValue<T> =
|
||||||
@ -31,52 +30,45 @@ export interface CreateInitializableStateResult<T> {
|
|||||||
export function createInitializableState<T>(args: CreateInitializableStateArgs<T>): CreateInitializableStateResult<T> {
|
export function createInitializableState<T>(args: CreateInitializableStateArgs<T>): CreateInitializableStateResult<T> {
|
||||||
const { id, init, injectionToken, when } = args;
|
const { id, init, injectionToken, when } = args;
|
||||||
|
|
||||||
|
let box: InitializableStateValue<T> = {
|
||||||
|
set: false,
|
||||||
|
};
|
||||||
|
let initCalled = false;
|
||||||
|
|
||||||
const valueInjectable = getInjectable({
|
const valueInjectable = getInjectable({
|
||||||
id,
|
id,
|
||||||
instantiate: (di) => {
|
instantiate: (): InitializableState<T> => ({
|
||||||
let box: InitializableStateValue<T> = {
|
get: () => {
|
||||||
set: false,
|
if (!initCalled) {
|
||||||
};
|
throw new Error(`InitializableState(${id}) has not been initialized yet`);
|
||||||
let initCalled = false;
|
}
|
||||||
|
|
||||||
return {
|
if (box.set === false) {
|
||||||
init: async () => {
|
throw new Error(`InitializableState(${id}) has not finished initializing`);
|
||||||
if (initCalled) {
|
}
|
||||||
throw new Error(`Cannot initialize InitializableState(${id}) more than once`);
|
|
||||||
}
|
|
||||||
|
|
||||||
initCalled = true;
|
return box.value;
|
||||||
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;
|
|
||||||
},
|
|
||||||
};
|
|
||||||
},
|
|
||||||
injectionToken,
|
injectionToken,
|
||||||
});
|
});
|
||||||
|
|
||||||
const initializer = getInjectable({
|
const initializer = getInjectable({
|
||||||
id: `initialize-${id}`,
|
id: `initialize-${id}`,
|
||||||
instantiate: (di) => {
|
instantiate: (di) => ({
|
||||||
const value = di.inject(valueInjectable);
|
id: `initialize-${id}`,
|
||||||
|
run: async () => {
|
||||||
|
if (initCalled) {
|
||||||
|
throw new Error(`Cannot initialize InitializableState(${id}) more than once`);
|
||||||
|
}
|
||||||
|
|
||||||
return {
|
initCalled = true;
|
||||||
id: `initialize-${id}`,
|
box = {
|
||||||
run: () => value.init(),
|
set: true,
|
||||||
};
|
value: await init(di),
|
||||||
},
|
};
|
||||||
|
},
|
||||||
|
}),
|
||||||
injectionToken: when,
|
injectionToken: when,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@ -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 { GetRelativePath } from "../../common/path/get-relative-path.injectable";
|
||||||
import type { RemovePath } from "../../common/fs/remove-path.injectable";
|
import type { RemovePath } from "../../common/fs/remove-path.injectable";
|
||||||
import type TypedEventEmitter from "typed-emitter";
|
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 {
|
interface Dependencies {
|
||||||
readonly extensionLoader: ExtensionLoader;
|
readonly extensionLoader: ExtensionLoader;
|
||||||
readonly extensionsStore: ExtensionsStore;
|
readonly extensionsStore: ExtensionsStore;
|
||||||
readonly extensionInstallationStateStore: ExtensionInstallationStateStore;
|
readonly extensionInstallationStateStore: ExtensionInstallationStateStore;
|
||||||
readonly extensionPackageRootDirectory: LazyInitializableState<string>;
|
readonly extensionPackageRootDirectory: InitializableState<string>;
|
||||||
readonly staticFilesDirectory: string;
|
readonly staticFilesDirectory: string;
|
||||||
readonly logger: Logger;
|
readonly logger: Logger;
|
||||||
readonly isProduction: boolean;
|
readonly isProduction: boolean;
|
||||||
|
|||||||
@ -9,12 +9,12 @@ import fs from "fs-extra";
|
|||||||
import path from "path";
|
import path from "path";
|
||||||
import logger from "../../main/logger";
|
import logger from "../../main/logger";
|
||||||
import type { PackageJson } from "type-fest";
|
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]";
|
const logModule = "[EXTENSION-INSTALLER]";
|
||||||
|
|
||||||
interface Dependencies {
|
interface Dependencies {
|
||||||
readonly extensionPackageRootDirectory: LazyInitializableState<string>;
|
readonly extensionPackageRootDirectory: InitializableState<string>;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@ -11,14 +11,14 @@ import path from "path";
|
|||||||
import { BaseStore } from "../../../common/base-store";
|
import { BaseStore } from "../../../common/base-store";
|
||||||
import type { LensExtensionId } from "../../lens-extension";
|
import type { LensExtensionId } from "../../lens-extension";
|
||||||
import { getOrInsertWith, toJS } from "../../../common/utils";
|
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 {
|
interface FSProvisionModel {
|
||||||
extensions: Record<string, string>; // extension names to paths
|
extensions: Record<string, string>; // extension names to paths
|
||||||
}
|
}
|
||||||
|
|
||||||
interface Dependencies {
|
interface Dependencies {
|
||||||
readonly directoryForExtensionData: LazyInitializableState<string>;
|
readonly directoryForExtensionData: InitializableState<string>;
|
||||||
}
|
}
|
||||||
|
|
||||||
export class FileSystemProvisionerStore extends BaseStore<FSProvisionModel> {
|
export class FileSystemProvisionerStore extends BaseStore<FSProvisionModel> {
|
||||||
|
|||||||
@ -10,11 +10,11 @@ import userStoreInjectable from "../../../../../../../common/user-store/user-sto
|
|||||||
import { observer } from "mobx-react";
|
import { observer } from "mobx-react";
|
||||||
import { Input, InputValidators } from "../../../../../../../renderer/components/input";
|
import { Input, InputValidators } from "../../../../../../../renderer/components/input";
|
||||||
import directoryForBinariesInjectable from "../../../../../../../common/app-paths/directory-for-binaries.injectable";
|
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 {
|
interface Dependencies {
|
||||||
userStore: UserStore;
|
userStore: UserStore;
|
||||||
defaultPathForGeneralBinaries: LazyInitializableState<string>;
|
defaultPathForGeneralBinaries: InitializableState<string>;
|
||||||
}
|
}
|
||||||
|
|
||||||
const NonInjectedKubectlDirectoryForBinaries = observer(
|
const NonInjectedKubectlDirectoryForBinaries = observer(
|
||||||
|
|||||||
@ -10,11 +10,11 @@ import userStoreInjectable from "../../../../../../../common/user-store/user-sto
|
|||||||
import { observer } from "mobx-react";
|
import { observer } from "mobx-react";
|
||||||
import { Input, InputValidators } from "../../../../../../../renderer/components/input";
|
import { Input, InputValidators } from "../../../../../../../renderer/components/input";
|
||||||
import directoryForKubectlBinariesInjectable from "../../../../../../../common/app-paths/directory-for-kubectl-binaries.injectable";
|
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 {
|
interface Dependencies {
|
||||||
userStore: UserStore;
|
userStore: UserStore;
|
||||||
defaultPathForKubectlBinaries: LazyInitializableState<string>;
|
defaultPathForKubectlBinaries: InitializableState<string>;
|
||||||
}
|
}
|
||||||
|
|
||||||
const NonInjectedKubectlPathToBinary = observer(
|
const NonInjectedKubectlPathToBinary = observer(
|
||||||
|
|||||||
@ -11,10 +11,10 @@ import { iter } from "../../../common/utils";
|
|||||||
import type { KubeconfigSyncValue } from "../../../common/user-store";
|
import type { KubeconfigSyncValue } from "../../../common/user-store";
|
||||||
import type { Logger } from "../../../common/logger";
|
import type { Logger } from "../../../common/logger";
|
||||||
import type { WatchKubeconfigFileChanges } from "./watch-file-changes.injectable";
|
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 {
|
interface KubeconfigSyncManagerDependencies {
|
||||||
readonly directoryForKubeConfigs: LazyInitializableState<string>;
|
readonly directoryForKubeConfigs: InitializableState<string>;
|
||||||
readonly logger: Logger;
|
readonly logger: Logger;
|
||||||
readonly kubeconfigSyncs: ObservableMap<string, KubeconfigSyncValue>;
|
readonly kubeconfigSyncs: ObservableMap<string, KubeconfigSyncValue>;
|
||||||
watchKubeconfigFileChanges: WatchKubeconfigFileChanges;
|
watchKubeconfigFileChanges: WatchKubeconfigFileChanges;
|
||||||
|
|||||||
@ -15,10 +15,10 @@ import type { GetDirnameOfPath } from "../../common/path/get-dirname.injectable"
|
|||||||
import type { PathExists } from "../../common/fs/path-exists.injectable";
|
import type { PathExists } from "../../common/fs/path-exists.injectable";
|
||||||
import type { DeleteFile } from "../../common/fs/delete-file.injectable";
|
import type { DeleteFile } from "../../common/fs/delete-file.injectable";
|
||||||
import type { WriteFile } from "../../common/fs/write-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 {
|
export interface KubeconfigManagerDependencies {
|
||||||
readonly directoryForTemp: LazyInitializableState<string>;
|
readonly directoryForTemp: InitializableState<string>;
|
||||||
readonly logger: Logger;
|
readonly logger: Logger;
|
||||||
readonly lensProxyPort: { get: () => number };
|
readonly lensProxyPort: { get: () => number };
|
||||||
joinPaths: JoinPaths;
|
joinPaths: JoinPaths;
|
||||||
|
|||||||
@ -17,12 +17,12 @@ import { noop } from "lodash/fp";
|
|||||||
import type { JoinPaths } from "../../common/path/join-paths.injectable";
|
import type { JoinPaths } from "../../common/path/join-paths.injectable";
|
||||||
import type { GetDirnameOfPath } from "../../common/path/get-dirname.injectable";
|
import type { GetDirnameOfPath } from "../../common/path/get-dirname.injectable";
|
||||||
import type { GetBasenameOfPath } from "../../common/path/get-basename.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";
|
const initScriptVersionString = "# lens-initscript v3";
|
||||||
|
|
||||||
export interface KubectlDependencies {
|
export interface KubectlDependencies {
|
||||||
readonly directoryForKubectlBinaries: LazyInitializableState<string>;
|
readonly directoryForKubectlBinaries: InitializableState<string>;
|
||||||
readonly normalizedDownloadPlatform: "darwin" | "linux" | "windows";
|
readonly normalizedDownloadPlatform: "darwin" | "linux" | "windows";
|
||||||
readonly normalizedDownloadArch: "amd64" | "arm64" | "386";
|
readonly normalizedDownloadArch: "amd64" | "arm64" | "386";
|
||||||
readonly kubectlBinaryName: string;
|
readonly kubectlBinaryName: string;
|
||||||
|
|||||||
@ -10,10 +10,10 @@ import type { ModifyTerminalShellEnv } from "../shell-env-modifier/modify-termin
|
|||||||
import type { JoinPaths } from "../../../common/path/join-paths.injectable";
|
import type { JoinPaths } from "../../../common/path/join-paths.injectable";
|
||||||
import type { GetDirnameOfPath } from "../../../common/path/get-dirname.injectable";
|
import type { GetDirnameOfPath } from "../../../common/path/get-dirname.injectable";
|
||||||
import type { GetBasenameOfPath } from "../../../common/path/get-basename.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 {
|
export interface LocalShellSessionDependencies extends ShellSessionDependencies {
|
||||||
readonly directoryForBinaries: LazyInitializableState<string>;
|
readonly directoryForBinaries: InitializableState<string>;
|
||||||
readonly userStore: UserStore;
|
readonly userStore: UserStore;
|
||||||
modifyTerminalShellEnv: ModifyTerminalShellEnv;
|
modifyTerminalShellEnv: ModifyTerminalShellEnv;
|
||||||
joinPaths: JoinPaths;
|
joinPaths: JoinPaths;
|
||||||
|
|||||||
@ -11,12 +11,12 @@ import { StorageHelper } from "../storageHelper";
|
|||||||
import type { JsonObject, JsonValue } from "type-fest";
|
import type { JsonObject, JsonValue } from "type-fest";
|
||||||
import type { Logger } from "../../../common/logger";
|
import type { Logger } from "../../../common/logger";
|
||||||
import type { JoinPaths } from "../../../common/path/join-paths.injectable";
|
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 {
|
interface Dependencies {
|
||||||
storage: { initialized: boolean; loaded: boolean; data: Record<string, any> };
|
storage: { initialized: boolean; loaded: boolean; data: Record<string, any> };
|
||||||
logger: Logger;
|
logger: Logger;
|
||||||
directoryForLensLocalStorage: LazyInitializableState<string>;
|
directoryForLensLocalStorage: InitializableState<string>;
|
||||||
readJsonFile: (filePath: string) => Promise<JsonValue>;
|
readJsonFile: (filePath: string) => Promise<JsonValue>;
|
||||||
writeJsonFile: (filePath: string, contentObject: JsonObject) => Promise<void>;
|
writeJsonFile: (filePath: string, contentObject: JsonObject) => Promise<void>;
|
||||||
joinPaths: JoinPaths;
|
joinPaths: JoinPaths;
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user