From 596ce9667d84ecb56ef09d4734e10d2977f08b22 Mon Sep 17 00:00:00 2001 From: ludeeus Date: Wed, 1 Jan 2020 12:32:44 +0000 Subject: [PATCH] Limit property updates of custom_repositories --- src/HacsFrontend.ts | 21 ++++++++--- src/misc/CustomRepositories.ts | 66 ++++++++++++++++++++++++++-------- src/misc/HiddenRepositories.ts | 10 ++++-- src/panels/settings.ts | 18 ++-------- src/types.ts | 6 ++++ 5 files changed, 83 insertions(+), 38 deletions(-) diff --git a/src/HacsFrontend.ts b/src/HacsFrontend.ts index d683a257..b49589dd 100644 --- a/src/HacsFrontend.ts +++ b/src/HacsFrontend.ts @@ -9,7 +9,7 @@ import { property, TemplateResult } from "lit-element"; -import { HACS } from "./Hacs"; +import { HACS, Hacs } from "./Hacs"; import { HacsStyle } from "./style/hacs-style"; import { @@ -20,7 +20,8 @@ import { Critical, SelectedValue, LocationChangedEvent, - LovelaceConfig + LovelaceConfig, + RepositoryActionData } from "./types"; import { load_lovelace } from "card-tools/src/hass"; @@ -52,12 +53,21 @@ class HacsFrontendBase extends LitElement { if (this.hacs.isnullorempty(repositories)) repositories = this.hacs.repositories; if (this.hacs.isnullorempty(status)) status = this.hacs.status; - this.hacs.configuration = configuration; - this.hacs.repositories = repositories; - this.hacs.status = status; + this.hacs = new Hacs(configuration, repositories, status); this.requestUpdate(); } + private RepositoryAction(ev): void { + console.log(ev.detail); + const evdata: RepositoryActionData = ev.detail; + this.hacs.RepositoryWebSocketAction( + this.hass, + evdata.repo, + evdata.action, + evdata.category + ); + } + public getRepositories(): void { this.hass.connection .sendMessagePromise({ @@ -144,6 +154,7 @@ class HacsFrontendBase extends LitElement { protected firstUpdated() { this.addEventListener("hacs-location-change", this.locationChanged); + this.addEventListener("hacs-repository-action", this.RepositoryAction); this.addEventListener("hacs-onboarding-done", this.onboardingDone); this.addEventListener("hacs-recreate", this._recreatehacs); this.addEventListener("hacs-force-reload", this._reload); diff --git a/src/misc/CustomRepositories.ts b/src/misc/CustomRepositories.ts index 30c3046e..f45854b6 100644 --- a/src/misc/CustomRepositories.ts +++ b/src/misc/CustomRepositories.ts @@ -2,13 +2,13 @@ import { LitElement, customElement, property, + PropertyValues, CSSResultArray, css, TemplateResult, html } from "lit-element"; import swal from "sweetalert"; -import { HomeAssistant } from "custom-card-helpers"; import { HacsStyle } from "../style/hacs-style"; import { HACS } from "../Hacs"; @@ -17,9 +17,39 @@ import { Route, RepositoryData } from "../types"; @customElement("hacs-custom-repositories") export class CustomRepositories extends LitElement { @property({ type: Object }) public hacs!: HACS; - @property({ type: Array }) public custom!: RepositoryData[]; - @property({ type: Object }) public hass!: HomeAssistant; @property({ type: Object }) public route!: Route; + @property({ type: Boolean }) private background_task: boolean = true; + @property({ type: Array }) private custom!: RepositoryData[]; + + protected update(changedProperties: PropertyValues): void { + super.update(changedProperties); + console.log(changedProperties); + } + + shouldUpdate(changedProperties: PropertyValues) { + changedProperties.forEach((_oldValue, propName) => { + if (propName === "hacs") { + this.background_task = this.hacs.status.background_task; + const customrepositories = this.getCustomRepositories(); + if (!this.custom || this.custom.length !== customrepositories.length) { + this.custom = customrepositories; + } + } + }); + return ( + changedProperties.has("custom") || + changedProperties.has("background_task") + ); + } + + getCustomRepositories(): RepositoryData[] { + return this.hacs.repositories + .sort((a, b) => (a.full_name > b.full_name ? 1 : -1)) + .filter(repo => { + if (repo.custom) return true; + else return false; + }); + } Delete(ev) { let RepoID: string; @@ -37,7 +67,13 @@ export class CustomRepositories extends LitElement { ] }).then(value => { if (!this.hacs.isnullorempty(value)) { - this.hacs.RepositoryWebSocketAction(this.hass, RepoID, "delete"); + this.dispatchEvent( + new CustomEvent("hacs-repository-action", { + detail: { repo: RepoID, action: "delete" }, + bubbles: true, + composed: true + }) + ); } }); } @@ -62,7 +98,13 @@ export class CustomRepositories extends LitElement { } var category = selected.category; var repo = ev.composedPath()[2].children[0].value; - this.hacs.RepositoryWebSocketAction(this.hass, repo, "add", category); + this.dispatchEvent( + new CustomEvent("hacs-repository-action", { + detail: { repo: repo, action: "add", category: category }, + bubbles: true, + composed: true + }) + ); swal( this.hacs.localize("settings.adding_new_repo", "{repo}", repo) + "\n" + @@ -100,26 +142,20 @@ export class CustomRepositories extends LitElement { ); return html``; } - - this.custom = this.hacs.repositories.filter(function(repo) { - if (!repo.custom) return false; - return true; - }); - + console.log(`Render! ${new Date()}`); return html`
- ${this.hacs.status.background_task + ${this.background_task ? html` ${this.hacs.localize("settings.bg_task_custom")} ` : html` - ${this.custom - .sort((a, b) => (a.full_name > b.full_name ? 1 : -1)) - .map( + ${this.custom && + this.custom.map( repo => html`
`; } return html` - + - + `; diff --git a/src/types.ts b/src/types.ts index 9532ced7..d1cfde0c 100644 --- a/src/types.ts +++ b/src/types.ts @@ -154,3 +154,9 @@ export interface HacsBanner extends HTMLElement { lovelaceconfig?: LovelaceConfig; status?: Status; } + +export interface RepositoryActionData { + repo: string; + action: string; + category?: string; +}