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() {
return {
hass: {},
cardConfig: {},
entities: {},
};
}
async setConfig(config) {
@ -19,8 +17,8 @@ class AutoEntities extends LitElement {
}
this._config = config;
this.entities = [];
this.cardConfig = {entities: this.entities, ...config.card};
this._entities = [];
this.cardConfig = {entities: [], ...config.card};
}
async _getEntities()
@ -94,28 +92,47 @@ class AutoEntities extends LitElement {
return entities;
}
async updated(changedProperties) {
if(changedProperties.has("hass") && this.hass) {
set entities(ent) {
function compare(a,b) {
if( a === b ) return true;
if( a == null || b == null) return false;
if(a.length != b.length) return false;
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])
if(JSON.stringify(a[i]) !== JSON.stringify(b[i]))
return false;
return true;
}
const oldEntities = this.entities;
const newEntities = await this._getEntities();
if(!compare(oldEntities, newEntities)) {
this.entities = newEntities;
this.cardConfig = {
...this.cardConfig,
entities: newEntities,
};
if(!compare(ent, this._entities))
{
this._entities = ent;
this.cardConfig = {...this.cardConfig, entities: this._entities};
}
}
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() {
@ -128,7 +145,6 @@ class AutoEntities extends LitElement {
}
return html`
<card-maker
.config=${this.cardConfig}
.hass=${this.hass}
></card-maker>`;
}