Subscribe on changes!

Computed values triggering an effect multiple times with partial updated values

avatar
Nov 25th 2021

Version

3.2.22

Reproduction link

stackblitz.com

const obs = reactive({});
const comp1 = computed(() => {
  return obs.a?.b;
});
const comp2 = computed(() => {
  return obs.a?.c;
});
effect(() => {
  console.log(comp1.value, comp2.value);
});
obs.a = {
  b: 1,
  c: 2,
};

another case

const obs = reactive({
  set: new Set(),
});
const comp1 = computed(() => {
  return obs.set.has(1);
});
const comp2 = computed(() => {
  return obs.set.size;
});
effect(() => {
  console.log(comp1.value, comp2.value);
});
obs.set.add(1);

Steps to reproduce

Open the devtool and look at the console output

What is expected?

Console outputs: undefined undefined 1 2

What is actually happening?

Console outputs: undefined undefined 1 undefined 1 2


Computed values triggered updates should be batched.

avatar
Nov 25th 2021

Thar requires a scheduler which @vue/reactivity doesn't provide - the Vue package does, and it's watch*() APIs do make use of it.