Subscribe on changes!

Add `watch`, `watchEffect` in "@vue/reactivity"

avatar
Jun 11th 2022

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.

https://github.com/vuejs/core/blob/bdffc143ef3aa27c347b22f19d0052194b54836e/packages/reactivity/src/index.ts#L1-L68

What does the proposed API look like?

import {watch, watchEffect} from "@vue/reactivity";
avatar
Jun 11th 2022

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.