question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Offer a .$listen() helper to wrap .$on() .$off() calls during the instance lifespan

See original GitHub issue

What problem does this feature solve?

I often add several event listeners to various things in my created() function, and then having to remove them in the beforeDestroy function.

This mixin helps a ton in every case for these cases, so that having to keep track of event listeners for a specific instance are cleaned up correctly.

Vue.mixin({
    methods: {
        listen: function listen(source, event, fn) {
            this.listeningEvents = this.listeningEvents || [];
            this.listeningEvents.push(() => {
                source.$off(event, fn);
            });
            source.$on(event, fn);
        },
    },
    beforeDestroy: function beforeDestroy() {
        (this.listeningEvents || []).forEach(fn => fn());
    },
});

if this could be built in (bonus points if (source.$on || source.on)() is used for commonly used eventemitters!) then it should save a lot of time and ease of use for tracking weird bugs due to left over bound events.

What does the proposed API look like?

On a Vue instance, this.$listen(otherVueInstance, 'event_name', (event) => {});.

No need to worry about cleaning up the event when the instance is destroyed.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:12 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
posvacommented, Jul 16, 2017

That fiddle is really different, you’re attaching events to another source, they’re not linked to the vue instance you’re calling the $on function in any way, so it’s normal for you to clean them up: https://jsfiddle.net/posva/71rfnyq8/

To make it clear: listeners setup with this.$on are cleaned up when the instance is destroyed. So doing this.$on without this.$off in the destroyed event should be ok (no memory leaks, etc)

A note about $destroy: https://vuejs.org/v2/api/#vm-destroy

Read more comments on GitHub >

github_iconTop Results From Across the Web

Build fulfillment with the Actions on Google Node.js client ...
To build an app instance in your fulfillment webhook, follow these steps: Call the require() function to import the 'actions-on-google' ...
Read more >
bpf-helpers(7) - Linux manual page - man7.org
This helper works in combination with bpf_csum_diff(), which does not update the checksum in-place, but offers more flexibility and can handle sizes larger...
Read more >
Node.js v19.3.0 Documentation
Iterates through the list of functions passed to tracker.calls() and will throw an error for functions that have not been called the expected...
Read more >
Backbone.js
on(event, callback, object), is that listenTo allows the object to keep track of the events, and they can be removed all at once...
Read more >
A First Look at PyScript: Python in the Web Browser
In this tutorial, you'll learn about PyScript, a new framework that allows for running Python in the web browser with few or no...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found