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-subscribe-store

v0.1.0

Published

Ability to integrate subscribable state into vue components

Downloads

4

Readme

Vue Subscribe Store

License Dependency Status NPM version

Why?

  1. Use any store that uses a subscribe method, such as svelte-store, with Vue.js. This package will subscribe to state changes and offer it to Vue in a way that's reactive.

  2. (Optional) Offers the same API as Vuex, so it's easy to convert vuex modules into another store type. This is optional—you could just offer a map of state and have a separate system for updating state. See examples for details.

Examples

import vuesub from 'vue-subscribe-store'
import { writable } from 'svelte/store'

Vue.use(vuesub)

const {
  mapState,
  mapMutations
} = vuesub.store({
  state: {
    value: writable(0), // writable() returns { subscribe }
    foo: 'treated as a static value'
  },
  mutators: {
    setValue(state, next) {
      // receive `state` as is; since we're using svelte-store,
      // writable objects have a .set method
      state.value.set(next)
    },
    resetValue(state) {
      state.value.set(0)
    }
  }
})

Vue.component('example', {
  // integrates `value` in a way that works with vue's reactivity model
  computed: mapState(['value']),
  methods: mapMutations(['setValue', 'resetValue']),
  template: `
  <div>
    <button @click="() => setValue(value + 1)">
      {{ value }}
    </button>
    <button @click="resetValue">x</button>
  </div>
  `
})

map* offers the same API as Vuex, either ['name'] or { rename: 'name' }. Currently no support for sub-modules, so mapActions('moduleName', ['name']) would not work.

Vue.use() will add this.$store to your vue components. It's schema is the same as vuex.

For example:

Vue.component('example', {
  computed: {
    value() {
      return this.$store.state.value
    }
  },
  methods: {
    setName(value) {
      this.$store.dispatch('setName', value)
    },
    ...mapActions(['setAge']),
    ...mapActions({
      setStatus: 'setRelationshipStatus'
    })
  }
})

To use along side Vuex, you can give the store an alternative name:

Vue.use(vuex)
Vue.use(vuesub, {
  name: 'svelte'
})

Vue.component('example', {
  created() {
    this.$store   // vuex
    this.$svelte  // vuesub
  }
})

If you are familiar with Vuex, you can provide state, getters, mutators, & actions. You can then integrate them into your vue-component using the mapState, mapGetters, mapMutations, and mapActions.

See the demo directory for a fully functional example.

import vuesub from 'vue-subscribe-store'
import { writable, derived } from 'svelte/store'

Vue.use(vuesub)

const {
  mapState,
  mapGetters,
  mapMutations,
  mapActions
} = vuesub.store({
  state: {
    value: writable(0)
  },
  getters: {
    isEven(state) {
      return derived(state.value, v => v % 2 === 0)
    },
    isOdd(state, getters) {
      return derived(getters.isEven, v => !v)
    }
  },
  mutators: {
    setValue(state, next) {
      state.value.set(next)
    }
  },
  actions: {
    empty({ commit }) {
      commit('setValue', 0)
    },
    change({ commit }, value) {
      commit('setValue', value)
    }
  }
})

Vue.component('example', {
  computed: {
    ...mapState(['value']),
    ...mapGetters(['isEven'])
  },
  methods: {
    ...mapActions(['empty', 'change']),
    inc() {
      this.change(this.value + 1)
    }
  },
  template: `
  <div>
    <button @click="inc">
      {{ value }}
    </button>
    <button @click="empty">x</button>
    <span>{{ isEven ? 'Even' : 'Odd' }}</span>
  </div>
  `
})