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-invalidation-controller

v0.1.4

Published

An AbortController-inspired way to force re-evaluation of Vue computed properties

Downloads

11

Readme

The package's logo showing a stamped red text stating "INVALID"

Vue Invalidation Controller

Test status on Travis Version on npm

The Invalidation Controller is a tiny class (less than 200 bytes gzipped) which enables you to manually force Vue computed properties to be re-evaluated. Its design is greatly inspired by the web API's AbortController.

Motivation

Nine out of ten times, you don't want to use this package. Manually invoking recomputations often violates the principle of unidirectional data flow.

However, since we don't live in a perfect world, there may be external entities in your application which are used as signaling devices rather than data sources.

That's what this package provides. It abstracts signals away, shielding you from creating reactive dummy counters that hold no valuable data and just increment for the sake of invalidating other properties.

Installation

npm install vue-invalidation-controller

Usage

This code is exclusively an example of how the InvalidationController class works. Do not do silly things like this.

You may also try out this example on CodeSandbox.

const Vue = require('vue')
const InvalidationController = require('vue-invalidation-controller')

new Vue({
  el: '#app',
  data: {
    // Instantiate the invalidation controller
    // Note that the controller instance doesn't *need* to be assigned to a reactive property
    // on the Vue instance. We just put it here because we want to access it in our template.
    controller: new InvalidationController()
  },
  computed: {
    time() {
      // Mark the computed property as invalidatable
      this.controller.useSignal()

      return Date.now()
    }
  },
  template: `
    <div>
      Time: {{ time }}<br>
      <!-- Click the button to activate the invalidation signal: -->
      <button @click="controller.invalidate">Invalidate</button>
    </div>`
})

Basic Recipe

  1. Create an InvalidationController instance.
  2. Call the useSignal() method inside a computed property definition to make it invalidatable.
  3. Use the invalidate() method to re-evaluate all invalidatable computed properties.

Advanced Usage

  • A controller's invalidate() method can be invoked more than once.

  • The same controller instance can be used in more than one computed property — and even across multiple components.

  • The useSignal() method works with Vue's built-in dependency tracking. That's to say that, if needed, you may invoke that method inside of an if/else branch to conditionally make a computed property invalidatable.

  • Because it's just a plain old Vue instance, the Invalidation Controller works flawlessly with Vue 3's composition API:

    const { computed } = require('vue')
    const InvalidationController = require('vue-invalidation-controller')
    
    // ...
    
    const controller = new InvalidationController()
    
    const time = computed(() => {
      controller.useSignal()
      return Date.now()
    })

Related

  • The vue-recomputed package targets the same use case with a very different API design.