npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

vue-async-computed-non-debounce

v3.4.1-alpha1

Published

Async computed properties for Vue

Downloads

12

Readme

vue-async-computed

This plugin is now Vue 2.0 compatible!

With this plugin, you can have computed properties in Vue that are computed asynchronously.

Without using this plugin, you can't do this:

new Vue({
  data: {
    userId: 1
  },
  computed: {
    username () {
      // Using vue-resource
      return Vue.http.get('/get-username-by-id/' + this.userId)
        // This assumes that this endpoint will send us a response
        // that contains something like this:
        // { 
        //   "username": "username-goes-here"
        // }
        .then(response => response.data.username)
    }
  }
}

Or rather, you could, but it wouldn't do what you'd want it to do. But using this plugin, it works just like you'd expect:

new Vue({
  data: {
    userId: 1
  },
  asyncComputed: {
    username () {
      return Vue.http.get('/get-username-by-id/' + this.userId)
        .then(response => response.data.username)
    }
  }
}

This is especially useful with ES7 async functions:

new Vue({
  asyncComputed: {
    async someCalculation () {
      const x = await someAsycFunction()
      const y = await anotherAsyncFunction()
      return x + y
    }
  }
})

Install

npm install --save vue-async-computed

Alternately, you can link it directly from a CDN:

<script src="https://unpkg.com/vue-async-computed"></script>
<!-- 
  That will always point to the latest version of vue-async-computed.
  You probably want to instead pin it to a specific version:
-->
<script src="https://unpkg.com/[email protected]"></script>

When used with a module system such as webpack or browserify, you need to explicitly install vue-async-computed via Vue.use():

import Vue from 'vue'
import AsyncComputed from 'vue-async-computed'

Vue.use(AsyncComputed)

You don't need to do this when using global script tags. So long as you include vue-async-computed in a script tag after Vue itself, it will be installed automatically.

Usage example

import AsyncComputed from 'vue-async-computed'

/* Initialize the plugin */
Vue.use(AsyncComputed)

/*
   Then, when you create a Vue instance (or component),
   you can pass an object named "asyncComputed" as well as
   or instead of the standard "computed" option. The functions
   you pass to "asyncComputed" should return promises, and the values
   those promises resolve to are then asynchronously bound to the
   Vue instance as they resolve. Just as with normal computed
   properties, if the data the property depends on changes
   then the property is re-run automatically.
   
   You can almost completely ignore the fact that behind the
   scenes they are asynchronous. The one thing to remember is
   that until a asynchronous property's promise resolves
   for the first time, the value of the computed property is null.
*/

const vm = new Vue({
  data: {
    x: 2,
    y: 3
  },
  asyncComputed: {
    sum () {
      const total = this.x + this.y
      return new Promise(resolve =>
        setTimeout(() => resolve(total), 1000)
      )
    }
  }
})

/*
   Until one second has passed, vm.sum will be null.  After that,
   vm.sum will be 5. If you change vm.x or vm.y, then one
   second later vm.sum will automatically update itself to be
   the sum of the values to which you set vm.x and vm.y the previous second.
*/

Like with regular synchronous computed properties, you can pass an object with a get method instead of a function, but unlike regular computed properties, async computed properties are always getter-only. If the object provided has a set method it will be ignored.

Async computed properties can also have a custom default value, which will be used until the data is loaded for the first time:

new Vue({
  data: {
    postId: 1
  },
  asyncComputed: {
    blogPostContent: {
      // The `get` function is the same as the function you would
      // pass directly as the value to `blogPostContent` if you
      // didn't need to specify a default value.
      get () {
        return Vue.http.get('/post/' + this.postId)
          .then(response => response.data.postContent)
       },
       // The computed proporty `blogPostContent` will have 
       // the value 'Loading...' until the first time the promise
       // returned from the `get` function resolves.
       default: 'Loading...'
    }
  }
}

/*
   Now you can display {{blogPostContent}} in your template, which
   will show a loading message until the blog post's content arrives
   from the server.
*/

You can instead define the default value as a function, in order to depend on props or on data:

new Vue({
  data: {
    postId: 1
  },
  asyncComputed: {
    blogPostContent: {
      get () {
        return Vue.http.get('/post/' + this.postId)
          .then(response => response.data.postContent)
      },
      default () {
        return 'Loading post ' + this.postId
      }
    }
  }
}

You can also set a custom global default value in the options passed to Vue.use:

Vue.use(AsyncComputed, {
  default: 'Global default value'
})

Recalculation

Just like normal computed properties, async computed properties keep track of their dependencies, and are only recalculated if those dependencies change. But often you'll have an async computed property you'll want to run again without any of its (local) dependencies changing, such as for instance the data may have changed in the database.

You can set up a watch function, whose purpose is to set up listeners on additional dependencies. Your async computed property will then be recalculated also if any of the watched dependencies change, in addition to the real dependencies the property itself has:


new Vue({
  data: {
    postId: 1,
    timesPostHasBeenUpdated: 0
  },
  asyncComputed: {
    // blogPostContent will update its contents if postId is changed
    // to point to a diffrent post, but will also refetch the post's
    // contents when you increment timesPostHasBeenUpdated.
    blogPostContent: {
      get () {
        return Vue.http.get('/post/' + this.postId)
          .then(response => response.data.postContent)
      },
      watch () {
        this.timesPostHasBeenUpdated
      }
    }
  }
}

Conditional Recalculation

Using watch it is possible to run the computed property again but it will run regardless of the value of the watched property. If you need more control over when the computation should be rerun you can use shouldUpdate:


new Vue({
  data: {
    postId: 1,
    // Imagine pageType can be one of 'index', 'details' and 'edit'
    pageType: 'index'
  },
  asyncComputed: {
    blogPostContent: {
      get () {
        return Vue.http.get('/post/' + this.postId)
          .then(response => response.data.postContent)
      },
      // Will update whenever the pageType or postId changes
      // but only if the pageType is not 'index' this way the
      // blogPostContent will be refetched when loading the
      // 'details' and 'edit' pages
      shouldUpdate () {
        return this.pageType !== 'index'
      }
    }
  }
}

The main advantage over adding an if statement within the get function is that when the computation is not rerun you are able to still access the old value.

Lazy properties

Normally, computed properties are run both immediately, and as necessary when their dependencies change. With async computed properties, you sometimes don't want that. With lazy: true, an async computed property will only be computed the first time it's accessed.

For example:

new Vue({
  data: {
    id: 1
  },
  asyncComputed: {
    mightNotBeNeeded: {
      lazy: true,
      get () {
        return Vue.http.get('/might-not-be-needed/' + this.id)
          .then(response => response.data.value)
      }
      // The value of `mightNotBeNeeded` will only be
      // calculated when it is first accessed.
    }
  }
}

Cancel debouncing

Normally, computed properties work with debouncing in the way the result of a change is not committed if other changes are triggered before the result is committed. For example, if a source property is continuously changing every 100ms and API call takes 500ms, normal async computed properties will not be changed until the change of the source property stops. With non-debounce option, async computed properties will be updated with the last source value.

new Vue({
  data: {
    searchText: ""
  },
  asyncComputed: {
    calculatedValue: {
      debounce: false,
      get () {
        return fetch.get('/some-api/' + searchText)
          .then(response => response.data.value)
      }
    }
  }
}

Error handling

By default, in case of a rejected promise in an async computed property, vue-async-computed will take care of logging the error for you.

If you want to use a custom logging function, the plugin takes an errorHandler option, which should be the function you want called with the error information. By default, it will be called with the error's stack trace as an argument, but if you want the raw error itself you can set the useRawError option to true.

For example:

Vue.use(AsyncComputed, {
  errorHandler (stack) {
    console.log('Hey, an error!')
    console.log('---')
    console.log(stack)
  }
)

// Or with `useRawError`:
Vue.use(AsyncComputed, {
  useRawError: true,
  errorHandler (err) {
    console.log('An error occurred!')
    console.log('The error message was: ' + err.msg)
    console.log('And the stack trace was:')
    console.log(err.stack)
  }
)

You can pass false as the errorHandler in order to silently ignore rejected promises.

License

MIT © Benjamin Fox