Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

flush: 'sync' option of watchEffect is inconsistent with the behavior of Vue 3 #770

Closed
John60676 opened this issue Jul 20, 2021 · 3 comments

Comments

@John60676
Copy link

Description

In composition-api, account change will not trigger this watch, when flush: 'sync' option is set,
but Vue 3 will trigger this watch.

setup() {
  const account = ref("John")
  const name = ref("")

  watchEffect(
    () => {
      name.value = account.value
      // in Vue 3, `account` change will trigger this watch, 
      // but in composition-api it will not
      console.log("watch")  
    },
    {
      flush: "sync",
    }
  )
  return { account, name }
}

Reproduce

Vue 2 demo: https://codesandbox.io/s/vue2-watcheffect-yebvl?file=/src/App.vue

Vue 3 demo: https://codesandbox.io/s/vue3-watcheffect-jy20t?file=/src/App.vue

@ygj6
Copy link
Member

ygj6 commented Jul 23, 2021

This PR #724 could solve the issue, but it's not the best solution.

@sunyanzhe
Copy link

I think the reason for this problem is this:

const value = getter ? getter.call(target) : val

When observe is executed, target will change to accessor mode.
Target must have getter/setter.
When the setter is triggered, getter.call(target) will collect dependencies.
That's why dependencies are collected despite assignment

I know this code is to avoid the data that is originally accessors, but this logic already exists in defineReactive, why repeat implementation.

sorry for the bad English

@ygj6
Copy link
Member

ygj6 commented Aug 16, 2021

Has been solved by #786.

@ygj6 ygj6 closed this as completed Aug 16, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants