diff --git a/src/explorer/models/kafka.ts b/src/explorer/models/kafka.ts index 4d6cc45..1bc2b2e 100644 --- a/src/explorer/models/kafka.ts +++ b/src/explorer/models/kafka.ts @@ -1,5 +1,5 @@ import { Disposable, TreeItemCollapsibleState } from "vscode"; -import { ClientAccessor, Cluster } from "../../client"; +import { ClientAccessor } from "../../client"; import { ClusterSettings } from "../../settings"; import { ClusterItem, NoClusterItem } from "./cluster"; import { NodeBase } from "./nodeBase"; @@ -16,8 +16,7 @@ export class KafkaModel extends NodeBase implements Disposable { } public async computeChildren(): Promise { - const clusters = this.clusterSettings.getAll() - .sort(this.sortByNameAscending); + const clusters = this.clusterSettings.getAll(); if (clusters.length === 0) { return [new NoClusterItem(this)]; } @@ -26,12 +25,6 @@ export class KafkaModel extends NodeBase implements Disposable { }); } - private sortByNameAscending(a: Cluster, b: Cluster): -1 | 0 | 1 { - if (a.name.toLowerCase() < b.name.toLowerCase()) { return -1; } - if (a.name.toLowerCase() > b.name.toLowerCase()) { return 1; } - return 0; - } - public dispose(): void { this.children?.forEach(child => (child).dispose()); } diff --git a/src/settings/clusters.ts b/src/settings/clusters.ts index 81a7165..b4e6375 100644 --- a/src/settings/clusters.ts +++ b/src/settings/clusters.ts @@ -21,7 +21,7 @@ export interface ClusterSettings { onDidChangeSelected: vscode.Event; /** - * Gets the full cluster collection.. + * Gets the full cluster collection sorted by cluster name. */ getAll(): Cluster[]; @@ -78,7 +78,14 @@ class MementoClusterSettings implements ClusterSettings { getAll(): Cluster[] { const state = this.storage.get(this.clusterCollectionStorageKey, {}); - return Object.values(state); + return Object.values(state) + .sort(this.sortByNameAscending); + } + + private sortByNameAscending(a: Cluster, b: Cluster): -1 | 0 | 1 { + if (a.name.toLowerCase() < b.name.toLowerCase()) { return -1; } + if (a.name.toLowerCase() > b.name.toLowerCase()) { return 1; } + return 0; } get(id: string): Cluster | undefined { @@ -112,7 +119,7 @@ class MementoClusterSettings implements ClusterSettings { } private setSelectedClusterIfNeeded(): void { - if (this.selected !== undefined){ + if (this.selected !== undefined) { return; } const all = this.getAll(); @@ -122,4 +129,4 @@ class MementoClusterSettings implements ClusterSettings { } } -export const getClusterSettings = (): ClusterSettings => MementoClusterSettings.getInstance(); \ No newline at end of file +export const getClusterSettings = (): ClusterSettings => MementoClusterSettings.getInstance();