Skip to content

Commit 5b43358

Browse files
committed
fix: Only run Component initialization functions once
If the Component gets removed from the DOM and then re-added, it already has contents, and we don't need to create them again. It also has already had on_ready called, so that doesn't need to happen again either. This fix stops Components duplicating their content elements and listener callbacks whenever they're moved around the document.
1 parent cf605c8 commit 5b43358

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

src/util/Component.js

+12-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import ValueHolder from "./ValueHolder.js";
22

33
export class Component extends HTMLElement {
4+
#has_created_element = false;
5+
#has_called_on_ready = false;
6+
47
// Render modes
58
static NO_SHADOW = Symbol('no-shadow');
69

@@ -86,12 +89,18 @@ export class Component extends HTMLElement {
8689
}
8790

8891
connectedCallback () {
89-
this.on_ready && this.on_ready(this.get_api_());
92+
if (!this.#has_called_on_ready) {
93+
this.on_ready && this.on_ready(this.get_api_());
94+
this.#has_called_on_ready = true;
95+
}
9096
}
9197

9298
attach (destination) {
93-
const el = this.create_element_();
94-
this.dom_.appendChild(el);
99+
if (!this.#has_created_element) {
100+
const el = this.create_element_();
101+
this.dom_.appendChild(el);
102+
this.#has_created_element = true;
103+
}
95104

96105
if ( destination instanceof HTMLElement ) {
97106
destination.appendChild(this);

0 commit comments

Comments
 (0)