From 991b651a4e04d12a3339557ac9eddea0702d7bbf Mon Sep 17 00:00:00 2001 From: Alex Kanunnikov Date: Mon, 15 Jan 2024 11:48:04 +0300 Subject: [PATCH] Revert "+" This reverts commit bec34f0498e03d531fcccbde119276cfab131d7d. --- src/utils/component.ts | 62 ++++++++++++++++++++++-------------------- 1 file changed, 32 insertions(+), 30 deletions(-) diff --git a/src/utils/component.ts b/src/utils/component.ts index e57a6760..34e6f1c4 100644 --- a/src/utils/component.ts +++ b/src/utils/component.ts @@ -249,44 +249,46 @@ export async function destroyElement( | null | null[], ) { - // Flatten the array if it's an array of components - const components = Array.isArray(component) ? component.flat() : [component]; - - const destructors: Array> = []; - const nodesToDestroy: Set = new Set(); - - for (const item of components) { - if (item === null) continue; - - if ($nodes in item) { - if (item.ctx) { - runDestructors(item.ctx, destructors); + if (Array.isArray(component)) { + await Promise.all(component.map((component) => destroyElement(component))); + } else { + if (component === null) { + return; + } + if ($nodes in component) { + const destructors: Array> = []; + if (component.ctx) { + runDestructors(component.ctx, destructors); } - - const nodes = item[$nodes]; + const nodes = component[$nodes]; let startNode: null | Node = nodes[0]; - const endNode = nodes.length > 1 ? nodes[nodes.length - 1] : null; - if (endNode !== null) { - // Collect nodes to destroy - while (startNode && startNode !== endNode) { - nodesToDestroy.add(startNode); - startNode = startNode.nextSibling; - } - if (endNode !== null) { + const endNode = + nodes.length === 1 ? null : nodes[nodes.length - 1] || null; + const nodesToDestroy = new Set(nodes); + while (true && endNode !== null) { + startNode = startNode.nextSibling; + if (startNode === null) { + break; + } else if (startNode === endNode) { nodesToDestroy.add(endNode); + break; + } else { + nodesToDestroy.add(startNode); } - } else { - nodesToDestroy.add(startNode); + } + await Promise.all(destructors); + try { + await Promise.all(Array.from(nodesToDestroy).map(destroyNode)); + } catch (e) { + console.warn( + `Woops, looks like node we trying to destroy no more in DOM`, + e, + ); } } else { - nodesToDestroy.add(item[$node]); + await destroyNode(component[$node]); } } - - // Await all destructors - await Promise.all(destructors); - // Destroy all nodes - await Promise.all(Array.from(nodesToDestroy).map(destroyNode)); } var $newDestructors = new WeakMap();