Add `watch`, `watchEffect` in "@vue/reactivity"
What problem does this feature solve?
I think "@vue/reactivity" should contain watch
, watchEffect
functions.
It's the core things for the reactive code writing approach, but they are missed in "@vue/reactivity" package.
It looks that "@vue/reactivity" contains all reactive things available in "vue" package, except watch*
functions.
It makes the lib uncompleted.
What does the proposed API look like?
import {watch, watchEffect} from "@vue/reactivity";
The raw counterpart of watchEffect()
is effect
, exported by the reactivity package.
watch()
-like behavior can be implemented with effect()
, pauseTracking()
and resumeTracking()
.
The reason why these two are only available invue
and not @vue/reactivity
, is that they rely on the scheduler implented of vue
, which schedules the component update lifecycle and doesn't make sense in the plain reactivity package.
So in short: watch()
~= effect()
, but all effects run immediately because there's no scheduler to schedule jobs asynchronously.