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

Fix components instantiated twice because of complex mutations #2376

Merged
merged 3 commits into from
Nov 19, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 21 additions & 1 deletion packages/alpinejs/src/mutation.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,13 +160,33 @@ function onMutate(mutations) {
onAttributeAddeds.forEach(i => i(el, attrs))
})

// Mutations are undled together by the browser but sometimes
// for complex cases, there may be javascript code adding a wrapper
// and then an alpine component as a child of that wrapper in the same
// function and the mutation observer will receive 2 different mutations.
// when it comes to run them, the dom contains both changes so the child
// element would be processed twice as Alpine calls initTree on
// both mutations. We mark all node as _x_ignored and only remove the flag
// when processing the node to avoid those duplicates.
addedNodes.forEach((node) => {
node._x_ignoreSelf = true
node._x_ignore = true
})
for (let node of addedNodes) {
// If an element gets moved on a page, it's registered
// If an element gets moved on a page, it's registered
// as both an "add" and "remove", so we want to skip those.
if (removedNodes.includes(node)) continue

delete node._x_ignoreSelf
delete node._x_ignore
onElAddeds.forEach(i => i(node))
node._x_ignore = true
node._x_ignoreSelf = true
}
addedNodes.forEach((node) => {
delete node._x_ignoreSelf
delete node._x_ignore
})

for (let node of removedNodes) {
// If an element gets moved on a page, it's registered
Expand Down
28 changes: 28 additions & 0 deletions tests/cypress/integration/mutation.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,31 @@ test('can pause and queue mutations for later resuming/flushing',
get('h1').should(haveText('3'))
}
)

test('does not initialise components twice when contained in multiple mutations',
html`
<div x-data="{
foo: 0,
bar: 0,
test() {
container = document.createElement('div')
this.$root.appendChild(container)
alpineElement = document.createElement('span')
alpineElement.setAttribute('x-data', '{init() {this.bar++}}')
alpineElement.setAttribute('x-init', 'foo++')
container.appendChild(alpineElement)
}
}">
<span id="one" x-text="foo"></span>
<span id="two" x-text="bar"></span>
<button @click="test">Test</button>
</div>
`,
({ get }) => {
get('span#one').should(haveText('0'))
get('span#two').should(haveText('0'))
get('button').click()
get('span#one').should(haveText('1'))
get('span#two').should(haveText('1'))
}
)