From a9cac343b0f0035617e8d75b5bda351dfe54ae7a Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Fri, 14 Jun 2019 13:30:20 -0700 Subject: [PATCH] Preload LL (#3273) * Preload LL * Remove using observer --- src/data/lovelace.ts | 8 ++++++-- src/entrypoints/core.ts | 8 ++++++++ src/panels/config/cloud/alexa/cloud-alexa.ts | 11 +++++++---- src/panels/lovelace/ha-panel-lovelace.ts | 13 ++++++++++++- 4 files changed, 33 insertions(+), 7 deletions(-) diff --git a/src/data/lovelace.ts b/src/data/lovelace.ts index 32bebb98ff..246dadb038 100644 --- a/src/data/lovelace.ts +++ b/src/data/lovelace.ts @@ -61,10 +61,10 @@ export type ActionConfig = | NoActionConfig; export const fetchConfig = ( - hass: HomeAssistant, + conn: Connection, force: boolean ): Promise => - hass.callWS({ + conn.sendMessagePromise({ type: "lovelace/config", force, }); @@ -82,3 +82,7 @@ export const subscribeLovelaceUpdates = ( conn: Connection, onChange: () => void ) => conn.subscribeEvents(onChange, "lovelace_updated"); + +export interface WindowWithLovelaceProm extends Window { + llConfProm?: Promise; +} diff --git a/src/entrypoints/core.ts b/src/entrypoints/core.ts index 17f2794167..bc515431c9 100644 --- a/src/entrypoints/core.ts +++ b/src/entrypoints/core.ts @@ -15,6 +15,7 @@ import { subscribeThemes } from "../data/ws-themes"; import { subscribeUser } from "../data/ws-user"; import { HomeAssistant } from "../types"; import { hassUrl } from "../data/auth"; +import { fetchConfig, WindowWithLovelaceProm } from "../data/lovelace"; declare global { interface Window { @@ -61,6 +62,9 @@ const connProm = async (auth) => { } }; +if (__DEV__) { + performance.mark("hass-start"); +} window.hassConnection = authProm().then(connProm); // Start fetching some of the data that we will need. @@ -74,6 +78,10 @@ window.hassConnection.then(({ conn }) => { subscribePanels(conn, noop); subscribeThemes(conn, noop); subscribeUser(conn, noop); + + if (location.pathname === "/" || location.pathname.startsWith("/lovelace/")) { + (window as WindowWithLovelaceProm).llConfProm = fetchConfig(conn, false); + } }); window.addEventListener("error", (e) => { diff --git a/src/panels/config/cloud/alexa/cloud-alexa.ts b/src/panels/config/cloud/alexa/cloud-alexa.ts index c065df17f0..ee94b90ec5 100644 --- a/src/panels/config/cloud/alexa/cloud-alexa.ts +++ b/src/panels/config/cloud/alexa/cloud-alexa.ts @@ -9,7 +9,6 @@ import { } from "lit-element"; import "@polymer/paper-toggle-button"; import "@polymer/paper-icon-button"; -import { observer } from "@material/mwc-base/observer"; import "../../../../layouts/hass-subpage"; import "../../../../layouts/hass-loading-screen"; import "../../../../components/ha-card"; @@ -48,9 +47,6 @@ class CloudAlexa extends LitElement { @property() public hass!: HomeAssistant; @property() - @observer(function(this: CloudAlexa, value) { - this._entityConfigs = value.prefs.alexa_entity_configs; - }) public cloudStatus!: CloudStatusLoggedIn; @property({ type: Boolean }) public narrow!: boolean; @@ -206,6 +202,13 @@ class CloudAlexa extends LitElement { this._fetchData(); } + protected updated(changedProps) { + super.updated(changedProps); + if (changedProps.has("cloudStatus")) { + this._entityConfigs = this.cloudStatus.prefs.alexa_entity_configs; + } + } + private async _fetchData() { const entities = await fetchCloudAlexaEntities(this.hass); entities.sort((a, b) => { diff --git a/src/panels/lovelace/ha-panel-lovelace.ts b/src/panels/lovelace/ha-panel-lovelace.ts index e8765cfcc3..37b440490a 100644 --- a/src/panels/lovelace/ha-panel-lovelace.ts +++ b/src/panels/lovelace/ha-panel-lovelace.ts @@ -5,6 +5,7 @@ import { LovelaceConfig, saveConfig, subscribeLovelaceUpdates, + WindowWithLovelaceProm, } from "../../data/lovelace"; import "../../layouts/hass-loading-screen"; import "../../layouts/hass-error-screen"; @@ -200,9 +201,19 @@ class LovelacePanel extends LitElement { private async _fetchConfig(forceDiskRefresh) { let conf: LovelaceConfig; let confMode: Lovelace["mode"] = this.panel!.config.mode; + let confProm: Promise; + const llWindow = window as WindowWithLovelaceProm; + + // On first load, we speed up loading page by having LL promise ready + if (llWindow.llConfProm) { + confProm = llWindow.llConfProm; + llWindow.llConfProm = undefined; + } else { + confProm = fetchConfig(this.hass!.connection, forceDiskRefresh); + } try { - conf = await fetchConfig(this.hass!, forceDiskRefresh); + conf = await confProm; } catch (err) { if (err.code !== "config_not_found") { // tslint:disable-next-line