foxbenjaminfox/vue-async-computed

[proposal] Allow watch to bail out of updating

tjallingt opened this issue · 2 comments

It would allow for a lot of flexibility if watch could return false to bail out of rerunning the getter like so:

new Vue({
  data: {
    postId: 1,
    pageType: 'list',
  },
  asyncComputed: {
    blogPostContent: {
      get () {
        return Vue.http.get('/post/' + this.postId)
          .then(response => response.data.postContent)
      },
      watch () {
        return this.pageType !== 'index';
      }
    }
  }
}

This would watch pageType and thus rerun the getter every time the pageType changed but only rerun blogPostContent.get whenever watch is not returning false (meaning current behaviour is preserved as long as nothing is returned).

Potentially in a more breaking update you could even choose to rename watch to shouldUpdate and force it to return either true or false but that'd be a pretty big breaking change and its not necessairy to get a simpler version of this working.

I can submit a pull request for this if you'd be willing to consider merging this @foxbenjaminfox 😄

This is a cool idea, but it seems sufficiently different from watch that I think it really should be a separate option, instead of repurposing watch to have this feature as well.

Let's leave watch with its current functionality, and have a separate option shouldUpdate that can be used independently from watch.

If you want to send a pull request for this, go right ahead. I'll be happy to merge it.