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

improved singleton types inferring

Signed-off-by: Roman <ixrock@gmail.com>
This commit is contained in:
Roman 2020-07-17 20:00:47 +03:00
parent 0dbb47a398
commit a3edb75fea
9 changed files with 17 additions and 16 deletions

View File

@ -192,4 +192,4 @@ export class ClusterStore extends BaseStore<ClusterStoreModel> {
}
}
export const clusterStore: ClusterStore = ClusterStore.getInstance();
export const clusterStore = ClusterStore.getInstance<ClusterStore>();

View File

@ -45,4 +45,4 @@ export class Tracker extends Singleton {
}
}
export const tracker: Tracker = Tracker.getInstance(app || remote.app);
export const tracker = Tracker.getInstance<Tracker>(app || remote.app);

View File

@ -75,4 +75,4 @@ export class UserStore extends BaseStore<UserStoreModel> {
}
}
export const userStore: UserStore = UserStore.getInstance();
export const userStore = UserStore.getInstance<UserStore>();

View File

@ -6,11 +6,13 @@
* const usersStore: UsersStore = UsersStore.getInstance();
*/
type Constructor<T = {}> = new (...args: any[]) => T;
class Singleton {
private static instances = new WeakMap<object, Singleton>();
// todo: figure out how to infer child class + arguments types
static getInstance<T extends Singleton>(...args: any[]): T {
// todo: improve types inferring
static getInstance<T>(...args: ConstructorParameters<Constructor<T>>): T {
if (!Singleton.instances.has(this)) {
Singleton.instances.set(this, Reflect.construct(this, args));
}

View File

@ -93,4 +93,4 @@ export class WorkspaceStore extends BaseStore<WorkspaceStoreModel> {
}
}
export const workspaceStore: WorkspaceStore = WorkspaceStore.getInstance()
export const workspaceStore = WorkspaceStore.getInstance<WorkspaceStore>()

View File

@ -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"}`)

View File

@ -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<string, number>()
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;

View File

@ -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 {

View File

@ -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"