-
Notifications
You must be signed in to change notification settings - Fork 546
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
Expose onInvalidate
on computed
.
#163
Comments
I am not sure if it's the right design. To me, as a primitive building block, computed evokes pure functions: caching a computation based on dependencies. This use-case is clearly a value that needs cleanup, which is a rare thing in JS. function disposableComputed(getter, dispose) {
let value;
return customRef((track, trigger) => {
watchEffect(onInvalidate => {
onInvalidate(() => dispose(value))
value = getter()
trigger()
})
return { get() { track(); return value } }
})
} Use it like this: function blobToUrl(blob) {
return disposableComputed(
() => URL.createObjectURL(unref(blob)),
url => URL.revokeObjectURL(url),
)
) |
True! It can be done in user land and is pretty simple to do so. I guess you can do with just const invalidableComputed = (getter) => {
const r = ref()
watchEffect(
onInvalidate => {
r.value = getter(onInvalidate)
},
{ flush: 'pre' }, // not sure if this is a good use of flush lol never really need it
)
return readonly(r)
}
Ya. I agree with you that computed is prettier and more ideal when it is pure. But it will miss out quite a few real-life use cases and forcing user to create pattern like the above. Adding |
Yes. Beyond "ideal" design, you're still free to have side-effects in a computed. What makes your use-case special is that you need to catch when the effect (computed or watch) stops for the final cleanup. That's the one special thing BTW, function blobToUrl(blob) {
let url;
const result = computed(() => {
URL.revokeObjectURL(url);
return url = URL.createObjectURL(url);
})
result.effect.onStop = () => URL.revokeObjectURL(url);
return result;
} That's an alternative way of writing the |
There is even a test for this: I am not so sure about if what you are saying about
Hmmm. Can't find anything regarding this in the doc. It is probably not intended for public use. I'll leave this issue open for now. |
Thanks for pointing this out to me! You are right, it works 100%.
I can't say, maybe someone from Vue team can clarify. For some apis it's a bit unclear if they are public or not, given that the docs are not final. I think it might be public because, as of
Sure do! I'm just pointing out some ideas, not trying to shut you down 😄. |
@yyx990803 just wanna ping you in case you missed this. 😄 |
I bumped in to a situation where I would need to invalidate some computed value. I could do this currently using watch
But ideally it would be nice if this invalidation logic is encapsulated in the computed.
My specific situation is that I am trying to create an object url as a computed value.
It would be quite useful if computed expose
onInvalidate
so I can write:The text was updated successfully, but these errors were encountered: