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

Handle non-unique elements in Mutation Observer (jest only?) #186

Open
JRJurman opened this issue May 9, 2022 · 1 comment
Open

Handle non-unique elements in Mutation Observer (jest only?) #186

JRJurman opened this issue May 9, 2022 · 1 comment

Comments

@JRJurman
Copy link
Member

JRJurman commented May 9, 2022

Summary

While writing the tests for #185, it was found that sometimes the mutation observer will get a duplicate of an element (usually when it is now catching up to multiple events that happened in the app).

I believe this is something that only happens in tests, and possible a side-effect of how jest / jsdom / testing-library work.

If that isn't the case, we should update the logic here:

new MutationObserver((mutationList) => {
// cleanup orphaned nodes that are no longer on the DOM
const removedNodesInMutation = (mutation: MutationRecord) => [...mutation.removedNodes];
const removedNodes = mutationList.flatMap(removedNodesInMutation).flatMap(childrenComponents);
removedNodes.forEach(clearNode);
// call new effects on any new nodes
const addedNodesInMutation = (mutation: MutationRecord) => [...mutation.addedNodes];
const newNodes = mutationList.flatMap(addedNodesInMutation).flatMap(childrenComponents);
newNodes.forEach(processEffects);
});

We should see this happening in apps after #185 and we see useStores hooks not cleaning up when they have been removed.

@JRJurman
Copy link
Member Author

JRJurman commented May 9, 2022

If this doesn't happen in apps, it is easy enough to work around in the testing, and we should avoid writing unique logic to handle it.

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

No branches or pull requests

1 participant