Skip to content

Commit

Permalink
handle pantry only store persistentIds
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelWest22 committed Dec 19, 2024
1 parent b33d7b5 commit a8efad7
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions src/idiomorph.js
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ var Idiomorph = (function () {

// if we are at the end of the exiting parent's children, just append
if (insertionPoint == null) {
if (ctx.pantry && ctx.persistentIds.has(newChild.id)) {
if (ctx.pantry && ctx.persistentIds.has(/** @type {Element} */ (newChild).id)) {
oldParent.appendChild(newChild);
removeIdsFromConsideration(ctx, newChild);
continue;
Expand Down Expand Up @@ -393,7 +393,7 @@ var Idiomorph = (function () {

// abandon all hope of morphing, just insert the new child before the insertion point
// and move on
if (ctx.pantry && ctx.persistentIds.has(newChild.id)) {
if (ctx.pantry && ctx.persistentIds.has(/** @type {Element} */ (newChild).id)) {
oldParent.insertBefore(newChild, insertionPoint);
removeIdsFromConsideration(ctx, newChild);

Expand Down Expand Up @@ -1100,7 +1100,8 @@ var Idiomorph = (function () {
// places where tempNode may be just a Node, not an Element
function removeNode(tempNode, ctx) {
removeIdsFromConsideration(ctx, tempNode)
if (ctx.pantry && ctx.persistentIds.has(tempNode.id)) {
// @ts-ignore - use new set intersection feature
if (ctx.pantry && ctx.idMap.get(tempNode)?.intersection(ctx.persistentIds).size > 0 && tempNode instanceof Element) {
moveToPantry(tempNode, ctx);
} else {
if (ctx.callbacks.beforeNodeRemoved(tempNode) === false) return;
Expand All @@ -1118,14 +1119,18 @@ var Idiomorph = (function () {
if (ctx.pantry instanceof HTMLDivElement) {
// If the node is a leaf (no children), process it, and then we're done
if (!node.hasChildNodes()) {
if (node.id) {
if (ctx.persistentIds.has(node.id)) {
// @ts-ignore - use proposed moveBefore feature
if (ctx.pantry.moveBefore) {
// @ts-ignore - use proposed moveBefore feature
ctx.pantry.moveBefore(node, null);
} else {
ctx.pantry.insertBefore(node, null);
}
} else {
if (ctx.callbacks.beforeNodeRemoved(node) === false) return;
node.parentNode?.removeChild(node);
ctx.callbacks.afterNodeRemoved(node);
}

// otherwise we need to process the children first
Expand All @@ -1135,11 +1140,13 @@ var Idiomorph = (function () {
});

// After processing children, process the current node
if (node.id) {
if (ctx.persistentIds.has(node.id)) {
node.innerHTML = '';
ctx.pantry.appendChild(node);
} else {
if (ctx.callbacks.beforeNodeRemoved(node) === false) return;
node.parentNode?.removeChild(node);
ctx.callbacks.afterNodeRemoved(node);
}
}
}
Expand Down

0 comments on commit a8efad7

Please sign in to comment.