From 8ad0025e3268f1166782bde2fd15e3c42a541f24 Mon Sep 17 00:00:00 2001 From: Emi Matchu Date: Fri, 30 Aug 2024 17:09:39 -0700 Subject: [PATCH] A few more comments and code style refactors for item previews --- app/assets/javascripts/outfit-viewer.js | 93 ++++++++++++++++--------- 1 file changed, 61 insertions(+), 32 deletions(-) diff --git a/app/assets/javascripts/outfit-viewer.js b/app/assets/javascripts/outfit-viewer.js index 19264af3..89c1d790 100644 --- a/app/assets/javascripts/outfit-viewer.js +++ b/app/assets/javascripts/outfit-viewer.js @@ -3,10 +3,12 @@ class OutfitViewer extends HTMLElement { constructor() { super(); - this.#internals = this.attachInternals(); + this.#internals = this.attachInternals(); // for CSS `:state()` } connectedCallback() { + // The `` is connected to the DOM right before its + // children are. So, to engage with the children, wait a tick! setTimeout(() => this.#connectToChildren(), 0); } @@ -67,6 +69,8 @@ class OutfitLayer extends HTMLElement { } disconnectedCallback() { + // When this `` leaves the DOM, stop listening for iframe + // messages, if we were. window.removeEventListener("message", this.#onMessage); } @@ -83,33 +87,40 @@ class OutfitLayer extends HTMLElement { const iframe = this.querySelector("iframe"); if (image) { - // Initialize status based on the image's current `complete` attribute, - // then wait for load/error events to update it further if needed. + // If this is an image layer, track its loading state by listening + // to the load/error events, and initialize based on whether it's + // already `complete` (which it can be if it loaded from cache). this.#setStatus(image.complete ? "loaded" : "loading"); image.addEventListener("load", () => this.#setStatus("loaded")); image.addEventListener("error", () => this.#setStatus("error")); } else if (iframe) { this.iframe = iframe; - // Initialize status to `loading`, and asynchronously request a status - // message from the iframe if it managed to load before this triggers - // (impressive, but I think I've seen it happen!). Then, wait for - // messages or error events from the iframe to update status further if - // needed. + // Initialize status to `loading`, and asynchronously request a + // status message from the iframe if it managed to load before this + // triggers (impressive, but I think I've seen it happen!). Then, + // wait for messages or error events from the iframe to update + // status further if needed. this.#setStatus("loading"); this.#sendMessageToIframe({ type: "requestStatus" }); window.addEventListener("message", (m) => this.#onMessage(m)); - this.iframe.addEventListener("error", () => this.#setStatus("error")); + this.iframe.addEventListener("error", () => + this.#setStatus("error"), + ); } else { - throw new Error(` must contain an or