Skip to content

Commit

Permalink
fix: Only run Component initialization functions once
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
AtkinsSJ committed May 10, 2024
1 parent cf605c8 commit 5b43358
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/util/Component.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import ValueHolder from "./ValueHolder.js";

export class Component extends HTMLElement {
#has_created_element = false;
#has_called_on_ready = false;

// Render modes
static NO_SHADOW = Symbol('no-shadow');

Expand Down Expand Up @@ -86,12 +89,18 @@ export class Component extends HTMLElement {
}

connectedCallback () {
this.on_ready && this.on_ready(this.get_api_());
if (!this.#has_called_on_ready) {
this.on_ready && this.on_ready(this.get_api_());
this.#has_called_on_ready = true;
}
}

attach (destination) {
const el = this.create_element_();
this.dom_.appendChild(el);
if (!this.#has_created_element) {
const el = this.create_element_();
this.dom_.appendChild(el);
this.#has_created_element = true;
}

if ( destination instanceof HTMLElement ) {
destination.appendChild(this);
Expand Down

0 comments on commit 5b43358

Please sign in to comment.