Performance improvements. Fix #32, I hope.

This commit is contained in:
Thomas Lovén 2019-11-12 09:15:51 +01:00
parent 65b2bd2fda
commit 01e22c1e3f
2 changed files with 41 additions and 26 deletions

File diff suppressed because one or more lines are too long

View File

@ -9,8 +9,6 @@ class AutoEntities extends LitElement {
static get properties() { static get properties() {
return { return {
hass: {}, hass: {},
cardConfig: {},
entities: {},
}; };
} }
async setConfig(config) { async setConfig(config) {
@ -19,8 +17,8 @@ class AutoEntities extends LitElement {
} }
this._config = config; this._config = config;
this.entities = []; this._entities = [];
this.cardConfig = {entities: this.entities, ...config.card}; this.cardConfig = {entities: [], ...config.card};
} }
async _getEntities() async _getEntities()
@ -94,28 +92,47 @@ class AutoEntities extends LitElement {
return entities; return entities;
} }
async updated(changedProperties) { set entities(ent) {
if(changedProperties.has("hass") && this.hass) {
function compare(a,b) { function compare(a,b) {
if( a === b ) return true; if( a === b )
if( a == null || b == null) return false; return true;
if(a.length != b.length) return false; if( a == null || b == null)
return false;
if(a.length != b.length)
return false;
for(var i = 0; i < a.length; i++) for(var i = 0; i < a.length; i++)
if(a[i] !== b[i]) if(JSON.stringify(a[i]) !== JSON.stringify(b[i]))
return false; return false;
return true; return true;
} }
if(!compare(ent, this._entities))
const oldEntities = this.entities; {
const newEntities = await this._getEntities(); this._entities = ent;
if(!compare(oldEntities, newEntities)) { this.cardConfig = {...this.cardConfig, entities: this._entities};
this.entities = newEntities;
this.cardConfig = {
...this.cardConfig,
entities: newEntities,
};
} }
} }
get entities() {
return this._entities;
}
set cardConfig(cardConfig) {
this._cardConfig = cardConfig;
if(this.querySelector("card-maker"))
this.querySelector("card-maker").config = cardConfig;
}
get cardConfig() {
return this._cardConfig;
}
firstUpdated() {
this.cardConfig = this._cardConfig;
}
updated(changedProperties) {
if(changedProperties.has("hass") && this.hass) {
// Run this in a timeout to improve performance
setTimeout(() => this._getEntities().then((e) => this.entities = e), 0);
}
} }
createRenderRoot() { createRenderRoot() {
@ -128,7 +145,6 @@ class AutoEntities extends LitElement {
} }
return html` return html`
<card-maker <card-maker
.config=${this.cardConfig}
.hass=${this.hass} .hass=${this.hass}
></card-maker>`; ></card-maker>`;
} }