mousetrap
mousetrap copied to clipboard
Correctly deleting mousetrap instances
I've just come across MouseTrap and think it's a fantastic framework. I'm attempting to wrap it with an Aurelia custom attribute but I wanted to be sure my chosen method for destroying the MouseTrap instance is correct and I'm therefore freeing memory and dropping any event listeners.
When the aurelia view model is bound to the interface I'm running the following code to create a specific MouseTrap for the current DOM element:
this.mousetrap = new MouseTrap(this.element);
this.bindKeyboard();
The bindKeyboard()
method binds the current configuration of keys and callbacks.
When aurelia removes the view model the unbind is automatically called and I'm currently doing this:
this.mousetrap.reset();
delete this.mousetrap;
Is that enough to remove the instance?
I also was wondering about just this need, though for me using React. I note that there are no removeEventListener() calls in Mousetrap, and there isn't any way indicated as to how to deregister DOM event listeners. So whatever the callers do, wouldn't this leave the three event routines registered?
I guess this is a feature request... :-)
I'm checking through issues and PRs and saw pull request "Add Mousetrap.prototype.destroy" #264 , so we're not the first to want this.
As i see this is quite old, question is this relevant? Has been this addressed somewhere in different way ? @ccampbell can you please give us your stand for this?
Also just bumped into this when evaluating Mousetrap. Other than this omission, seems like a great lib. Why do we need to manually remove event listeners when cleaning up?