Skip to content

Commit

Permalink
Try to improve icon styling performance.
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasloven committed Jan 8, 2024
1 parent e33c663 commit 4de5148
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 11 deletions.
2 changes: 1 addition & 1 deletion card-mod.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "card-mod",
"private": true,
"version": "3.4.1",
"version": "3.4.2",
"description": "",
"scripts": {
"build": "rollup -c",
Expand Down
2 changes: 1 addition & 1 deletion src/helpers/apply_card_mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export async function apply_card_mod_compatible(
(window as any).cm_compatibility_warning = true;
console.groupCollapsed("Card-mod warning");
console.info(
"You are using a custom card which reiles on card-mod, and uses an outdated signature for <code>applyElement</code>."
"You are using a custom card which relies on card-mod, and uses an outdated signature for applyToElement."
);
console.info(
"The outdated signature will be removed at some point in the future. Hopefully the developer of your card will have updated their card by then."
Expand Down
19 changes: 13 additions & 6 deletions src/patch/ha-icon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const updateIcon = (el) => {
if (filter === "none") el.style.filter = "none";
};

const bindCardMod = async (el, retry = 0) => {
const bindCardMod = async (el) => {
// Find the most relevant card-mods in order to listen to change events so we can react quickly

updateIcon(el);
Expand All @@ -40,32 +40,39 @@ const bindCardMod = async (el, retry = 0) => {
}

// Find card-mod elements created later, increased interval
if (retry < 5) {
return window.setTimeout(() => bindCardMod(el, retry + 1), 250 * retry);
if (el.cm_retries < 5) {
el.cm_retries++;
return window.setTimeout(() => bindCardMod(el), 250 * el.cm_retries);
}
};

@patch_element("ha-state-icon")
class HaStateIconPatch extends ModdedElement {
firstUpdated(_orig, ...args) {
cm_retries = 0;
updated(_orig, ...args) {
_orig?.(...args);
this.cm_retries = 0;
bindCardMod(this);
}
}

@patch_element("ha-icon")
class HaIconPatch extends ModdedElement {
firstUpdated(_orig, ...args) {
cm_retries = 0;
updated(_orig, ...args) {
_orig?.(...args);
this.cm_retries = 0;
bindCardMod(this);
}
}

@patch_element("ha-svg-icon")
class HaSvgIconPatch extends ModdedElement {
firstUpdated(_orig, ...args) {
cm_retries = 0;
updated(_orig, ...args) {
_orig?.(...args);
if ((this.parentNode as any)?.host?.localName === "ha-icon") return;
this.cm_retries = 0;
bindCardMod(this);
}
}
Expand Down

0 comments on commit 4de5148

Please sign in to comment.