mousetrap icon indicating copy to clipboard operation
mousetrap copied to clipboard

Correctly deleting mousetrap instances

Open philiphendry opened this issue 8 years ago • 4 comments

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?

philiphendry avatar Jan 30 '16 14:01 philiphendry

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... :-)

tshinnic avatar Feb 09 '16 20:02 tshinnic

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.

tshinnic avatar Feb 09 '16 21:02 tshinnic

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?

kukjevov avatar Oct 24 '18 14:10 kukjevov

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?

ericsoco avatar Dec 08 '18 01:12 ericsoco