From a0cce91bd5d73a5a25ff465f280a6729219b553e Mon Sep 17 00:00:00 2001 From: Sli Date: Tue, 31 Mar 2026 17:42:35 +0200 Subject: [PATCH] Simplify ElementOnce observer --- .../bundled/core/components/include-index.ts | 34 +++++++++++-------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/core/static/bundled/core/components/include-index.ts b/core/static/bundled/core/components/include-index.ts index 657c3127..028082e4 100644 --- a/core/static/bundled/core/components/include-index.ts +++ b/core/static/bundled/core/components/include-index.ts @@ -61,7 +61,26 @@ const startObserver = (observer: MutationObserver) => { }); }; -// Refresh *-once components when changes happens +// Refresh all ElementOnce components on the document based on their tag name +// They should all be be extended from the `elementOnce` factory +const refreshElement = (componentName: string, tagName: string) => { + for (const element of document.getElementsByTagName(componentName)) { + const node = element as unknown as { + inheritedTagName: string; + refresh(): null; + }; + + // We can't guess if an element is compatible before we get one + // We exit the function completely if it's not compatible + if (node.inheritedTagName.toUpperCase() !== tagName.toUpperCase()) { + return; + } + + node.refresh(); + } +}; + +// Refresh ElementOnce components when changes happens const observer = new MutationObserver((mutations: MutationRecord[]) => { observer.disconnect(); for (const mutation of mutations) { @@ -69,19 +88,6 @@ const observer = new MutationObserver((mutations: MutationRecord[]) => { if (node.nodeType !== node.ELEMENT_NODE) { continue; } - const refreshElement = (componentName: string, tagName: string) => { - for (const element of document.getElementsByTagName(componentName)) { - // We can't guess if an element is compatible before we get one - // We exit the function completely if it's not compatible - if ( - (element as any).inheritedTagName.toUpperCase() !== tagName.toUpperCase() - ) { - return; - } - - (element as any).refresh(); - } - }; for (const registered of registeredComponents) { refreshElement(registered, (node as HTMLElement).tagName); }