Clean system health subscription after data is collected (#17665)

This commit is contained in:
Simon Lamon 2023-08-22 17:20:13 +02:00 committed by GitHub
parent aa2b2b0d16
commit bbb6fccaec
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 30 deletions

View File

@ -69,7 +69,7 @@ type SystemHealthEvent =
export const subscribeSystemHealthInfo = ( export const subscribeSystemHealthInfo = (
hass: HomeAssistant, hass: HomeAssistant,
callback: (info: SystemHealthInfo) => void callback: (info: SystemHealthInfo | undefined) => void
) => { ) => {
let data = {}; let data = {};
@ -82,6 +82,7 @@ export const subscribeSystemHealthInfo = (
} }
if (updateEvent.type === "finish") { if (updateEvent.type === "finish") {
unsubProm.then((unsub) => unsub()); unsubProm.then((unsub) => unsub());
callback(undefined);
return; return;
} }

View File

@ -71,7 +71,9 @@ class DialogSystemInformation extends LitElement {
@state() private _opened = false; @state() private _opened = false;
private _subscriptions?: Array<UnsubscribeFunc | Promise<UnsubscribeFunc>>; private _systemHealthSubscription?: Promise<UnsubscribeFunc>;
private _hassIOSubscription?: UnsubscribeFunc;
public showDialog(): void { public showDialog(): void {
this._opened = true; this._opened = true;
@ -86,48 +88,43 @@ class DialogSystemInformation extends LitElement {
} }
private _subscribe(): void { private _subscribe(): void {
const subs: Array<UnsubscribeFunc | Promise<UnsubscribeFunc>> = [];
if (isComponentLoaded(this.hass, "system_health")) { if (isComponentLoaded(this.hass, "system_health")) {
subs.push( this._systemHealthSubscription = subscribeSystemHealthInfo(
subscribeSystemHealthInfo(this.hass!, (info) => { this.hass,
this._systemInfo = info; (info) => {
}) if (!info) {
this._systemHealthSubscription = undefined;
} else {
this._systemInfo = info;
}
}
); );
} }
if (isComponentLoaded(this.hass, "hassio")) { if (isComponentLoaded(this.hass, "hassio")) {
subs.push( this._hassIOSubscription = subscribePollingCollection(
subscribePollingCollection( this.hass,
this.hass, async () => {
async () => { this._supervisorStats = await fetchHassioStats(
this._supervisorStats = await fetchHassioStats( this.hass,
this.hass, "supervisor"
"supervisor" );
); this._coreStats = await fetchHassioStats(this.hass, "core");
this._coreStats = await fetchHassioStats(this.hass, "core"); },
}, 10000
10000
)
); );
fetchHassioResolution(this.hass).then((data) => { fetchHassioResolution(this.hass).then((data) => {
this._resolutionInfo = data; this._resolutionInfo = data;
}); });
} }
this._subscriptions = subs;
} }
private _unsubscribe() { private _unsubscribe() {
while (this._subscriptions?.length) { this._systemHealthSubscription?.then((unsubFunc) => unsubFunc());
const unsub = this._subscriptions.pop()!; this._systemHealthSubscription = undefined;
if (unsub instanceof Promise) { this._hassIOSubscription?.();
unsub.then((unsubFunc) => unsubFunc()); this._hassIOSubscription = undefined;
} else {
unsub();
}
}
this._subscriptions = undefined;
this._systemInfo = undefined; this._systemInfo = undefined;
this._resolutionInfo = undefined; this._resolutionInfo = undefined;