Offer a .$listen() helper to wrap .$on() .$off() calls during the instance lifespan
See original GitHub issueWhat 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:
- Created 6 years ago
- Comments:12 (6 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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 doingthis.$on
withoutthis.$off
in thedestroyed
event should be ok (no memory leaks, etc)A note about
$destroy
: https://vuejs.org/v2/api/#vm-destroyI think it should go here: https://github.com/vuejs/vuejs.org/blob/master/src/v2/guide/migration.md#dispatch-and-broadcast-replaced