-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
fix: always mount listeners in useStorage #4730
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
Conversation
This is _yet another_ possible fix for the various `useStorage` scope issues. Basically, `onMounted` will _always_ run in the scope of the _component_. This means the event listener will not be torn down unless the element itself is. When the various effect scopes we use are torn down, the listener will remain and cause various memory leaks. We can solve this by moving the event listeners _outside_ the `onMounted` callback, as they will then teardown when the scope they're in disposes. However, this then means the event listener could run before the component has mounted, when `initOnMount` is true. To solve this, we check in the event handler and do nothing if it isn't mounted yet. We only care about the _first_ mount, since all others will have already gone through at least one `update` call.
tryOnMounted(() => { | ||
firstMounted = true | ||
update() | ||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is needed for SSR maybe? Why the hooks need to be added on mount in the first place?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i'm just maintaining whatever was there before
there's an initOnMounted
option, that's why this is a thing. don't know why that is an option though
This is yet another possible fix for the various
useStorage
scope issues.Basically,
onMounted
will always run in the scope of the component. This means the event listener will not be torn down unless the element itself is. When the various effect scopes we use are torn down, the listener will remain and cause various memory leaks.We can solve this by moving the event listeners outside the
onMounted
callback, as they will then teardown when the scope they're in disposes.However, this then means the event listener could run before the component has mounted, when
initOnMount
is true. To solve this, we check in the event handler and do nothing if it isn't mounted yet.We only care about the first mount, since all others will have already gone through at least one
update
call.Before submitting the PR, please make sure you do the following
fixes #123
).