-
-
Notifications
You must be signed in to change notification settings - Fork 255
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
Feature request: Watch #149
Comments
It would be nice if we could come up with the api without changing the core to cover the use case. import { watch } from 'valtio/utils' |
I'm not sure how you would be able to track dependencies while reading them without changing the core tho. I don't mind if this feature is part of another major release. |
You may not like this, but this would work. import { watch } from 'valtio/utils'
const stop = watch((get) => {
// We mark `count` as our dependency. This
// callback will then only run whenever count updates.
console.log(`Count: ${get(count).value}`);
return () => {
// We may perform cleanups here when `stop` is called.
};
}); With this little change, we can implement it without changing the core, which is a huge win for me. |
Loved this! |
Does watch make the current subscribe feature obsolete? |
The primary use case and the motivation of valtio is React (and its concurrent mode) and |
It doesn't. Even though both functions can "listen" to proxies, the timing of execution is different:
I agree, without changing the core, this by far the closest implementation we can get. |
#149 (comment) |
I would probably create an implementation this weekend 😅 |
Thanks for the implementation @lxsmnsyc. The code looks dope. One question though. Why would I use I understand that they're different, but I can't see any use case that |
(Seems like you already reached the conclusion?) I would use |
I just want to be sure that I did misunderstand the
Indeed. But are they really that different in performance? I don't how to compare it. |
Well, you would need to measure them to know the real performance. |
Already asked by @AjaxSolutions,
|
watch
(pending name) is a proposed function exportable fromvaltio
that allows automatically tracking valtio instances instead of explicitly declaring thevaltio
instances you need to watch (subscribe) for. This is similar touseEffect
except that you don't have to explicitly define the dependency list.This proposed function allows you to easily compose subscriptions to
valtio
instances and not to individually watch them (subscribe
).Example
Multiple subscriptions:
The text was updated successfully, but these errors were encountered: