watchEffect should allow multiple onInvalidate calls #3341
Labels
has PR
A pull request has already been submitted to solve the issue
scope: reactivity
✨ feature request
New feature or request
What problem does this feature solve?
Right now each effect 'remembers' only the last callback passed to
onInvalidate
:the first
console.log
won't ever be called. While in this example it's trivial to shove bothconsole.log
s to a single cleanup function it can get pretty hairy when the effect gets complex.My exact use case was reacting to audio device selection changes by creating multiple MediaStream instances conditionally and hooking them up with some other parts of the app. Since MediaStreams are created conditionally I had several if statements and each of them called
onInvalidate
passing some cleanup function. I was surprised that some resources weren't properly released when I changed the devices.This feature request doesn't require any end user facing API changes, rather changing of Vue internals to keep track of multiple callbacks instead of just the last one.
In the meantime I solved my use case by creating a crude wrapper around
watchEffect
that does what I need. While it was easy to implement this workaround I think it would greatly improvewatchEffect
ergonomics if we had it in Vue out of the box.What does the proposed API look like?
no changes
The text was updated successfully, but these errors were encountered: