Skip to content

Commit

Permalink
Fix race condition with picture-elements card custom elements (#18765)
Browse files Browse the repository at this point in the history
  • Loading branch information
vangorra authored Nov 28, 2023
1 parent b6a7581 commit 6134f41
Showing 1 changed file with 35 additions and 9 deletions.
44 changes: 35 additions & 9 deletions src/panels/lovelace/cards/hui-picture-elements-card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,10 @@ class HuiPictureElementsCard extends LitElement implements LovelaceCard {

this._config = config;

this._elements = this._config.elements.map(
(elementConfig: LovelaceElementConfig) => {
const element = createStyledHuiElement(elementConfig);
if (this.hass) {
element.hass = this.hass;
}
return element as LovelaceElement;
}
);
this._elements = config.elements.map((element) => {
const cardElement = this._createElement(element);
return cardElement;
});
}

protected updated(changedProps: PropertyValues): void {
Expand Down Expand Up @@ -165,6 +160,37 @@ class HuiPictureElementsCard extends LitElement implements LovelaceCard {
}
`;
}

private _createElement(
elementConfig: LovelaceElementConfig
): LovelaceElement {
const element = createStyledHuiElement(elementConfig) as LovelaceCard;
if (this.hass) {
element.hass = this.hass;
}
element.addEventListener(
"ll-rebuild",
(ev) => {
ev.stopPropagation();
this._rebuildElement(element, elementConfig);
},
{ once: true }
);
return element;
}

private _rebuildElement(
elToReplace: LovelaceElement,
config: LovelaceElementConfig
): void {
const newCardEl = this._createElement(config);
if (elToReplace.parentElement) {
elToReplace.parentElement.replaceChild(newCardEl, elToReplace);
}
this._elements = this._elements!.map((curCardEl) =>
curCardEl === elToReplace ? newCardEl : curCardEl
);
}
}

declare global {
Expand Down

0 comments on commit 6134f41

Please sign in to comment.