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 bug when mutation causes controller to be registered twice #97

Merged
merged 1 commit into from
Feb 22, 2022

Conversation

lewispb
Copy link
Contributor

@lewispb lewispb commented Feb 21, 2022

Partially reverts 67e2cf3#diff-8e0a8f9875453f690405e8c037a8e76c4436f09f47fd9c605283c2f16bd95789L77

Resolves an issue where a controller is registered twice.

I created a sample Rails app to reproduce this issue. Relevant commit: lewispb/stimulus-loader-issue@ce8aaa5

To reproduce:

  1. Insert a Turbo Stream element onto the page which in turn appends an element specifying a Stimulus controller
  2. It appears the mutation observer triggers twice, once for the HTML element mutating (as the Turbo Stream element is inserted into the dom) and once for the append performed by the Turbo Stream action.
  3. This triggers registering the Stimulus controller twice, and that has the effect of immediately disconnecting the controller

Before

The first inserted element is removed as the disconnect function is immediately called. In our application this is a flash message.

before.mov

After

Elements are removed correctly, after the timeout.

after.mov

@dhh
Copy link
Member

dhh commented Feb 22, 2022

I take that this is dealing with a race condition when the same controller is listed twice? Because otherwise the existing guards should take care of it. Anyway, all good.

@dhh dhh merged commit c58b5c4 into hotwired:main Feb 22, 2022
@lewispb
Copy link
Contributor Author

lewispb commented Feb 22, 2022

Yeah, but manually appending multiple elements with the same controller to the DOM didn’t reproduce. Requires turbo stream to reproduce and I ran out of time trying to understand exactly why.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants