Skip to content

Commit

Permalink
Revert "+"
Browse files Browse the repository at this point in the history
This reverts commit bec34f0.
  • Loading branch information
lifeart committed Jan 15, 2024
1 parent ccf9972 commit 991b651
Showing 1 changed file with 32 additions and 30 deletions.
62 changes: 32 additions & 30 deletions src/utils/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Promise<void>> = [];
const nodesToDestroy: Set<Node> = 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<Promise<void>> = [];
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<any, Destructors>();
Expand Down

0 comments on commit 991b651

Please sign in to comment.