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

vuex-cancellable-actions

v0.1.0

Published

This repository provides a wrapper around your vuex actions to allow the cancellation of an ongoing action. For the moment the only API function implemented is `takeLatest` which automatically cancels every past actions as soon as a new instance of the ac

Downloads

818

Readme

vuex-cancellable-actions

This repository provides a wrapper around your vuex actions to allow the cancellation of an ongoing action. For the moment the only API function implemented is takeLatest which automatically cancels every past actions as soon as a new instance of the action is dispatched.

Usage

Wrap all your actions with makeCancellable and use the takeLatest wrapper around actions that should not be run in parallel.

new Vuex.Store({
  …
  actions: makeCancellable({
    action1: takeLatest(async function ({ commit, dispatch }, data) {
      dispatch('action2', data.subData)
      commit('commit1', data)
    }),
    action2: function (data) {
      commit('commit2', data)
    },
  })
})

Use case

After triggering an async action (let's name it action1), you may want to cancel it because a more recent action (action2) has occured and you don't want "action1" to keep on committing to the store while "action2" is also commiting. This could indeed lead to an inconsistent store.

To make things a bit less abstract let's consider the following scenario. Your vuejs application is loading and displaying a bunch of books and these books are filtered based on the menu selected by the user (e.g. read VS unread books). You have defined an action like:

async loadBooks ({ commit }, booksState) {
  const data = await apiCall(booksState)
  commit('setBookList', data)
}

If the number of read books is large, the API call may take longer to respond when fetching read books compared to when fetching unread books. Consequently, if the user switches quickly between the two menus, the following actions will be triggered:

dispatch('loadBooks', 'read')
// and a few ms later
dispatch('loadBooks', 'unread')

But since the API call is slower for read books, the order of commits will be reversed and the store will not reflect the user's latest action.

commit('setBooksList', unreadBooksData)
// and a few ms later
commit('setBooksList', readBooksData)

In this case it would be interesting to be able to cancel loadBooks, read as soon as loadBooks, unread is received to avoid the second commit. This is exactly what this library provides.