From a3edb75fea2994fdab3bc41d7095292ac9713859 Mon Sep 17 00:00:00 2001 From: Roman Date: Fri, 17 Jul 2020 20:00:47 +0300 Subject: [PATCH] improved singleton types inferring Signed-off-by: Roman --- src/common/cluster-store.ts | 2 +- src/common/tracker.ts | 2 +- src/common/user-store.ts | 2 +- src/common/utils/singleton.ts | 6 ++++-- src/common/workspace-store.ts | 2 +- src/main/index.ts | 2 +- src/main/lens-proxy.ts | 10 ++++------ src/main/window-manager.ts | 1 + yarn.lock | 6 +++--- 9 files changed, 17 insertions(+), 16 deletions(-) diff --git a/src/common/cluster-store.ts b/src/common/cluster-store.ts index 083031eba1..54c16f94d9 100644 --- a/src/common/cluster-store.ts +++ b/src/common/cluster-store.ts @@ -192,4 +192,4 @@ export class ClusterStore extends BaseStore { } } -export const clusterStore: ClusterStore = ClusterStore.getInstance(); +export const clusterStore = ClusterStore.getInstance(); diff --git a/src/common/tracker.ts b/src/common/tracker.ts index 97550d2ad6..c904d2c806 100644 --- a/src/common/tracker.ts +++ b/src/common/tracker.ts @@ -45,4 +45,4 @@ export class Tracker extends Singleton { } } -export const tracker: Tracker = Tracker.getInstance(app || remote.app); +export const tracker = Tracker.getInstance(app || remote.app); diff --git a/src/common/user-store.ts b/src/common/user-store.ts index 7e09d8e0bd..705cdae23f 100644 --- a/src/common/user-store.ts +++ b/src/common/user-store.ts @@ -75,4 +75,4 @@ export class UserStore extends BaseStore { } } -export const userStore: UserStore = UserStore.getInstance(); +export const userStore = UserStore.getInstance(); diff --git a/src/common/utils/singleton.ts b/src/common/utils/singleton.ts index b940f2bfbb..70347f9b42 100644 --- a/src/common/utils/singleton.ts +++ b/src/common/utils/singleton.ts @@ -6,11 +6,13 @@ * const usersStore: UsersStore = UsersStore.getInstance(); */ +type Constructor = new (...args: any[]) => T; + class Singleton { private static instances = new WeakMap(); - // todo: figure out how to infer child class + arguments types - static getInstance(...args: any[]): T { + // todo: improve types inferring + static getInstance(...args: ConstructorParameters>): T { if (!Singleton.instances.has(this)) { Singleton.instances.set(this, Reflect.construct(this, args)); } diff --git a/src/common/workspace-store.ts b/src/common/workspace-store.ts index 393f14c0df..6afee4878e 100644 --- a/src/common/workspace-store.ts +++ b/src/common/workspace-store.ts @@ -93,4 +93,4 @@ export class WorkspaceStore extends BaseStore { } } -export const workspaceStore: WorkspaceStore = WorkspaceStore.getInstance() +export const workspaceStore = WorkspaceStore.getInstance() diff --git a/src/main/index.ts b/src/main/index.ts index 74a0b4a963..fc5dcade85 100644 --- a/src/main/index.ts +++ b/src/main/index.ts @@ -66,7 +66,7 @@ async function main() { // run proxy try { - proxyServer = LensProxy.create(clusterManager); + proxyServer = LensProxy.create(proxyPort, clusterManager); } catch (error) { logger.error(`Could not start proxy (127.0.0:${proxyPort}): ${error.message}`) await dialog.showErrorBox("Lens Error", `Could not start proxy (127.0.0:${proxyPort}): ${error.message || "unknown error"}`) diff --git a/src/main/lens-proxy.ts b/src/main/lens-proxy.ts index b5c485f4fc..8179b9d8d9 100644 --- a/src/main/lens-proxy.ts +++ b/src/main/lens-proxy.ts @@ -11,22 +11,20 @@ import { apiKubePrefix } from "../common/vars"; import logger from "./logger" export class LensProxy { - protected clusterManager: ClusterManager protected proxyServer: http.Server protected router: Router protected closed = false protected retryCounters = new Map() - static create(clusterManager: ClusterManager) { - return new LensProxy(clusterManager).listen(); + static create(port: number, clusterManager: ClusterManager) { + return new LensProxy(port, clusterManager).listen(); } - private constructor(clusterManager: ClusterManager) { - this.clusterManager = clusterManager; + private constructor(protected port: number, protected clusterManager: ClusterManager) { this.router = new Router(); } - listen(port = this.clusterManager.port): this { + listen(port = this.port): this { this.proxyServer = this.buildCustomProxy().listen(port); logger.info(`LensProxy server has started http://localhost:${port}`); return this; diff --git a/src/main/window-manager.ts b/src/main/window-manager.ts index fde815dd7f..828fd66e8f 100644 --- a/src/main/window-manager.ts +++ b/src/main/window-manager.ts @@ -5,6 +5,7 @@ import type { ClusterId } from "../common/cluster-store"; import { clusterStore } from "../common/cluster-store"; import logger from "./logger"; +// fixme: error when removing active cluster (can't activate next view => empty window) // fixme: remove switching view delay on first load export class WindowManager { diff --git a/yarn.lock b/yarn.lock index 5eee77baf2..fc614075ec 100644 --- a/yarn.lock +++ b/yarn.lock @@ -11255,9 +11255,9 @@ typeface-roboto@^0.0.75: integrity sha512-VrR/IiH00Z1tFP4vDGfwZ1esNqTiDMchBEXYY9kilT6wRGgFoCAlgkEUMHb1E3mB0FsfZhv756IF0+R+SFPfdg== typescript@^3.9.5: - version "3.9.5" - resolved "https://registry.yarnpkg.com/typescript/-/typescript-3.9.5.tgz#586f0dba300cde8be52dd1ac4f7e1009c1b13f36" - integrity sha512-hSAifV3k+i6lEoCJ2k6R2Z/rp/H3+8sdmcn5NrS3/3kE7+RyZXm9aqvxWqjEXHAd8b0pShatpcdMTvEdvAJltQ== + version "3.9.7" + resolved "https://registry.yarnpkg.com/typescript/-/typescript-3.9.7.tgz#98d600a5ebdc38f40cb277522f12dc800e9e25fa" + integrity sha512-BLbiRkiBzAwsjut4x/dsibSTB6yWpwT5qWmC2OfuCg3GgVQCSgMs4vEctYPhsaGtd0AeuuHMkjZ2h2WG8MSzRw== uglify-js@^3.1.4: version "3.9.4"