question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Vue.watch - Watchers for custom objects.

See original GitHub issue

What problem does this feature solve?

More than a problem this is a PoC. I’d like to test if this feature is something wanted or even if it has sense inside Vue ecosystem.

With this proposed API, Vue would have the ability to create watchers over custom objects that aren’t part of a component.

What does the proposed API look like?

The definition of this method would be something like:

Vue.watch(obj: Object, expOrFn: string | Function, cb: any, options?: Object): Function

The usage of this API would be something like:

const state = {
  counter: 0
}

// Use subscriber() to unsubscribe ...
cons subscriber = Vue.watch(state, 'counter', newVal => console.log(`Counter updated ${newVal}`))

state.counter += 1

// Console output: 1

Take into consideration

Current implementation of Watcher

The Watcher object requires a Vue instance. I don’t know if its mandatory for a watcher but I’d like to know if it feasible to decouple the Watcher from the need of an instance. Checking the code I’ve seen the VM is only responsible for storing the watchers and bind itself into the callback functions.

This seems to be a considerable amount of work.

Other thing would be that the passed object would need (Maybe it’s not needed) to store its watchers somewhere. Maybe a private property (__watchers__) inside the current object. Ideas are welcomed.

To be or not be reactive

The object passed to Vue.watch could be reactive or non-reactive. I see 2 options here:

  1. Only allow reactive objects using Vue.observable first. With this approach, the initial example won’t work because the state was a plain object.
const state = Vue.observable({
  counter: 0
})

Vue.watch(state, 'counter', newVal => console.log(`Counter updated: ${newVal}`))

state.counter += 1

// Counter updated: 1
  1. Vue.watch makes your object reactive, if it isn’t initially. We won’t need to make it observable, because Vue.watch make it reactive for us. This approach will mutate the original object.

Final note

I didn’t make an RFC because I want to check if this feature is useful and feasible before I inspect a bit more in the details of this new API.

If core maintainers think that it will be more suitable in RFCs repository, I’ll write one then.

Any feedback is welcomed.

Thanks.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:7
  • Comments:13 (2 by maintainers)

github_iconTop GitHub Comments

24reactions
yyx990803commented, Feb 18, 2019

We do plan to expose something like this in v3. There will be an RFC discussing the actual API design, and when it is finalized we will likely introduce it in v2 first.

19reactions
sirlancelotcommented, Feb 17, 2019

This is possible now in Vue 2.6.x using the new Vue.observable() helper:

const shared = Vue.observable({ value: "hello" })

const vm = new Vue({
  created() {
    this.$watch(() => shared.value, (value) => {
      console.log("value changed to:", value)
    })
  }
})
Read more comments on GitHub >

github_iconTop Results From Across the Web

Watchers
Deep Watchers # ... Deep watch requires traversing all nested properties in the watched object, and can be expensive when used on large...
Read more >
Understanding watchers in Vue
With watchers, we are not just able to watch a property and perform custom actions; we can also access the old value that...
Read more >
Vue.js - How to properly watch for nested data
If you want to watch all items in a list and know which item in the list changed, you can set up custom...
Read more >
How to use Watchers in Vue
In Vue, we can watch for property changes using watchers. ... Vue only watches data changes in objects or arrays at its first...
Read more >
Learn How to use Vue.js Watchers
Sometimes you may want to watch data for changes and react to them. Typically all you need is computed properties, but there are...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found