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

@wlard/vue-class-store

v3.0.0

Published

Universal Vue stores you write once and use anywhere

Downloads

14

Readme

Vue Class Store

Universal Vue stores you write once and use anywhere

logo

Abstract

So Vue, Vuex and Vue Class Store walk into a bar...

Vue says: "I'll give you reactivity, computed properties and watches, but only in components, only using a special objects-and-function schema, and I demand initial values be passed in from a parent component using props! I don't get along particularly well with TypeScript either, so good luck with figuring that one out, buddy"

Vuex says: "I'll give you global reactivity and computed properties, but I'm going to call them esoteric names and require you set them up globally with a convoluted schema, access them only through a centralised store, and I'll force you to make all updates through string-based paths, with different formats, helpers, terminology and best practices. I'll also make it difficult to go more than a couple of levels deep, and I'll give you watches but you'll hate them so probably best not to use those either"

Vue Class Store says: "I'll give you reactivity, computed properties and watches, written in standard JavaScript or TypeScript, with no setup or boilerplate, and you can use me anywhere"

The end

Usage

Installation

Install the package from NPM:

#vue 2
npm i vue-class-store@^2.0.0

#vue 3
npm i vue-class-store@^3.0.0

Yarn users, replace npm i with yarn add.

Declaration

Write classes as normal, and add the decorator @VueStore to those you want to become reactive.

import VueStore from 'vue-class-store'

@VueStore
export class Store {
  // properties are rebuilt as reactive data values
  public value: number

  // getters are converted to (cached) computed properties
  public get double (): number {
    return this.value * 2
  }

  // constructor parameters serve as props
  constructor (value: number = 1) {
    // constructor function serves as the created hook
    this.value = value
  }

  // prefix properties with `on:` to convert to watches
  'on:value' () {
    console.log('value changed to:', this.value)
  }

  // you can even drill into sub properties!
  'on:some.other.value' = 'log'

  // class methods are added as methods
  log () {
    console.log('value is:', this.value)
  }
}

Instantiation

To use a store, simply instantiate the class.

You can do this outside of a component, and it will be completely reactive:

const store = new Store({ ... })

Or you can instantiate within a component:

export default {
  ...
  computed: {
    model () {
      return new Store({ ... })
    }
  }
}

Alternatively, you can make any non-decorated class reactive on the fly using the static .create() method:

import VueStore from 'vue-class-store'
import Store from './Store'

const store: Store = VueStore.create(new Store({ ... }))

How it works

This is probably a good point to stop and explain what is happening under the hood.

Immediately after the class is instantiated, the decorator function extracts the class' properties and methods and rebuilds either a new Vue instance (Vue 2) or a Proxy object (Vue 3).

This functionally-identical object is then returned, and thanks to TypeScript generics your IDE and the TypeScript compiler will think it's an instance of the original class, so code completion will just work.

Additionally, because all methods have their scope rebound to the original class, breakpoints will stop in the right place, and you can even call the class keyword super and it will resolve correctly up the prototype chain.

devtools

Note that the object will of course be a Vue or Proxy instance, so running code like store instanceof Store will return false .

Inheritance

The decorator supports class inheritance meaning you can do things like this:

class Rectangle {
  width = 0
  height = 0
  
  constructor (width, height) {
    this.width = width
    this.height = height
  }
  
  get area () { return this.width * this.height }
}

@VueStore
class Square extends Rectangle {
  constructor (size) {
    super(size, size)
  }

  'on:width' (value) { this.height = value }
  'on:height' (value) { this.width = value }
}

Make sure you don't inherit from another decorated class because the original link to the prototype chain will have been broken by the substituted object returned by the previous decorator:

// don't do this!

@VueStore
class Rectangle { ... }

@VueStore
class Square extends Rectangle { ... }

If you need to keep the original Rectangle and Square intact, decorate a final empty class that leaves the original classes untouched:

// do this instead...

class Rectangle { ... }
class Square extends Rectangle { ... }

@VueStore
class SquareStore extends Square { } 

Alternatively, use inline creation:

import Square from './Square'

const model: Square = VueStore.create(new Square(10))

Global / shared state

Because the class itself is reactive, you could inject it into a component tree, simulating global state:

export default {
  provide () {
    return {
      $products: new ProductsStore()
    }
  },
}
export default {
  inject: [
    '$products'
  ],
  
  computed: {
    items () {
      return this.$products.items
    }
  },
  
  filterProducts (value) {
    this.$products.filter = value
  }
}

Development

Demo

The library has demos for Vue 2, Vue 3 and Nuxt, and can be found in the following repo:

  • https://github.com/davestewart/vue-class-store-demos

Scripts

The package uses Yarn, and has only two scripts, to build for development and production:

  • yarn dev - build and watch for development
  • yarn build - build for production