From b86cf34492ec3712904559dfd889380a1ebab4aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20Lov=C3=A9n?= Date: Mon, 11 Feb 2019 21:52:23 +0100 Subject: [PATCH] Minimize number of rebuilds --- auto-entities.js | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/auto-entities.js b/auto-entities.js index fb372a7..2f4179c 100644 --- a/auto-entities.js +++ b/auto-entities.js @@ -174,17 +174,34 @@ class AutoEntities extends cardTools.litElement() { } } + _compare_arrays(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; + } + set hass(hass) { this._hass = hass; this.get_data(hass).then(() => { - const oldlen = this.entities.length; - this.entities = this.get_entities() || []; if(this.card) { this.card.hass = this._hass; - this.card.setConfig({entities: this.entities, ...this._config.card}); } - if(this.entities.length != oldlen) this.requestUpdate(); + + const oldEntities = this.entities.map((e) => e.entity); + this.entities = this.get_entities() || []; + const newEntities = this.entities.map((e) => e.entity); + + if(!this._compare_arrays(oldEntities, newEntities)) { + this.card.setConfig({entities: this.entities, ...this._config.card}); + this.requestUpdate(); + } }); }