Make setView not async (#3274)

This commit is contained in:
Paulus Schoutsen 2019-06-14 13:40:16 -07:00 committed by GitHub
parent 1add5077af
commit d609155022
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -57,8 +57,6 @@ import { computeRTLDirection } from "../../common/util/compute_rtl";
const CSS_CACHE = {}; const CSS_CACHE = {};
const JS_CACHE = {}; const JS_CACHE = {};
let loadedUnusedEntities = false;
class HUIRoot extends LitElement { class HUIRoot extends LitElement {
@property() public hass?: HomeAssistant; @property() public hass?: HomeAssistant;
@property() public lovelace?: Lovelace; @property() public lovelace?: Lovelace;
@ -441,7 +439,8 @@ class HUIRoot extends LitElement {
if (force && newSelectView === undefined) { if (force && newSelectView === undefined) {
newSelectView = this._curView; newSelectView = this._curView;
} }
this._selectView(newSelectView, force); // Will allow for ripples to start rendering
afterNextRender(() => this._selectView(newSelectView, force));
} }
} }
@ -565,10 +564,7 @@ class HUIRoot extends LitElement {
scrollToTarget(this, this._layout.header.scrollTarget); scrollToTarget(this, this._layout.header.scrollTarget);
} }
private async _selectView( private _selectView(viewIndex: HUIRoot["_curView"], force: boolean): void {
viewIndex: HUIRoot["_curView"],
force: boolean
): Promise<void> {
if (!force && this._curView === viewIndex) { if (!force && this._curView === viewIndex) {
return; return;
} }
@ -589,15 +585,16 @@ class HUIRoot extends LitElement {
} }
if (viewIndex === "hass-unused-entities") { if (viewIndex === "hass-unused-entities") {
if (!loadedUnusedEntities) {
loadedUnusedEntities = true;
await import(/* webpackChunkName: "hui-unused-entities" */ "./hui-unused-entities");
}
const unusedEntities = document.createElement("hui-unused-entities"); const unusedEntities = document.createElement("hui-unused-entities");
// Wait for promise to resolve so that the element has been upgraded.
import(/* webpackChunkName: "hui-unused-entities" */ "./hui-unused-entities").then(
() => {
unusedEntities.setConfig(this.config); unusedEntities.setConfig(this.config);
unusedEntities.hass = this.hass!; unusedEntities.hass = this.hass!;
}
);
root.style.background = this.config.background || ""; root.style.background = this.config.background || "";
root.appendChild(unusedEntities); root.append(unusedEntities);
return; return;
} }
@ -612,8 +609,6 @@ class HUIRoot extends LitElement {
if (!force && this._viewCache![viewIndex]) { if (!force && this._viewCache![viewIndex]) {
view = this._viewCache![viewIndex]; view = this._viewCache![viewIndex];
} else { } else {
await new Promise((resolve) => afterNextRender(resolve));
if (viewConfig.panel && viewConfig.cards && viewConfig.cards.length > 0) { if (viewConfig.panel && viewConfig.cards && viewConfig.cards.length > 0) {
view = createCardElement(viewConfig.cards[0]); view = createCardElement(viewConfig.cards[0]);
view.isPanel = true; view.isPanel = true;
@ -629,7 +624,7 @@ class HUIRoot extends LitElement {
view.hass = this.hass; view.hass = this.hass;
root.style.background = root.style.background =
viewConfig.background || this.config.background || ""; viewConfig.background || this.config.background || "";
root.appendChild(view); root.append(view);
} }
private _loadResources(resources) { private _loadResources(resources) {