From 4a8079debc63e3af5471543ea708f4a185cc3301 Mon Sep 17 00:00:00 2001 From: Roman Date: Tue, 12 Jan 2021 13:37:20 +0200 Subject: [PATCH] chore / small fixes Signed-off-by: Roman --- .../components/+namespaces/namespace.store.ts | 50 +++++++++---------- .../item-object-list/item-list-layout.tsx | 23 +++++---- src/renderer/kube-object.store.ts | 10 +++- 3 files changed, 45 insertions(+), 38 deletions(-) diff --git a/src/renderer/components/+namespaces/namespace.store.ts b/src/renderer/components/+namespaces/namespace.store.ts index 4818cc3fde..009dc72e71 100644 --- a/src/renderer/components/+namespaces/namespace.store.ts +++ b/src/renderer/components/+namespaces/namespace.store.ts @@ -45,13 +45,6 @@ export class NamespaceStore extends KubeObjectStore { this.init(); } - onContextChange(callback: (contextNamespaces: string[]) => void, opts: IReactionOptions = {}): IReactionDisposer { - return reaction(() => this.contextNs.toJS(), callback, { - equals: comparer.identity, - ...opts, - }); - } - private async init() { await clusterStore.whenLoaded; if (!getHostedCluster()) return; @@ -59,29 +52,32 @@ export class NamespaceStore extends KubeObjectStore { this.isReady = true; this.setContext(this.initNamespaces); + this.onSelectedNamespacesChange(); + this.onAllowedNamespacesChange(); + } - const disposers: IReactionDisposer[] = []; + public onContextChange(callback: (contextNamespaces: string[]) => void, opts: IReactionOptions = {}): IReactionDisposer { + return reaction(() => this.contextNs.toJS(), callback, { + equals: comparer.identity, + ...opts, + }); + } - // save selected namespaces to local-storage and update URL - disposers.push( - this.onContextChange(namespaces => { - storage.set(namespaces); - namespaceUrlParam.set(namespaces, { replaceHistory: true }); - }, { - fireImmediately: true, - equals: comparer.identity, - }) - ); + private onSelectedNamespacesChange(): IReactionDisposer { + return this.onContextChange(namespaces => { + storage.set(namespaces); // save to local-storage + namespaceUrlParam.set(namespaces, { replaceHistory: true }); // update url + }, { + fireImmediately: true, + equals: comparer.identity, + }); + } - // auto-load allowed namespaces - disposers.push( - reaction(() => this.allowedNamespaces, () => this.loadAll(), { - fireImmediately: true, - equals: comparer.identity, - }) - ); - - return disposers; + private onAllowedNamespacesChange(): IReactionDisposer { + return reaction(() => this.allowedNamespaces, () => this.loadAll(), { + fireImmediately: true, + equals: comparer.identity, + }); } get allowedNamespaces(): string[] { diff --git a/src/renderer/components/item-object-list/item-list-layout.tsx b/src/renderer/components/item-object-list/item-list-layout.tsx index 58e663df8d..77810f008e 100644 --- a/src/renderer/components/item-object-list/item-list-layout.tsx +++ b/src/renderer/components/item-object-list/item-list-layout.tsx @@ -112,11 +112,13 @@ export class ItemListLayout extends React.Component { } async componentDidMount() { - disposeOnUnmount(this, [ - namespaceStore.onContextChange(() => this.loadStores(), { - fireImmediately: true, - }) - ]); + if (!this.props.isClusterScoped) { + disposeOnUnmount(this, [ + namespaceStore.onContextChange(() => this.loadStores(), { + fireImmediately: true, + }) + ]); + } } async componentWillUnmount() { @@ -124,7 +126,7 @@ export class ItemListLayout extends React.Component { this.unsubscribeStores(); } - async loadStores() { + @computed get stores() { const { store, dependentStores, isClusterScoped } = this.props; const stores = new Set([store, ...dependentStores]); @@ -132,11 +134,14 @@ export class ItemListLayout extends React.Component { stores.add(namespaceStore); } - // reset - this.unsubscribeStores(); + return stores; + } + + async loadStores() { + this.unsubscribeStores(); // reset first // load - for (const store of stores) { + for (const store of this.stores) { if (this.isUnmounting) { this.unsubscribeStores(); break; diff --git a/src/renderer/kube-object.store.ts b/src/renderer/kube-object.store.ts index f188dd8625..3ea6d3f3c3 100644 --- a/src/renderer/kube-object.store.ts +++ b/src/renderer/kube-object.store.ts @@ -99,8 +99,14 @@ export abstract class KubeObjectStore extends ItemSt let items: T[]; try { - const { namespaceStore } = await import("./components/+namespaces/namespace.store"); - const contextNamespaces = params.namespaces || namespaceStore.getContextNamespaces(); + let contextNamespaces = params.namespaces; + + if (!params.namespaces) { + const { namespaceStore } = await import("./components/+namespaces/namespace.store"); + + await namespaceStore.whenReady; + contextNamespaces = namespaceStore.getContextNamespaces(); + } items = await this.loadItems({ isAdmin: getHostedCluster().isAdmin,