mirror of
https://github.com/home-assistant/frontend.git
synced 2025-07-16 22:06:34 +00:00
Cleanup event listeners (#18803)
This commit is contained in:
parent
7356db919a
commit
c1c186d279
@ -34,7 +34,7 @@ export class HuiNotificationDrawer extends LitElement {
|
|||||||
|
|
||||||
disconnectedCallback() {
|
disconnectedCallback() {
|
||||||
super.disconnectedCallback();
|
super.disconnectedCallback();
|
||||||
window.addEventListener("location-changed", this.closeDialog);
|
window.removeEventListener("location-changed", this.closeDialog);
|
||||||
}
|
}
|
||||||
|
|
||||||
showDialog({ narrow }) {
|
showDialog({ narrow }) {
|
||||||
|
@ -16,7 +16,6 @@ import {
|
|||||||
DataTableColumnContainer,
|
DataTableColumnContainer,
|
||||||
SelectionChangedEvent,
|
SelectionChangedEvent,
|
||||||
} from "../../../components/data-table/ha-data-table";
|
} from "../../../components/data-table/ha-data-table";
|
||||||
import "../../../components/data-table/ha-data-table-icon";
|
|
||||||
import "../../../components/ha-fab";
|
import "../../../components/ha-fab";
|
||||||
import "../../../components/ha-help-tooltip";
|
import "../../../components/ha-help-tooltip";
|
||||||
import "../../../components/ha-svg-icon";
|
import "../../../components/ha-svg-icon";
|
||||||
|
@ -430,23 +430,29 @@ export class VoiceAssistantsExpose extends LitElement {
|
|||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
public constructor() {
|
public connectedCallback() {
|
||||||
super();
|
super.connectedCallback();
|
||||||
window.addEventListener("location-changed", () => {
|
window.addEventListener("location-changed", this._locationChanged);
|
||||||
if (
|
window.addEventListener("popstate", this._popState);
|
||||||
window.location.search.substring(1) !== this._searchParms.toString()
|
}
|
||||||
) {
|
|
||||||
|
disconnectedCallback(): void {
|
||||||
|
super.disconnectedCallback();
|
||||||
|
window.removeEventListener("location-changed", this._locationChanged);
|
||||||
|
window.removeEventListener("popstate", this._popState);
|
||||||
|
}
|
||||||
|
|
||||||
|
private _locationChanged = () => {
|
||||||
|
if (window.location.search.substring(1) !== this._searchParms.toString()) {
|
||||||
this._searchParms = new URLSearchParams(window.location.search);
|
this._searchParms = new URLSearchParams(window.location.search);
|
||||||
}
|
}
|
||||||
});
|
};
|
||||||
window.addEventListener("popstate", () => {
|
|
||||||
if (
|
private _popState = () => {
|
||||||
window.location.search.substring(1) !== this._searchParms.toString()
|
if (window.location.search.substring(1) !== this._searchParms.toString()) {
|
||||||
) {
|
|
||||||
this._searchParms = new URLSearchParams(window.location.search);
|
this._searchParms = new URLSearchParams(window.location.search);
|
||||||
}
|
}
|
||||||
});
|
};
|
||||||
}
|
|
||||||
|
|
||||||
private async _fetchEntities() {
|
private async _fetchEntities() {
|
||||||
this._extEntities = await getExtendedEntityRegistryEntries(
|
this._extEntities = await getExtendedEntityRegistryEntries(
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
import "@material/mwc-button";
|
import "@material/mwc-button";
|
||||||
import deepFreeze from "deep-freeze";
|
import deepFreeze from "deep-freeze";
|
||||||
|
import { UnsubscribeFunc } from "home-assistant-js-websocket";
|
||||||
import { html, LitElement, TemplateResult } from "lit";
|
import { html, LitElement, TemplateResult } from "lit";
|
||||||
import { customElement, property, state } from "lit/decorators";
|
import { customElement, property, state } from "lit/decorators";
|
||||||
import { constructUrlCurrentPath } from "../../common/url/construct-url";
|
import { constructUrlCurrentPath } from "../../common/url/construct-url";
|
||||||
@ -9,6 +10,20 @@ import {
|
|||||||
} from "../../common/url/search-params";
|
} from "../../common/url/search-params";
|
||||||
import { domainToName } from "../../data/integration";
|
import { domainToName } from "../../data/integration";
|
||||||
import { subscribeLovelaceUpdates } from "../../data/lovelace";
|
import { subscribeLovelaceUpdates } from "../../data/lovelace";
|
||||||
|
import {
|
||||||
|
deleteConfig,
|
||||||
|
fetchConfig,
|
||||||
|
isStrategyDashboard,
|
||||||
|
LovelaceConfig,
|
||||||
|
LovelaceDashboardStrategyConfig,
|
||||||
|
LovelaceRawConfig,
|
||||||
|
saveConfig,
|
||||||
|
} from "../../data/lovelace/config/types";
|
||||||
|
import {
|
||||||
|
isStrategyView,
|
||||||
|
LovelaceViewConfig,
|
||||||
|
} from "../../data/lovelace/config/view";
|
||||||
|
import { fetchResources } from "../../data/lovelace/resource";
|
||||||
import { WindowWithPreloads } from "../../data/preloads";
|
import { WindowWithPreloads } from "../../data/preloads";
|
||||||
import "../../layouts/hass-error-screen";
|
import "../../layouts/hass-error-screen";
|
||||||
import "../../layouts/hass-loading-screen";
|
import "../../layouts/hass-loading-screen";
|
||||||
@ -19,20 +34,6 @@ import { showSaveDialog } from "./editor/show-save-config-dialog";
|
|||||||
import "./hui-root";
|
import "./hui-root";
|
||||||
import { generateLovelaceDashboardStrategy } from "./strategies/get-strategy";
|
import { generateLovelaceDashboardStrategy } from "./strategies/get-strategy";
|
||||||
import { Lovelace } from "./types";
|
import { Lovelace } from "./types";
|
||||||
import {
|
|
||||||
deleteConfig,
|
|
||||||
fetchConfig,
|
|
||||||
isStrategyDashboard,
|
|
||||||
LovelaceConfig,
|
|
||||||
LovelaceRawConfig,
|
|
||||||
LovelaceDashboardStrategyConfig,
|
|
||||||
saveConfig,
|
|
||||||
} from "../../data/lovelace/config/types";
|
|
||||||
import { fetchResources } from "../../data/lovelace/resource";
|
|
||||||
import {
|
|
||||||
isStrategyView,
|
|
||||||
LovelaceViewConfig,
|
|
||||||
} from "../../data/lovelace/config/view";
|
|
||||||
|
|
||||||
(window as any).loadCardHelpers = () => import("./custom-card-helpers");
|
(window as any).loadCardHelpers = () => import("./custom-card-helpers");
|
||||||
|
|
||||||
@ -71,12 +72,7 @@ export class LovelacePanel extends LitElement {
|
|||||||
|
|
||||||
private _fetchConfigOnConnect = false;
|
private _fetchConfigOnConnect = false;
|
||||||
|
|
||||||
private _unsubUpdates?;
|
private _unsubUpdates?: Promise<UnsubscribeFunc>;
|
||||||
|
|
||||||
constructor() {
|
|
||||||
super();
|
|
||||||
this._closeEditor = this._closeEditor.bind(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
public connectedCallback(): void {
|
public connectedCallback(): void {
|
||||||
super.connectedCallback();
|
super.connectedCallback();
|
||||||
@ -100,14 +96,21 @@ export class LovelacePanel extends LitElement {
|
|||||||
// Config was changed when we were not at the lovelace panel
|
// Config was changed when we were not at the lovelace panel
|
||||||
this._fetchConfig(false);
|
this._fetchConfig(false);
|
||||||
}
|
}
|
||||||
|
window.addEventListener("connection-status", this._handleConnectionStatus);
|
||||||
}
|
}
|
||||||
|
|
||||||
public disconnectedCallback(): void {
|
public disconnectedCallback(): void {
|
||||||
super.disconnectedCallback();
|
super.disconnectedCallback();
|
||||||
// On the main dashboard we want to stay subscribed as that one is cached.
|
// On the main dashboard we want to stay subscribed as that one is cached.
|
||||||
if (this.urlPath !== null && this._unsubUpdates) {
|
if (this.urlPath !== null && this._unsubUpdates) {
|
||||||
this._unsubUpdates();
|
this._unsubUpdates.then((unsub) => unsub());
|
||||||
|
this._unsubUpdates = undefined;
|
||||||
}
|
}
|
||||||
|
// reload lovelace on reconnect so we are sure we have the latest config
|
||||||
|
window.removeEventListener(
|
||||||
|
"connection-status",
|
||||||
|
this._handleConnectionStatus
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected render(): TemplateResult | void {
|
protected render(): TemplateResult | void {
|
||||||
@ -165,13 +168,14 @@ export class LovelacePanel extends LitElement {
|
|||||||
if (!this._unsubUpdates) {
|
if (!this._unsubUpdates) {
|
||||||
this._subscribeUpdates();
|
this._subscribeUpdates();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private _handleConnectionStatus = (ev) => {
|
||||||
// reload lovelace on reconnect so we are sure we have the latest config
|
// reload lovelace on reconnect so we are sure we have the latest config
|
||||||
window.addEventListener("connection-status", (ev) => {
|
|
||||||
if (ev.detail === "connected") {
|
if (ev.detail === "connected") {
|
||||||
this._fetchConfig(false);
|
this._fetchConfig(false);
|
||||||
}
|
}
|
||||||
});
|
};
|
||||||
}
|
|
||||||
|
|
||||||
private async _regenerateConfig() {
|
private async _regenerateConfig() {
|
||||||
const conf = await generateLovelaceDashboardStrategy(
|
const conf = await generateLovelaceDashboardStrategy(
|
||||||
@ -183,16 +187,16 @@ export class LovelacePanel extends LitElement {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private async _subscribeUpdates() {
|
private async _subscribeUpdates() {
|
||||||
this._unsubUpdates = await subscribeLovelaceUpdates(
|
this._unsubUpdates = subscribeLovelaceUpdates(
|
||||||
this.hass!.connection,
|
this.hass!.connection,
|
||||||
this.urlPath,
|
this.urlPath,
|
||||||
() => this._lovelaceChanged()
|
() => this._lovelaceChanged()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
private _closeEditor() {
|
private _closeEditor = () => {
|
||||||
this._panelState = "loaded";
|
this._panelState = "loaded";
|
||||||
}
|
};
|
||||||
|
|
||||||
private _lovelaceChanged() {
|
private _lovelaceChanged() {
|
||||||
if (this._ignoreNextUpdateEvent) {
|
if (this._ignoreNextUpdateEvent) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user