From 2c290cb32f7c0f99683ab61a19793ea22f826c98 Mon Sep 17 00:00:00 2001 From: Jari Kolehmainen Date: Fri, 7 May 2021 12:19:40 +0300 Subject: [PATCH] remove ExtendedObservableMap Signed-off-by: Jari Kolehmainen --- src/common/utils/extended-map.ts | 42 --------------------- src/main/catalog-sources/kubeconfig-sync.ts | 17 +++++++-- 2 files changed, 13 insertions(+), 46 deletions(-) diff --git a/src/common/utils/extended-map.ts b/src/common/utils/extended-map.ts index 1a32b3b4a9..a51b48c918 100644 --- a/src/common/utils/extended-map.ts +++ b/src/common/utils/extended-map.ts @@ -1,11 +1,3 @@ -import { - action, - IEnhancer, - IObservableMapInitialValues, - ObservableMap, - makeObservable, -} from "mobx"; - export class ExtendedMap extends Map { constructor(protected getDefault: () => V, entries?: readonly (readonly [K, V])[] | null) { super(entries); @@ -35,37 +27,3 @@ export class ExtendedMap extends Map { return this.set(key, this.getDefault()).get(key); } } - -export class ExtendedObservableMap extends ObservableMap { - constructor(protected getDefault: () => V, initialData?: IObservableMapInitialValues, enhancer?: IEnhancer, name?: string) { - super(initialData, enhancer, name); - makeObservable(this); - } - - @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 643d6f1a17..4fbbb83a1c 100644 --- a/src/main/catalog-sources/kubeconfig-sync.ts +++ b/src/main/catalog-sources/kubeconfig-sync.ts @@ -12,7 +12,7 @@ import { watch } from "chokidar"; import fs from "fs"; import fse from "fs-extra"; import stream from "stream"; -import { Disposer, ExtendedObservableMap, iter, Singleton } from "../../common/utils"; +import { Disposer, iter, Singleton } from "../../common/utils"; import logger from "../logger"; import { KubeConfig } from "@kubernetes/client-node"; import { loadConfigFromString, splitConfig, validateKubeConfig } from "../../common/kube-helpers"; @@ -98,6 +98,7 @@ 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}`); } } @@ -241,17 +242,25 @@ async function watchFileChanges(filePath: string): Promise<[IComputedValue>(observable.map); + const rootSource = new ObservableMap>(); 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.getOrDefault(childFilePath))); + stoppers.set(childFilePath, diffChangedConfig(childFilePath, rootSource.get(childFilePath))); }) .on("add", (childFilePath) => { - stoppers.set(childFilePath, diffChangedConfig(childFilePath, rootSource.getOrDefault(childFilePath))); + if (!rootSource.has(childFilePath)) { + rootSource.set(childFilePath, observable.map()); + } + + stoppers.set(childFilePath, diffChangedConfig(childFilePath, rootSource.get(childFilePath))); }) .on("unlink", (childFilePath) => { stoppers.get(childFilePath)();