Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/panels/lovelace/create-element/create-element-base.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import deepClone from "deep-clone-simple";

import {
LovelaceCardConfig,
LovelaceBadgeConfig,
Expand Down Expand Up @@ -36,11 +38,11 @@ const _createElement = <T extends keyof CreateElementConfigTypes>(
) as CreateElementConfigTypes[T]["element"];
try {
// @ts-ignore
element.setConfig(config);
element.setConfig(deepClone(config));
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We shouldn't do this. This hurts our performance because cards are doing the incorrect thing.

Instead we should use Object.freeze and let custom cards crash so they fix their bad behavior. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/freeze

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should freeze it as soon as we load the config.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Object.freeze() is shallow, so that would be a recursive process. But I guess it's better to do it once...
Skärmavbild 2020-02-12 kl  08 45 53

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, we should deep freeze. Deep freeze example code is on the docs I linked :) https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/freeze#What_is_shallow_freeze

Copy link
Copy Markdown
Contributor Author

@thomasloven thomasloven Feb 13, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤦‍♂ 🙈

} catch (err) {
// tslint:disable-next-line
console.error(tag, err);
return _createErrorElement(err.message, config);
return _createErrorElement(err.message, deepClone(config));
}
return element;
};
Expand Down