From de11075d2d3816930eef2ec6c763d9dadd48efb9 Mon Sep 17 00:00:00 2001 From: Roman Date: Thu, 20 May 2021 13:49:46 +0300 Subject: [PATCH] reverted some changes, removed auto-opening devtools in dev-mode Signed-off-by: Roman --- .../catalog/catalog-category-registry.ts | 4 +++ src/common/catalog/catalog-entity-registry.ts | 20 ++++------- src/common/utils/extended-map.ts | 34 +++++++++++++++++++ src/main/catalog-sources/kubeconfig-sync.ts | 21 ++++-------- src/main/window-manager.ts | 3 +- 5 files changed, 52 insertions(+), 30 deletions(-) diff --git a/src/common/catalog/catalog-category-registry.ts b/src/common/catalog/catalog-category-registry.ts index 1ed990a57a..019c11ae80 100644 --- a/src/common/catalog/catalog-category-registry.ts +++ b/src/common/catalog/catalog-category-registry.ts @@ -53,6 +53,10 @@ export class CatalogCategoryRegistry { return Array.from(this.categories); } + getById(id: string): CatalogCategory{ + return this.items.find(category => category.getId() === id); + } + getForGroupKind(group: string, kind: string): T | undefined { return this.groupKindLookup.get(group)?.get(kind) as T; } diff --git a/src/common/catalog/catalog-entity-registry.ts b/src/common/catalog/catalog-entity-registry.ts index 6658685f2d..5f395a20ab 100644 --- a/src/common/catalog/catalog-entity-registry.ts +++ b/src/common/catalog/catalog-entity-registry.ts @@ -21,25 +21,21 @@ import { action, computed, IComputedValue, IObservableArray, makeObservable, observable } from "mobx"; import type { CatalogEntity } from "./catalog-entity"; -import logger from "../../main/logger"; +import { iter } from "../utils"; export class CatalogEntityRegistry { - private logPrefix = `[CatalogEntityRegistry]`; protected sources = observable.map>(); constructor() { makeObservable(this); } - @action - addObservableSource(id: string, source: IObservableArray | IComputedValue) { - logger.debug(`${this.logPrefix}: adding observable source with id="${id}"`); + @action addObservableSource(id: string, source: IObservableArray) { + this.sources.set(id, computed(() => source)); + } - if (Array.isArray(source)) { - this.sources.set(id, computed(() => source.toJSON())); - } else { - this.sources.set(id, source); - } + @action addComputedSource(id: string, source: IComputedValue) { + this.sources.set(id, source); } @action removeSource(id: string) { @@ -47,9 +43,7 @@ export class CatalogEntityRegistry { } @computed get items(): CatalogEntity[] { - return Array.from(this.sources.values()) - .map(source => source.get()) - .flat(); + return Array.from(iter.flatMap(this.sources.values(), source => source.get())); } getItemsForApiKind(apiVersion: string, kind: string): T[] { diff --git a/src/common/utils/extended-map.ts b/src/common/utils/extended-map.ts index 86ff291fad..c8b7ff4c65 100644 --- a/src/common/utils/extended-map.ts +++ b/src/common/utils/extended-map.ts @@ -19,6 +19,7 @@ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +import { action, IEnhancer, IObservableMapInitialValues, ObservableMap } from "mobx"; export class ExtendedMap extends Map { static new(entries?: readonly (readonly [K, V])[] | null): ExtendedMap { @@ -64,3 +65,36 @@ export class ExtendedMap extends Map { return this.get(key); } } + +export class ExtendedObservableMap extends ObservableMap { + constructor(protected getDefault: () => V, initialData?: IObservableMapInitialValues, enhancer?: IEnhancer, name?: string) { + super(initialData, enhancer, name); + } + + @action + getOrInsert(key: K, val: V): V { + if (this.has(key)) { + return this.get(key); + } + + return this.set(key, val).get(key); + } + + @action + getOrInsertWith(key: K, getVal: () => V): V { + if (this.has(key)) { + return this.get(key); + } + + return this.set(key, getVal()).get(key); + } + + @action + getOrDefault(key: K): V { + if (this.has(key)) { + return this.get(key); + } + + return this.set(key, this.getDefault()).get(key); + } +} diff --git a/src/main/catalog-sources/kubeconfig-sync.ts b/src/main/catalog-sources/kubeconfig-sync.ts index 1d6426a3f1..80b304a2f9 100644 --- a/src/main/catalog-sources/kubeconfig-sync.ts +++ b/src/main/catalog-sources/kubeconfig-sync.ts @@ -19,13 +19,13 @@ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -import { action, computed, IComputedValue, makeObservable, observable, ObservableMap, runInAction, observe } from "mobx"; +import { action, observable, IComputedValue, computed, ObservableMap, runInAction, makeObservable, observe } from "mobx"; import { CatalogEntity, catalogEntityRegistry } from "../../common/catalog"; import { watch } from "chokidar"; import fs from "fs"; import fse from "fs-extra"; import type stream from "stream"; -import { Disposer, iter, Singleton } from "../../common/utils"; +import { Disposer, ExtendedObservableMap, iter, Singleton } from "../../common/utils"; import logger from "../logger"; import type { KubeConfig } from "@kubernetes/client-node"; import { loadConfigFromString, splitConfig, validateKubeConfig } from "../../common/kube-helpers"; @@ -60,7 +60,7 @@ export class KubeconfigSyncManager extends Singleton { logger.info(`${logPrefix} starting requested syncs`); - catalogEntityRegistry.addObservableSource(KubeconfigSyncManager.syncName, computed(() => ( + catalogEntityRegistry.addComputedSource(KubeconfigSyncManager.syncName, computed(() => ( Array.from(iter.flatMap( this.sources.values(), ([entities]) => entities.get() @@ -111,7 +111,6 @@ export class KubeconfigSyncManager extends Singleton { logger.info(`${logPrefix} starting sync of file/folder`, { filePath }); logger.debug(`${logPrefix} ${this.sources.size} files/folders watched`, { files: Array.from(this.sources.keys()) }); } catch (error) { - console.error(error); logger.warn(`${logPrefix} failed to start watching changes: ${error}`); } } @@ -255,25 +254,17 @@ async function watchFileChanges(filePath: string): Promise<[IComputedValue>(); + const rootSource = new ExtendedObservableMap>(observable.map); const derivedSource = computed(() => Array.from(iter.flatMap(rootSource.values(), from => iter.map(from.values(), child => child[1])))); const stoppers = new Map(); watcher .on("change", (childFilePath) => { - if (!rootSource.has(childFilePath)) { - rootSource.set(childFilePath, observable.map()); - } - stoppers.get(childFilePath)(); - stoppers.set(childFilePath, diffChangedConfig(childFilePath, rootSource.get(childFilePath))); + stoppers.set(childFilePath, diffChangedConfig(childFilePath, rootSource.getOrDefault(childFilePath))); }) .on("add", (childFilePath) => { - if (!rootSource.has(childFilePath)) { - rootSource.set(childFilePath, observable.map()); - } - - stoppers.set(childFilePath, diffChangedConfig(childFilePath, rootSource.get(childFilePath))); + stoppers.set(childFilePath, diffChangedConfig(childFilePath, rootSource.getOrDefault(childFilePath))); }) .on("unlink", (childFilePath) => { stoppers.get(childFilePath)(); diff --git a/src/main/window-manager.ts b/src/main/window-manager.ts index 451cf72179..4e5588e47a 100644 --- a/src/main/window-manager.ts +++ b/src/main/window-manager.ts @@ -31,7 +31,7 @@ import { Singleton } from "../common/utils"; import { ClusterFrameInfo, clusterFrameMap } from "../common/cluster-frames"; import { IpcRendererNavigationEvents } from "../renderer/navigation/events"; import logger from "./logger"; -import { isDevelopment, productName } from "../common/vars"; +import { productName } from "../common/vars"; import { LensProxy } from "./proxy/lens-proxy"; export class WindowManager extends Singleton { @@ -106,7 +106,6 @@ export class WindowManager extends Singleton { shell.openExternal(url); }) .on("dom-ready", () => { - this.mainWindow.webContents.openDevTools({ mode: "right", activate: isDevelopment }); appEventBus.emit({ name: "app", action: "dom-ready" }); }) .on("did-fail-load", (_event, code, desc) => {