From b0801c17fea3fd84e3ef61a73844a4522e8e1deb Mon Sep 17 00:00:00 2001 From: Sebastian Malton Date: Tue, 4 Aug 2020 16:49:18 -0400 Subject: [PATCH] simplifying main code Signed-off-by: Sebastian Malton --- src/common/cluster-store.ts | 39 ++++++++++++------------------------- 1 file changed, 12 insertions(+), 27 deletions(-) diff --git a/src/common/cluster-store.ts b/src/common/cluster-store.ts index d9fcb497e3..bf5a0a84c2 100644 --- a/src/common/cluster-store.ts +++ b/src/common/cluster-store.ts @@ -22,10 +22,6 @@ interface ClusterIconOrdering { [workspaceId: string]: ClusterId[] } -interface ClusterIconOrderingMap { - [id: string]: number -} - export interface ClusterStoreModel { activeCluster?: ClusterId; // last opened cluster clusters?: ClusterModel[] @@ -123,26 +119,8 @@ export class ClusterStore extends BaseStore { return this.clusters.get(id); } - getByWorkspaceId(workspaceId: string, sorted = true): Cluster[] { - const ordering = this.clusterOrders[workspaceId]?.reduce((acc: ClusterIconOrderingMap, cur, i) => { acc[cur] = i; return acc; }, {}); - const clusters = this.clustersList.filter(cluster => cluster.workspace === workspaceId); - if (!ordering || !sorted) { - return clusters; - } - - const sortedClusters = []; - const unsortedClusters = []; - - for (const cluster of clusters) { - const index = ordering[cluster.id]; - if (typeof index === "number" && index >= 0) { - sortedClusters[index] = cluster; - } else { - unsortedClusters.push(cluster); - } - } - - return [...sortedClusters.filter(c => c), ...unsortedClusters]; + getByWorkspaceId(workspaceId: string): Cluster[] { + return this.clusterOrders[workspaceId].map(clusterId => this.clusters.get(clusterId)); } @action @@ -172,7 +150,7 @@ export class ClusterStore extends BaseStore { @action removeByWorkspaceId(workspaceId: string) { - this.getByWorkspaceId(workspaceId, false).forEach(cluster => { + this.getByWorkspaceId(workspaceId).forEach(cluster => { this.removeById(cluster.id) }) } @@ -186,7 +164,7 @@ export class ClusterStore extends BaseStore { this.clusterOrders = iconOrder; // update new clusters - clusters.forEach(clusterModel => { + for (const clusterModel of clusters) { let cluster = currentClusters.get(clusterModel.id); if (cluster) { cluster.updateModel(clusterModel); @@ -194,7 +172,14 @@ export class ClusterStore extends BaseStore { cluster = new Cluster(clusterModel); } newClusters.set(clusterModel.id, cluster); - }); + + if (!this.clusterOrders[cluster.workspace]) { + this.clusterOrders[cluster.workspace] = []; + } + if (!this.clusterOrders[cluster.workspace].includes(cluster.id)) { + this.clusterOrders[cluster.workspace].push(cluster.id); + } + } // update removed clusters currentClusters.forEach(cluster => {