-
-
Notifications
You must be signed in to change notification settings - Fork 8.5k
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
Option to not stop watch when component is unmounted #1532
Comments
I think some mechanism for this would be useful for Vuex as well |
So in this case, when do you expect it to be stopped? It seems you will need to manually implement a reference count in order to prevent the watcher from running indefinitely (and potentially lead to memory leaks) |
Not stopped at all by Vue. The data is essentially global and lives forever. I could run
|
Watch, watchEffect and computed from vue are wrappers around the lower-level primitives You can import those to create watches that stop whenever you want. |
Converting this into a higher-order function with export const createEffectComposable = (ref = ref(null)) => {
const _effect = effect(() => {
// ...
})
const _stop = () => stop(effect)
return () => { ref, stop: _stop }
} export const useFoo = createEffectComposable() comp1 = {
setup() {
const { ref, stop } = useFoo();
}
}
comp2 = {
setup() {
const { ref, stop } = useFoo();
}
} |
composition-api has a solution for this under name
watch is 300+ lines "wrapper" which is a little bit too much for this task. |
@vmihailenco It does a lot of things. |
RFC #212 provides a solution for this. |
Closed, track this in vuejs/rfcs#212 |
What problem does this feature solve?
I have a composition function that is shared between multiple components:
The problem is that watch is stopped when the component that called
useFoo
first is unmounted.What does the proposed API look like?
An option for watch
Or a helper that nulls the current instance:
Or export
getCurrentInstance
andsetCurrentInstance
so this can be solved in user space.The text was updated successfully, but these errors were encountered: