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

Dispose @snapshottedView reactions when destroyed #105

Merged
merged 1 commit into from
Jul 3, 2024

Conversation

scott-rc
Copy link
Contributor

@scott-rc scott-rc commented Jul 2, 2024

While adding @snapshottedView to Gadget, I encountered an issue where the reaction we set up to track @snapshottedViews was still running after we detached a model from the tree:

[mobx] Encountered an uncaught exception that was thrown by a reaction or observer component, in: 'Reaction[Reaction@998]' Error: [mobx-state-tree] You are trying to read or write to an object that is no longer part of a state tree. (Object type: 'DataModel', Path upon death: '/models/DataModel-xyz', Subpath: 'type', Action: ''). Either detach nodes first, or don't use objects after removing / replacing them in the tree.

This PR disposes the reactions after the node is destroyed so we avoid this error.

Copy link
Contributor

@airhorns airhorns left a comment

Choose a reason for hiding this comment

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

Ah, of course, oops! I think that just checking aliveness in the reaction works, but it means the reaction won't ever get GC'd. I think it'd be better to instead tear it down explicitly when the node is destroyed. I think we can use one of the other lifecycle hooks for that. reaction returns a disposer function that you would need to call in the destroy lifecycle hook.

@scott-rc scott-rc force-pushed the sc/skip-reaction-when-detached branch from 21e08f7 to 5ba0951 Compare July 3, 2024 14:25
@scott-rc scott-rc requested a review from airhorns July 3, 2024 14:27
Copy link

codspeed-hq bot commented Jul 3, 2024

CodSpeed Performance Report

Merging #105 will degrade performances by 48.88%

Comparing sc/skip-reaction-when-detached (5ba0951) with main (0ab634a)

Summary

⚡ 1 improvements
❌ 2 regressions
✅ 8 untouched benchmarks

⚠️ Please fix the performance issues or acknowledge them on CodSpeed.

Benchmarks breakdown

Benchmark main sc/skip-reaction-when-detached Change
instantiating a large root 2.5 ms 4.8 ms -48.88%
accessing unmemoized getter properties of a class model 15.3 µs 19 µs -19.56%
accessing unmemoized null property of a class model 12.2 µs 11 µs +11.44%

@scott-rc scott-rc changed the title Skip @snapshottedView reaction when detached Dispose @snapshottedView reactions when destroyed Jul 3, 2024
@scott-rc scott-rc merged commit d311c44 into main Jul 3, 2024
5 of 6 checks passed
@scott-rc scott-rc deleted the sc/skip-reaction-when-detached branch July 3, 2024 15:05
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

Successfully merging this pull request may close these issues.

3 participants