mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
cluster-store refactoring -- part 3
Signed-off-by: Roman <ixrock@gmail.com>
This commit is contained in:
1 parent
bde60b1625
commit
f474a20904
11 files changed
+290
-424
No files matched your search
@@ -3,7 +3,7 @@ import Config from "conf"
|
||||
import { Options as ConfOptions } from "conf/dist/source/types"
|
||||
import produce from "immer";
|
||||
import { app, remote } from "electron"
|
||||
import { observable, reaction, toJS, when } from "mobx";
|
||||
import { action, observable, reaction, toJS, when } from "mobx";
|
||||
import Singleton from "./utils/singleton";
|
||||
import isEqual from "lodash/isEqual"
|
||||
import { getAppVersion } from "./utils/app-version";
|
||||
@@ -65,7 +65,6 @@ export class BaseStore<T = any> extends Singleton {
|
||||
configName: configName,
|
||||
watch: syncEnabled, // watch for changes in multi-process app (e.g. main/renderer)
|
||||
get cwd() {
|
||||
// todo: remove remote.app in favor ipc.invoke
|
||||
return (app || remote.app).getPath("userData");
|
||||
},
|
||||
...confOptions,
|
||||
@@ -111,11 +110,12 @@ export class BaseStore<T = any> extends Singleton {
|
||||
}
|
||||
}
|
||||
|
||||
// todo: use "serializr" ?
|
||||
@action
|
||||
protected fromStore(data: Partial<T> = {}) {
|
||||
Object.assign(this.data, data);
|
||||
}
|
||||
|
||||
@action
|
||||
merge(updater: (modelDraft: T) => void) {
|
||||
this.data = produce(this.data, updater);
|
||||
}
|
||||
|
||||
+34
-40
@@ -1,7 +1,8 @@
|
||||
import { action, computed, toJS } from "mobx";
|
||||
import migrations from "../migrations/cluster-store"
|
||||
import { action, observable, toJS } from "mobx";
|
||||
import { v4 as uuid } from "uuid"
|
||||
import { BaseStore } from "./base-store";
|
||||
import { Cluster } from "../main/cluster";
|
||||
import migrations from "../migrations/cluster-store"
|
||||
|
||||
export interface ClusterStoreModel {
|
||||
clusters: ClusterModel[]
|
||||
@@ -13,8 +14,8 @@ export interface ClusterModel {
|
||||
id: ClusterId;
|
||||
contextName: string;
|
||||
kubeConfigPath: string;
|
||||
kubeConfig?: string;
|
||||
port?: number;
|
||||
kubeConfig?: string;
|
||||
workspace?: string;
|
||||
preferences?: ClusterPreferences;
|
||||
}
|
||||
@@ -36,6 +37,8 @@ export interface ClusterPreferences {
|
||||
}
|
||||
|
||||
export class ClusterStore extends BaseStore<ClusterStoreModel> {
|
||||
@observable clusters = observable.map<ClusterId, Cluster>();
|
||||
|
||||
private constructor() {
|
||||
super({
|
||||
configName: "lens-cluster-store",
|
||||
@@ -46,60 +49,51 @@ export class ClusterStore extends BaseStore<ClusterStoreModel> {
|
||||
});
|
||||
}
|
||||
|
||||
// setup initial value
|
||||
protected data: ClusterStoreModel = {
|
||||
clusters: [],
|
||||
getById(id: ClusterId): Cluster {
|
||||
return this.clusters.get(id);
|
||||
}
|
||||
|
||||
@computed get clusters(): Cluster[] {
|
||||
return toJS(this.data.clusters).map(model => new Cluster(model));
|
||||
getByWorkspaceId(workspaceId: string): Cluster[] {
|
||||
return Array.from(this.clusters.values()).filter(cluster => {
|
||||
return cluster.workspace === workspaceId;
|
||||
})
|
||||
}
|
||||
|
||||
@computed get clustersMap(): Map<string, Cluster> {
|
||||
return this.clusters.reduce((map, cluster) => {
|
||||
map.set(cluster.id, cluster);
|
||||
return map;
|
||||
}, new Map);
|
||||
}
|
||||
|
||||
getById(clusterId: ClusterId): Cluster {
|
||||
return this.clusters.find(cluster => cluster.id === clusterId)
|
||||
}
|
||||
|
||||
getIndexById(clusterId: ClusterId): number {
|
||||
return this.clusters.findIndex(cluster => cluster.id === clusterId)
|
||||
@action
|
||||
addCluster(model: ClusterModel): Cluster {
|
||||
const id = model.id || uuid();
|
||||
const cluster = new Cluster({ ...model, id })
|
||||
this.clusters.set(id, cluster);
|
||||
return cluster;
|
||||
}
|
||||
|
||||
@action
|
||||
removeById(clusterId: ClusterId): void {
|
||||
const index = this.getIndexById(clusterId);
|
||||
if (index > -1) {
|
||||
this.data.clusters.splice(index, 1);
|
||||
}
|
||||
this.clusters.delete(clusterId);
|
||||
}
|
||||
|
||||
@action
|
||||
removeAllByWorkspaceId(workspaceId: string) {
|
||||
this.clusters.forEach(cluster => {
|
||||
if (cluster.workspace === workspaceId) {
|
||||
this.removeById(cluster.id)
|
||||
removeByWorkspaceId(workspaceId: string) {
|
||||
this.getByWorkspaceId(workspaceId).forEach(cluster => {
|
||||
this.removeById(cluster.id)
|
||||
})
|
||||
}
|
||||
|
||||
@action
|
||||
protected fromStore({ clusters = [] }: Partial<ClusterStoreModel> = {}) {
|
||||
// fixme: handle clusters update + delete
|
||||
clusters.forEach(model => {
|
||||
if (!this.clusters.has(model.id)) {
|
||||
this.clusters.set(model.id, new Cluster(model));
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
toJSON(): ClusterStoreModel {
|
||||
const clusters: ClusterModel[] = this.clusters.map(cluster => {
|
||||
return {
|
||||
id: cluster.id,
|
||||
contextName: cluster.contextName,
|
||||
kubeConfigPath: cluster.kubeConfigPath,
|
||||
preferences: cluster.preferences,
|
||||
workspace: cluster.workspace,
|
||||
}
|
||||
const clusters = Array.from(this.clusters).map(([id, cluster]) => cluster.toJSON());
|
||||
return toJS({ clusters }, {
|
||||
recurseEverything: true
|
||||
})
|
||||
return {
|
||||
clusters
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -9,16 +9,15 @@ export interface IpcOptions {
|
||||
timeout?: number;
|
||||
}
|
||||
|
||||
export async function sendMessage(channel: string, ...args: any[]) {
|
||||
logger.debug(`[IPC]: invoke "${channel}" with arguments`, args);
|
||||
export async function invokeMessage(channel: string, ...args: any[]) {
|
||||
logger.debug(`[IPC]: invoke channel "${channel}"`, { args });
|
||||
return ipcRenderer.invoke(channel, ...args);
|
||||
}
|
||||
|
||||
// todo: maybe spawn callback in separate thread/worker
|
||||
export function onMessage<T = any>(channel: string, callback: (...args: any[]) => T, options: IpcOptions = {}) {
|
||||
export function onMessage(channel: string, handler: (...args: any[]) => any, options: IpcOptions = {}) {
|
||||
const { timeout = 0 } = options;
|
||||
ipcMain.handle(channel, async (event, ...args: any[]) => {
|
||||
logger.debug(`[IPC]: handle "${channel}"`, event, args);
|
||||
logger.debug(`[IPC]: handle "${channel}"`, { event, args });
|
||||
return new Promise(async (resolve, reject) => {
|
||||
let timerId;
|
||||
if (timeout) {
|
||||
@@ -28,7 +27,7 @@ export function onMessage<T = any>(channel: string, callback: (...args: any[]) =
|
||||
}, timeout);
|
||||
}
|
||||
try {
|
||||
const result = await callback(...args);
|
||||
const result = await handler(...args); // todo: maybe exec in separate thread/worker
|
||||
clearTimeout(timerId);
|
||||
return result;
|
||||
} catch (err) {
|
||||
@@ -37,3 +36,9 @@ export function onMessage<T = any>(channel: string, callback: (...args: any[]) =
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
export function onMessages(messages: Record<string, Function>, options?: IpcOptions) {
|
||||
Object.entries(messages).forEach(([channel, handler]) => {
|
||||
this.onMessage(channel, handler, options);
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
// IPC messages (all channels)
|
||||
|
||||
export enum ClusterIpcMessage {
|
||||
CLUSTER_ADD = "cluster-add",
|
||||
CLUSTER_STOP = "cluster-stop",
|
||||
CLUSTER_REFRESH = "cluster-refresh",
|
||||
CLUSTER_REMOVE = "cluster-remove",
|
||||
CLUSTER_REMOVE_WORKSPACE = "cluster-remove-all-from-workspace",
|
||||
CLUSTER_EVENTS = "cluster-events-count",
|
||||
FEATURE_INSTALL = "cluster-feature-install",
|
||||
FEATURE_UPGRADE = "cluster-feature-upgrade",
|
||||
FEATURE_REMOVE = "cluster-feature-remove",
|
||||
ICON_SAVE = "cluster-icon-save",
|
||||
ICON_RESET = "cluster-icon-reset",
|
||||
}
|
||||
@@ -62,7 +62,7 @@ export class WorkspaceStore extends BaseStore<WorkspaceStoreModel> {
|
||||
const index = this.getIndexById(workspace.id);
|
||||
if (index > -1) {
|
||||
this.data.workspaces.splice(index, 1)
|
||||
clusterStore.removeAllByWorkspaceId(workspace.id)
|
||||
clusterStore.removeByWorkspaceId(workspace.id)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user