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

Fix type errors

Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
Sebastian Malton 2023-03-21 14:42:58 -04:00
parent cb538bf716
commit b5178c7cdf
5 changed files with 35 additions and 29 deletions

View File

@ -6,7 +6,6 @@ import { disposer, isPromiseLike } from "@k8slens/utilities";
import { getInjectable } from "@ogre-tools/injectable"; import { getInjectable } from "@ogre-tools/injectable";
import type Conf from "conf/dist/source"; import type Conf from "conf/dist/source";
import type { Options } from "conf/dist/source"; import type { Options } from "conf/dist/source";
import type { Migrations } from "conf/dist/source/types";
import { isEqual, kebabCase } from "lodash"; import { isEqual, kebabCase } from "lodash";
import type { IEqualsComparer } from "mobx"; import type { IEqualsComparer } from "mobx";
import { reaction } from "mobx"; import { reaction } from "mobx";
@ -20,6 +19,7 @@ import type { MessageChannel } from "@k8slens/messaging";
import { persistentStorageIpcChannelPrefixesInjectionToken } from "./channel-prefix"; import { persistentStorageIpcChannelPrefixesInjectionToken } from "./channel-prefix";
import { shouldPersistentStorageDisableSyncInIpcListenerInjectionToken } from "./disable-sync"; import { shouldPersistentStorageDisableSyncInIpcListenerInjectionToken } from "./disable-sync";
import { persistStateToConfigInjectionToken } from "./save-to-file"; import { persistStateToConfigInjectionToken } from "./save-to-file";
import type { Migrations } from "./migrations.injectable";
export interface PersistentStorage { export interface PersistentStorage {
/** /**
@ -30,13 +30,12 @@ export interface PersistentStorage {
} }
export interface PersistentStorageParams<T extends object> extends Omit<Options<T>, "migrations"> { export interface PersistentStorageParams<T extends object> extends Omit<Options<T>, "migrations"> {
syncOptions?: { readonly syncOptions?: {
fireImmediately?: boolean; readonly fireImmediately?: boolean;
equals?: IEqualsComparer<T>; equals?: IEqualsComparer<T>;
}; };
readonly configName: string; readonly configName: string;
readonly migrations?: Migrations;
migrations?: Migrations<Record<string, unknown>>;
/** /**
* fromStore is called internally when a child class syncs with the file * fromStore is called internally when a child class syncs with the file
@ -91,7 +90,7 @@ const createPersistentStorageInjectable = getInjectable({
const config = getConfigurationFileModel({ const config = getConfigurationFileModel({
projectName: "lens", projectName: "lens",
cwd, cwd,
migrations: migrations as Migrations<T>, migrations: migrations as Options<T>["migrations"],
...params, ...params,
}); });

View File

@ -4,19 +4,26 @@
*/ */
import type { InjectionToken } from "@ogre-tools/injectable"; import type { InjectionToken } from "@ogre-tools/injectable";
import { lifecycleEnum, getInjectable } from "@ogre-tools/injectable"; import { lifecycleEnum, getInjectable } from "@ogre-tools/injectable";
import type { Migrations } from "conf/dist/source/types";
import loggerInjectable from "../logger.injectable"; import loggerInjectable from "../logger.injectable";
import { getOrInsert, iter } from "@k8slens/utilities"; import { getOrInsert, iter, object } from "@k8slens/utilities";
export type AllowedSetValue<T> = T extends (...args: any[]) => any
? never
: T extends undefined | symbol
? never
: T;
export interface MigrationStore { export interface MigrationStore {
readonly path: string;
get(key: string): unknown; get(key: string): unknown;
delete(key: string): void; delete(key: string): void;
has(key: string): boolean; has(key: string): boolean;
clear(): void; clear(): void;
set(key: string, value: number | string | boolean | unknown[]): void; set<T>(key: string, value: AllowedSetValue<T>): void;
set<Key extends string>(key: string, value: Record<Key, unknown>): void;
} }
export type Migrations = Partial<Record<string, (store: MigrationStore) => void>>;
export interface MigrationDeclaration { export interface MigrationDeclaration {
version: string; version: string;
run(store: MigrationStore): void; run(store: MigrationStore): void;
@ -24,7 +31,7 @@ export interface MigrationDeclaration {
const persistentStorageMigrationsInjectable = getInjectable({ const persistentStorageMigrationsInjectable = getInjectable({
id: "persistent-storage-migrations", id: "persistent-storage-migrations",
instantiate: (di, token): Migrations<Record<string, unknown>> => { instantiate: (di, token) => {
const logger = di.inject(loggerInjectable); const logger = di.inject(loggerInjectable);
const declarations = di.injectMany(token); const declarations = di.injectMany(token);
const migrations = new Map<string, MigrationDeclaration["run"][]>(); const migrations = new Map<string, MigrationDeclaration["run"][]>();
@ -33,18 +40,15 @@ const persistentStorageMigrationsInjectable = getInjectable({
getOrInsert(migrations, decl.version, []).push(decl.run); getOrInsert(migrations, decl.version, []).push(decl.run);
} }
return Object.fromEntries( return iter.chain(migrations.entries())
iter.map( .map(([v, fns]) => [v, (store: MigrationStore) => {
migrations, logger.info(`Running ${v} migration for ${store.path}`);
([v, fns]) => [v, (store) => {
logger.info(`Running ${v} migration for ${store.path}`);
for (const fn of fns) { for (const fn of fns) {
fn(store); fn(store);
} }
}], }] as const)
), .collect(object.fromEntries);
);
}, },
lifecycle: lifecycleEnum.keyedSingleton({ lifecycle: lifecycleEnum.keyedSingleton({
getInstanceKey: (di, token: InjectionToken<MigrationDeclaration, void>) => token.id, getInstanceKey: (di, token: InjectionToken<MigrationDeclaration, void>) => token.id,

View File

@ -8,14 +8,15 @@ import type { LensExtension } from "./lens-extension";
import type { StaticThis } from "../common/utils/singleton"; import type { StaticThis } from "../common/utils/singleton";
import { getOrInsertWith } from "@k8slens/utilities"; import { getOrInsertWith } from "@k8slens/utilities";
import { getLegacyGlobalDiForExtensionApi } from "./as-legacy-globals-for-extension-api/legacy-global-di-for-extension-api"; import { getLegacyGlobalDiForExtensionApi } from "./as-legacy-globals-for-extension-api/legacy-global-di-for-extension-api";
import type { Migrations } from "conf/dist/source/types";
import type { PersistentStorage, PersistentStorageParams } from "../common/persistent-storage/create.injectable"; import type { PersistentStorage, PersistentStorageParams } from "../common/persistent-storage/create.injectable";
import createPersistentStorageInjectable from "../common/persistent-storage/create.injectable"; import createPersistentStorageInjectable from "../common/persistent-storage/create.injectable";
import directoryForUserDataInjectable from "../common/app-paths/directory-for-user-data/directory-for-user-data.injectable"; import directoryForUserDataInjectable from "../common/app-paths/directory-for-user-data/directory-for-user-data.injectable";
import assert from "assert"; import assert from "assert";
import type { Options } from "conf";
import type { Migrations } from "../common/persistent-storage/migrations.injectable";
export interface ExtensionStoreParams<T extends object> extends Omit<PersistentStorageParams<T>, "migrations" | "cwd" | "fromStore" | "toJSON"> { export interface ExtensionStoreParams<T extends object> extends Omit<PersistentStorageParams<T>, "migrations" | "cwd" | "fromStore" | "toJSON"> {
migrations?: Migrations<T>; migrations?: Options<T>["migrations"];
cwd?: string; cwd?: string;
} }
@ -76,7 +77,7 @@ export abstract class BaseExtensionStore<T extends object> {
...params, ...params,
cwd: this.cwd(), cwd: this.cwd(),
projectVersion, projectVersion,
migrations: migrations as Migrations<Record<string, unknown>>, migrations: migrations as Migrations,
fromStore: (data) => this.fromStore(data), fromStore: (data) => this.fromStore(data),
toJSON: () => this.toJSON(), toJSON: () => this.toJSON(),
}); });

View File

@ -26,8 +26,6 @@ const clustersPersistentStorageInjectable = getInjectable({
const readClusterConfigSync = di.inject(readClusterConfigSyncInjectable); const readClusterConfigSync = di.inject(readClusterConfigSyncInjectable);
const clustersState = di.inject(clustersStateInjectable); const clustersState = di.inject(clustersStateInjectable);
const logger = di.inject(loggerInjectable); const logger = di.inject(loggerInjectable);
const storeMigrationVersion = di.inject(storeMigrationVersionInjectable);
const migrations = di.inject(persistentStorageMigrationsInjectable, clusterStoreMigrationInjectionToken);
return createPersistentStorage<ClusterStoreModel>({ return createPersistentStorage<ClusterStoreModel>({
configName: "lens-cluster-store", configName: "lens-cluster-store",
@ -35,8 +33,8 @@ const clustersPersistentStorageInjectable = getInjectable({
syncOptions: { syncOptions: {
equals: comparer.structural, equals: comparer.structural,
}, },
projectVersion: storeMigrationVersion, projectVersion: di.inject(storeMigrationVersionInjectable),
migrations, migrations: di.inject(persistentStorageMigrationsInjectable, clusterStoreMigrationInjectionToken),
fromStore: action(({ clusters = [] }) => { fromStore: action(({ clusters = [] }) => {
const currentClusters = new Map(clustersState); const currentClusters = new Map(clustersState);
const newClusters = new Map<ClusterId, Cluster>(); const newClusters = new Map<ClusterId, Cluster>();

View File

@ -11,6 +11,8 @@ interface Iterator<T> extends Iterable<T> {
find(fn: (val: T) => unknown): T | undefined; find(fn: (val: T) => unknown): T | undefined;
collect<U>(fn: (values: Iterable<T>) => U): U; collect<U>(fn: (values: Iterable<T>) => U): U;
toArray(): T[]; toArray(): T[];
toMap(): T extends [infer K, infer V] ? Map<K, V> : never;
toSet(): Set<T>;
map<U>(fn: (val: T) => U): Iterator<U>; map<U>(fn: (val: T) => U): Iterator<U>;
flatMap<U>(fn: (val: T) => U[]): Iterator<U>; flatMap<U>(fn: (val: T) => U[]): Iterator<U>;
concat(src2: IterableIterator<T>): Iterator<T>; concat(src2: IterableIterator<T>): Iterator<T>;
@ -28,6 +30,8 @@ function chain<T>(src: IterableIterator<T>): Iterator<T> {
join: (sep) => join(src, sep), join: (sep) => join(src, sep),
collect: (fn) => fn(src), collect: (fn) => fn(src),
toArray: () => [...src], toArray: () => [...src],
toMap: () => new Map(src as IterableIterator<[any, any]>) as any,
toSet: () => new Set(src),
concat: (src2) => chain(concat(src, src2)), concat: (src2) => chain(concat(src, src2)),
take: (count) => chain(take(src, count)), take: (count) => chain(take(src, count)),
[Symbol.iterator]: () => src, [Symbol.iterator]: () => src,