vue-handy-subscriptions

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
679 9
Install
npm install vue-handy-subscriptions
Latest Version:1.6.4
License:ISC
Last Updated:Nov 5, 2018
Links: GitHub  ·  npm
Maintainer: p1pchenk0

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')
        }
    }

Related Packages

vue-button-spinner

A simple button spinner.

16,948 35
vue-event-handler

Global event bus for Vue 1 and Vue 2.

2,456 15
vue-bus

A event bus for Vue.js

338,089 309
vuemit

The smallest Vue.js events handler.

18,053 141
vue-events

Easy event handling for Vue applications.

294,092 231

Version History

Version Released License
1.6.4 ISC
1.6.3 ISC
1.6.2 ISC
1.6.1 ISC
1.6.0 ISC