From fbfe5673a7079214f3152a7b99e199721475a6bc Mon Sep 17 00:00:00 2001 From: Roman Date: Thu, 28 Jan 2021 19:18:08 +0200 Subject: [PATCH] clean up Signed-off-by: Roman --- src/renderer/api/kube-watch-api.ts | 49 ++++++++++++++---------------- 1 file changed, 22 insertions(+), 27 deletions(-) diff --git a/src/renderer/api/kube-watch-api.ts b/src/renderer/api/kube-watch-api.ts index 5947468010..8adf58676f 100644 --- a/src/renderer/api/kube-watch-api.ts +++ b/src/renderer/api/kube-watch-api.ts @@ -108,7 +108,7 @@ export class KubeWatchApi { } isAllowedApi(api: KubeApi): boolean { - return this.cluster.isAllowedResource(api.kind); + return !!this?.cluster.isAllowedResource(api.kind); } subscribeApi(api: KubeApi | KubeApi[]): () => void { @@ -134,8 +134,14 @@ export class KubeWatchApi { const limitRequests = plimit(1); // load stores one by one to allow quick skipping when fast clicking btw pages const preloading: Promise[] = []; const apis = new Set(stores.map(store => store.getSubscribeApis()).flat()); + const unsubscribeList: (() => void)[] = []; let isUnsubscribed = false; + const subscribe = () => { + if (isUnsubscribed) return; + apis.forEach(api => unsubscribeList.push(this.subscribeApi(api))); + }; + if (preload) { for (const store of stores) { preloading.push(limitRequests(async () => { @@ -146,35 +152,24 @@ export class KubeWatchApi { } } - const subscribe = () => { - const unsubscribeList: (() => void)[] = []; - - const subscribeApis = () => { - if (isUnsubscribed) return; - apis.forEach(api => unsubscribeList.push(this.subscribeApi(api))); - }; - - if (waitUntilLoaded) { - Promise.all(preloading).then(subscribeApis, error => { - this.log({ - message: new Error("Loading stores has failed"), - meta: { stores, error, options }, - }); + if (waitUntilLoaded) { + Promise.all(preloading).then(subscribe, error => { + this.log({ + message: new Error("Loading stores has failed"), + meta: { stores, error, options }, }); - } else { - subscribeApis(); - } + }); + } else { + subscribe(); + } - // unsubscribe - return () => { - if (isUnsubscribed) return; - isUnsubscribed = true; - limitRequests.clearQueue(); - unsubscribeList.forEach(unsubscribe => unsubscribe()); - }; + // unsubscribe + return () => { + if (isUnsubscribed) return; + isUnsubscribed = true; + limitRequests.clearQueue(); + unsubscribeList.forEach(unsubscribe => unsubscribe()); }; - - return subscribe(); } protected async connectionCheck() {