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-handy-subscriptions

v1.6.4

Published

Vue 2.x plugin for creating events object, which pays attention to subscribers IDs in order to allow listener to unsubscribe from all events at once

Downloads

88

Readme

vue-handy-subscriptions

npm npm

This plugin is for easier Vue event bus subsciptions management. By using standart event bus approach:

    Vue.prototype.$eventBus = new Vue()
    // or
    export const EventBus = new Vue()

we create a new Vue instance with lots of unused methods and properties. vue-handy-subscriptions creates simple object containing events-related functionality.

Installation

import Vue from 'vue'
import HandySubs from 'vue-handy-subscriptions'

Vue.use(HandySubs)

Events management

Issue with using standart event bus is that you cannot just write this.$eventBus.off() inside component in order to unsubscribe only this component from all events it was subscribed to. Instead code above will remove all events from everywhere.

This package is responsible for automatic event bus unsubscription when component is being destroyed. No more need to write something like:

    beforeDestroy() {
        this.$eventBus.off('event-one', this.methodOne)
        this.$eventBus.off('event-two', this.methodTwo)
        this.$eventBus.off('event-three', this.methodThree)
        this.$eventBus.off('event-four', this.methodFour)
        // ... and so on
    }

Instead of above you should write:

Yes. Correct. Nothing. Plugin will handle all of this by itself, unsubscribing current component inside of its beforeDestroy hook.

Methods

Listening to event or events:

    created() {
        this.$listenTo('some-event', this.eventCallback)
        this.$listenTo(['second-event', 'third-event'], this.commonCallback)
    }

It is possible to fire multiple callbacks (even for multiple events):

    created() {
        this.$listenTo('some-event', [this.eventCallbackOne, this.eventCallbackTwo])
        this.$listenTo(['second-event', 'third-event'], [this.eventCallbackOne, this.eventCallbackTwo])
    }

Emitting event (example):

    methods: {
        fireEvent() {
            this.$emitEvent('some-event', { test: 'one' })
        }
    }

Removing event from events object for all listeners (example):

    methods: {
        dontWannaListenAnymore() {
            this.$eraseEvent('some-event') // now no component will listen to this event
            this.$eraseEvent(['second-event', 'third-event'])
        }
    }

Unsubsribe from all events manually (example):

    methods: {
        leaveMeAlone() {
            this.$fallSilent() // nice, but it is also done automatically inside "beforeDestroy" hook
        }
    }

Remove specific callback for specific event (example):

    methods: {
        leaveMeWithoutSpecificCallback() {
            this.$fallSilent('some-event', this.specificCallback)
        }
    }

Remove array of callbacks for specific event (example):

    methods: {
        leaveMeWithoutSpecificCallbacks() {
            this.$fallSilent('some-event', [this.callbackOne, this.callbackTwo])
        }
    }

Unsubscribe component from specific event or events (all component's callbacks for these events will be removed):

    methods: {
        notListenToOne() {
            this.$fallSilent('some-event')
        },
        notListenToMany() {
            this.$fallSilent(['some-event', 'another-event'])
        }
    }

Customization

If you use some plugins, which have some conflicting function names (or you just don't like default ones), you can rename all of them according to your preferences. NOTE: use this feature at your own risk as it will warn you only for Vue basic properties:

    "$options", "$parent", "$root", "$children", "$refs", "$vnode", "$slots", "$scopedSlots", "$createElement", "$attrs", "$listeners", "$el"
    import Vue from 'vue'
    import HandySubs from 'vue-handy-subscriptions'

    Vue.use(HandySubs, {
        listenTo: '$hear',
        emitEvent: '$fireEvent',
        eraseEvent: '$deleteEvent',
        fallSilent: '$noMore'
    })

    // later in component...
    created() {
        this.$hear('some-event', this.callbackMethod)
    },
    methods: {
        doSmth() {
            this.$fireEvent('some-event')
        },
        unsubscribe() {
            this.$noMore('some-event')
        }
    }