Skip to content

computed triggers watcher of sync twice #9149

Closed
@Mini-ghost

Description

@Mini-ghost

Vue version

3.3.4

Link to minimal reproduction

https://stackblitz.com/edit/vitejs-vite-9dgxah?file=src%2FApp.vue

Steps to reproduce

  1. Open reproduction and click plus button.
  2. The update become 2 from 0. (expected is 1)

What is expected?

According the following code, when count update once, the watcher should just run once.

import { ref, computed, watch } from 'vue';

const update = ref(0);

const count = ref(0);

const sync1 = computed(() => count.value);
const sync2 = computed(() => count.value);

const sync = computed(() => ({
  sync1: sync1.value,
  sync2: sync2.value,
}));

watch(
  sync,
  (value) => {
    update.value++;
  },
  {
    flush: 'sync',
  }
);

What is actually happening?

The watcher ran twice!

System Info

System:
    OS: macOS 11.6
    CPU: (8) arm64 Apple M1
    Memory: 90.47 MB / 16.00 GB
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 18.16.0 - ~/.nvm/versions/node/v18.16.0/bin/node
    npm: 9.5.1 - ~/.nvm/versions/node/v18.16.0/bin/npm
  Browsers:
    Firefox: 116.0.2
    Safari: 14.1.2
    Safari Technology Preview: 15.4
  npmPackages:
    vue: ^3.3.4 => 3.3.4 

Any additional comments?

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions