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,27 +92,46 @@ 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 )
if( a === b ) return true;
if( a == null || b == null) return false;
if(a.length != b.length) return false;
for(var i = 0; i < a.length; i++)
if(a[i] !== b[i])
return false;
return true; return true;
} if( a == null || b == null)
return false;
if(a.length != b.length)
return false;
for(var i = 0; i < a.length; i++)
if(JSON.stringify(a[i]) !== JSON.stringify(b[i]))
return false;
return true;
}
if(!compare(ent, this._entities))
{
this._entities = ent;
this.cardConfig = {...this.cardConfig, entities: this._entities};
}
}
get entities() {
return this._entities;
}
const oldEntities = this.entities; set cardConfig(cardConfig) {
const newEntities = await this._getEntities(); this._cardConfig = cardConfig;
if(!compare(oldEntities, newEntities)) { if(this.querySelector("card-maker"))
this.entities = newEntities; this.querySelector("card-maker").config = cardConfig;
this.cardConfig = { }
...this.cardConfig, get cardConfig() {
entities: newEntities, 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);
} }
} }
@ -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>`;
} }