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-easy-subscription

v0.3.2

Published

Quick and easy subscriptions for vue!

Downloads

9

Readme

vue-easy-subscription

Quick and modular subscriptions for vue!

Installation

npm i vue-easy-subscription
// NOTE: This library requires Vue and Vuex as peer dependencies!

Initialization

There are 2 main ways to use library:

A) As a Vue Plugin

In this scenario, a Vuex store will be created for you by default! Use option B if you wish to integrate it into an already existing Vuex store.

You can initialize the plugin in one of the following ways:

  1. Initialize with firestore. The subscription function for firestore is already in the lib:
import vueEasySub from "vue-easy-subscription"
import Vue from "vue"
import Firebase from "firebase"

Firebase.initializeApp({ ... your appConfig })

Vue.use(vueEasySub, { firestore: Firebase.firestore() })
  1. Initialize with firebase realtime database. The subscription function for firebase realtime database is already in the lib:
import vueEasySub from "vue-easy-subscription"
import Vue from "vue"
import Firebase from "firebase"

Firebase.initializeApp({ ... your appConfig })

Vue.use(vueEasySub, { firebaseRealtime: Firebase.database() })
  1. Initialize with your own subscription function:
import vueEasySub from "vue-easy-subscription"
Vue from "vue"
	
Vue.use(vueEasySub, { sub: yourSubFn(path, updateValueFn) })

/*
	1) yourSubFn must return a function that will unsubscribe from path. The function can be 
	async if you wish.
	2) yourSubFn must call the updateValueFn whenever the value at path is updated.

	---------------------
	Simple example subFn:
	---------------------
	const subFn = (path, updateValue) => {
		const updater = e => updateValue(e);
		window.addEventListener("scroll", updater);
		return () => window.removeEventListener("scroll", updater)
	}
*/

B) As Vuex store module

Use this method if you wish to integrate into an already existing Vuex Store. It is very similar to using method A.

import Vue from "vue"
import Vuex from "vuex"
import VueEasySub from "vue-easy-subscription"

const easySubModules = VueEasySub.getVuexModules(Vue, { sub: yourSubFn(path, updateValueFn) })
const store = new Vuex.Store({
	modules: {
		...easySubModules,
		yourStoreModuleA,
		yourStoreModuleB,
	}
})

Usage

Usage of this lib is very simple and powerful.

To establish a subscription, declare a $subs object in your data()

// inside your vue component
{
	data() {
		return {
			$subs: {
				pet: `pets/1`	// subFn will be called with path = `pets/1`
			}
		}
	},
	computed: {
		pet() {
			// a pet entry will become available in $subData when the subscription
			// returns a value.
			return this.$subData.pet
		}
	}
}

Once this component is destroyed and if there are no other subscribers to pets/1, the library will automatically unsubscribe from pets/1

Establishing subscription using computed property.

{
	props: ['petId'],	// assume we pass in 4
	computed: {
		$subs() {
			return {
				pet: 'pets/${this.petId}'	 // subFn will be called with path = `pets/4`
			}
		},
		pet() {
			// a pet entry will become available in $subData when the subscription
			// returns a value.
			return this.$subData.pet
		}
	}
}

Interdependent subscriptions!

{
	props: ['petId'],	// assume we pass in 4
	computed: {
		$subs() {
			const subs = {
				pet: 'pets/${this.petId}'	 // subFn will be called with path = `pets/4`
			}
			
			if (this.pet) {	// we retrieved this from our subscription!!
				subs.owner = `owners/${this.pet.owner}`
			}

			return subs
		},
		pet() {  return this.$subData.pet },
		owner() { return this.$subData.owner }
	}
}

non-string subscriptions (firestore example)

This example illustrates that the paths dont need to simple strings. As long as they are JSON serialiazible, any path is ok! The builtin firestore subscription function gives support for OR queries!! Firestore, by itself does not have that.

// In the example below, the where query would read as:
// (ownerId === this.ownerId) AND (type === 'DOG' OR type === 'CAT')
{
	props: ['ownerId'],
	computed: {
		$subs() {
			return {
			    pets: {
			        collection: "pet",
			        where: [
                        ["ownerId", "==", this.ownerId],
                        { or: [['type', '==', 'dog'], ['type', '==', 'cat']] }
                    ]
			    }
			}
		},
		// This will contain all the pets whose owner is what is passed into
		// the ownerId prop.
		pets() {  return this.$subData.pets },
	}
}

Using multiple instances

The library comes equipped with being able to handle multiple subscription methods!

Vue.use(VueEasySub, [
	{ firestore:  Firebase.firestore(), name:  'firestore' },
	{ firebaseRealtime:  Firebase.database(), name:  'firebase' },
	{ sub: yourSubFn, name: 'yours' }
])

/* 
subscriptions to firestore will be made using the $firestore_subs property
subscriptions to firebase will be made using the $firebase_subs property
subscriptions to yours will be made using the the $yours_subs property

the results of firestore subscriptions will be available at $firestore_subData
the results of firestore subscriptions will be available at $firebase_subData
the results of firestore subscriptions will be available at $yours_subData
*/

Unsubscribing

As mentioned earlier, the lib will automatically unsubscribe when instances pointing to a subscription are destroyed. You don't have to manually call unsubscribe on anything!