Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize mutation observer to better handle move operations - ref #4450 #4451

Merged
merged 1 commit into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 19 additions & 11 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,25 @@
<!-- <script src="//cdn.tailwindcss.com"></script> -->
<!-- <script src="//cdn.tailwindcss.com"></script> -->

<div x-data x-sort>
<div x-sort:item >foo</div>
<div >foo</div>
<div x-sort:item >foo</div>
</div>
<script>
let count = 0;
document.addEventListener("alpine:init", () => {
Alpine.directive("test", (el, _, { cleanup }) => {
el.textContent = "Initialized";
cleanup(() => {
el.textContent = "Cleaned up";
});
// Alpine.nextTick(() => {
// el.parentNode.replaceChildren(el);
// })
});
});
</script>
<div x-data>
<div x-ref="anchor"></div>

<div x-ref="target" class="bg-red-300 w-32 h-32" x-test></div>

<div x-data="{ val: true }"
>
<input type="text" x-model.boolean="val">
<input type="checkbox" x-model.boolean="val">
<input type="radio" name="foo" value="true" x-model.boolean="val">
<input type="radio" name="foo" value="false" x-model.boolean="val">
<button @click="$refs.anchor.before($refs.target)">Move</button>
</div>
</html>
20 changes: 17 additions & 3 deletions packages/alpinejs/src/mutation.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,14 +112,16 @@ export function flushAndStopDeferringMutations() {
}

function onMutate(mutations) {
console.log(mutations);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@calebporzio you forgot a console.log in the code here ;)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@levu42 thanks! He removed it after in a WIP commit 😆


if (isCollecting) {
deferredMutations = deferredMutations.concat(mutations)

return
}

let addedNodes = []
let removedNodes = []
let removedNodes = new Set
let addedAttributes = new Map
let removedAttributes = new Map

Expand All @@ -129,14 +131,26 @@ function onMutate(mutations) {
if (mutations[i].type === 'childList') {
mutations[i].removedNodes.forEach(node => {
if (node.nodeType !== 1) return

// No need to process removed nodes that haven't been initialized by Alpine...
if (! node._x_marker) return

removedNodes.push(node)
removedNodes.add(node)
})

mutations[i].addedNodes.forEach(node => {
if (node.nodeType !== 1) return

// If the node is a removal as well, that means it's a "move" operation and we'll leave it alone...
if (removedNodes.has(node)) {
removedNodes.delete(node)

return
}

// If the node has already been initialized, we'll leave it alone...
if (node._x_marker) return;

addedNodes.push(node)
})
}
Expand Down Expand Up @@ -197,7 +211,7 @@ function onMutate(mutations) {

for (let node of addedNodes) {
if (! node.isConnected) continue
if (node._x_marker) return;

onElAddeds.forEach(i => i(node))
}

Expand Down