Skip to content

Commit

Permalink
tweak implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
calebporzio committed Jan 21, 2024
1 parent c68aba0 commit 3196c6a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 18 deletions.
20 changes: 7 additions & 13 deletions packages/alpinejs/src/lifecycle.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,18 +82,13 @@ export function interceptInit(callback) { initInterceptors.push(callback) }
export function initTree(el, walker = walk, intercept = () => {}) {
deferHandlingDirectives(() => {
walker(el, (el, skip) => {
if (!el._x_isInit) {
intercept(el, skip)

initInterceptors.forEach(i => i(el, skip))
directives(el, el.attributes).forEach(handle => handle())
}

if (el._x_ignore) {
skip()
} else {
el._x_isInit = true
}
intercept(el, skip)

initInterceptors.forEach(i => i(el, skip))

directives(el, el.attributes).forEach(handle => handle())

el._x_ignore && skip()
})
})
}
Expand All @@ -102,6 +97,5 @@ export function destroyTree(root) {
walk(root, el => {
cleanupAttributes(el)
cleanupElement(el)
delete el._x_isInit
})
}
11 changes: 6 additions & 5 deletions packages/alpinejs/src/mutation.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,17 +119,17 @@ function onMutate(mutations) {
return
}

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

for (let i = 0; i < mutations.length; i++) {
if (mutations[i].target._x_ignoreMutationObserver) continue

if (mutations[i].type === 'childList') {
mutations[i].addedNodes.forEach(node => node.nodeType === 1 && addedNodes.push(node))
mutations[i].removedNodes.forEach(node => node.nodeType === 1 && removedNodes.push(node))
mutations[i].addedNodes.forEach(node => node.nodeType === 1 && addedNodes.add(node))
mutations[i].removedNodes.forEach(node => node.nodeType === 1 && removedNodes.add(node))
}

if (mutations[i].type === 'attributes') {
Expand Down Expand Up @@ -171,10 +171,11 @@ function onMutate(mutations) {
onAttributeAddeds.forEach(i => i(el, attrs))
})

console.log(removedNodes, addedNodes)
for (let node of removedNodes) {
// If an element gets moved on a page, it's registered
// as both an "add" and "remove", so we want to skip those.
if (addedNodes.includes(node)) continue
if (addedNodes.has(node)) continue

onElRemoveds.forEach(i => i(node))

Expand Down

0 comments on commit 3196c6a

Please sign in to comment.