-
-
Notifications
You must be signed in to change notification settings - Fork 8.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(runtime-core): add watchEffect API
BREAKING CHANGE: replae `watch(fn, options?)` with `watchEffect` The `watch(fn, options?)` signature has been replaced by the new `watchEffect` API, which has the same usage and behavior. `watch` now only supports the `watch(source, cb, options?)` signautre.
- Loading branch information
Showing
8 changed files
with
77 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99a2e18
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.
@yyx990803 What motivated this change?
From a user perspective I think
watch(fn, options?)
is the most useful signature of the two, so I'm a bit sad to see it get the longer namewatchEffect
.Thanks to the beauty of automatic dependency tracking, you tend to just write the code that you want to run automatically and not care about its deps.
Want to paint a canvas? Just write the function that does the painting and call
watchEffect(paint)
. It will repaint automatically whenever anything changes.Want to fetch some server data? Just write the fetch based on your parameters and
watchEffect(fetch)
. It will fetch any time some parameter changes.Want to accept promises as well as raw data in your props? There you go:
Need your own virtual scrolling? I did and this is the core piece:
Bottom line:
watch(source, cb, options?)
is the lesser used one because it makes you manually track your dependencies.It's still useful for that special case where you want to depend on something you won't actually read in the callback; or when you access some reactive state that you don't want to depend on. But it's not the easiest api to use, most of the time
watchEffect
is.99a2e18
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.
Personally I use more the
watch
with source than the currentwatchEffect
.For example to track loading status and then run animations or run external library update when a set of variables are updated.
And I think the code is more readable like
watch(myVariable, then () => {})
99a2e18
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.
@jods4 There are various concerns with this change. One of the most obvious one is that from Vue 2's perspective,
watch
is an existing API andwatchEffect
is something new. Let's say we go the other way around, and havewatch
(eager) +watchSource
(lazy), then we are:watch
to a different name (this would affect thewatch
option in the Options API too).watch
name, but with a different behavior.watch
usage and what it means semantically. This is my primary concern, and we've already seen this from current 2.x users.Whereas with
watchEffect
(eager) +watch
(lazy):watchEffect
.I played with the shorter name
effect
, but it has several problems:effect
alone only implies it applies side effects; however it doesn't signify that the effect will be reactively re-run.effect
doesn't show that connection and can make it harder to 2.x users to grasp the concept.With type definitions and proper IDE support, you will get auto completion for the API imports in both JS and TS, so name length shouldn't really be a concern (unless you really wish to stick to an editor without such support).
I did consider having both under the same
watch
with different signatures, but the eager/lazy behavior difference based on signature change can be confusing.99a2e18
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 see.
Everybody has a different mental model of how things work but I agree it's a bit confusing that
effect
runs only after the deps change in the first case, while they run every time in the second.In that sense re-using
watch
in the new name is a little counter-productive.I wish we had a better name but I don't have great suggestions...
run
,keepUp
,sustain
? meh.I'll stop the bike-shedding here 🚲