Skip to content

Commit

Permalink
Merge pull request #4 from catdad-experiments/3-better-loading-handling
Browse files Browse the repository at this point in the history
better handling of re-rendering the card
  • Loading branch information
catdad committed Apr 1, 2024
2 parents d2c8403 + 4fe8c7c commit ff0e9b6
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions src/combined-card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { NAME, EDITOR_NAME, HELPERS, LOG, loadStackEditor } from './utils';
@customElement(NAME)
class CombinedCard extends LitElement implements LovelaceCard {
@state() private _config?: LovelaceCardConfig;
@state() private _helpers?;

private _card?: LovelaceCard;
private _hass?: HomeAssistant;
private _editMode: boolean = false;
Expand Down Expand Up @@ -70,17 +72,18 @@ class CombinedCard extends LitElement implements LovelaceCard {
this._config = config;
const that = this;

if (!HELPERS.loaded) {
if (HELPERS.loaded) {
this._helpers = HELPERS.helpers;
} else {
HELPERS.whenLoaded.then(() => {
LOG('re-rendering card after helpers have loaded');
that._config = { ...(that._config || config) };
that.render();
that._helpers = HELPERS.helpers;
});
}
}

protected render() {
if (!this._config || !HELPERS.loaded) {
if (!(this._config && this._helpers)) {
LOG(`Rendering card: { config: ${!!this._config}, helpers: ${HELPERS.loaded} }`);
return this._loading();
}
Expand Down Expand Up @@ -114,11 +117,12 @@ class CombinedCard extends LitElement implements LovelaceCard {
}

private _createCard(config: LovelaceCardConfig): LovelaceCard {
if (!HELPERS.loaded) {
// TODO does this need to be removed?
if (!this._helpers) {
return this._loading();
}

const element: LovelaceCard = HELPERS.helpers.createCardElement({
const element: LovelaceCard = this._helpers.createCardElement({
...config,
type: 'vertical-stack'
});
Expand Down Expand Up @@ -156,6 +160,8 @@ class CombinedCard extends LitElement implements LovelaceCard {
this._rebuildCard(cardElToReplace, config);
}

// TODO is this a mistake? should we just re-render the card instead?
// I don't actually remember when this does execute
private _rebuildCard(
cardElToReplace: LovelaceCard,
config: LovelaceCardConfig
Expand Down

0 comments on commit ff0e9b6

Please sign in to comment.