From bbb6fccaec888d456069f0f3c311caa793cad3a4 Mon Sep 17 00:00:00 2001 From: Simon Lamon <32477463+silamon@users.noreply.github.com> Date: Tue, 22 Aug 2023 17:20:13 +0200 Subject: [PATCH] Clean system health subscription after data is collected (#17665) --- src/data/system_health.ts | 3 +- .../repairs/dialog-system-information.ts | 55 +++++++++---------- 2 files changed, 28 insertions(+), 30 deletions(-) diff --git a/src/data/system_health.ts b/src/data/system_health.ts index 72d5e20068..a98ef79b4f 100644 --- a/src/data/system_health.ts +++ b/src/data/system_health.ts @@ -69,7 +69,7 @@ type SystemHealthEvent = export const subscribeSystemHealthInfo = ( hass: HomeAssistant, - callback: (info: SystemHealthInfo) => void + callback: (info: SystemHealthInfo | undefined) => void ) => { let data = {}; @@ -82,6 +82,7 @@ export const subscribeSystemHealthInfo = ( } if (updateEvent.type === "finish") { unsubProm.then((unsub) => unsub()); + callback(undefined); return; } diff --git a/src/panels/config/repairs/dialog-system-information.ts b/src/panels/config/repairs/dialog-system-information.ts index c96ac4c574..5158d0b61f 100644 --- a/src/panels/config/repairs/dialog-system-information.ts +++ b/src/panels/config/repairs/dialog-system-information.ts @@ -71,7 +71,9 @@ class DialogSystemInformation extends LitElement { @state() private _opened = false; - private _subscriptions?: Array>; + private _systemHealthSubscription?: Promise; + + private _hassIOSubscription?: UnsubscribeFunc; public showDialog(): void { this._opened = true; @@ -86,48 +88,43 @@ class DialogSystemInformation extends LitElement { } private _subscribe(): void { - const subs: Array> = []; if (isComponentLoaded(this.hass, "system_health")) { - subs.push( - subscribeSystemHealthInfo(this.hass!, (info) => { - this._systemInfo = info; - }) + this._systemHealthSubscription = subscribeSystemHealthInfo( + this.hass, + (info) => { + if (!info) { + this._systemHealthSubscription = undefined; + } else { + this._systemInfo = info; + } + } ); } if (isComponentLoaded(this.hass, "hassio")) { - subs.push( - subscribePollingCollection( - this.hass, - async () => { - this._supervisorStats = await fetchHassioStats( - this.hass, - "supervisor" - ); - this._coreStats = await fetchHassioStats(this.hass, "core"); - }, - 10000 - ) + this._hassIOSubscription = subscribePollingCollection( + this.hass, + async () => { + this._supervisorStats = await fetchHassioStats( + this.hass, + "supervisor" + ); + this._coreStats = await fetchHassioStats(this.hass, "core"); + }, + 10000 ); fetchHassioResolution(this.hass).then((data) => { this._resolutionInfo = data; }); } - - this._subscriptions = subs; } private _unsubscribe() { - while (this._subscriptions?.length) { - const unsub = this._subscriptions.pop()!; - if (unsub instanceof Promise) { - unsub.then((unsubFunc) => unsubFunc()); - } else { - unsub(); - } - } - this._subscriptions = undefined; + this._systemHealthSubscription?.then((unsubFunc) => unsubFunc()); + this._systemHealthSubscription = undefined; + this._hassIOSubscription?.(); + this._hassIOSubscription = undefined; this._systemInfo = undefined; this._resolutionInfo = undefined;