From 6ac065462a68560c5739652d956ff7b98680af84 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 | 33 +++++++++++-------- 1 file changed, 19 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..f455037b 100644 --- a/core/static/bundled/core/components/include-index.ts +++ b/core/static/bundled/core/components/include-index.ts @@ -61,7 +61,25 @@ 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)) { + // Creating a base class would be really complicated, we can use any ElementOnce + // element instead to cast, it doesn't matter + const node = element as LinkOnce; + + // 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 +87,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); }