Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add destroy method #1

Merged

Conversation

misterdev
Copy link

This is useful to provide a way to avoid memory leaks

See also ccampbell#428

@misterdev misterdev changed the base branch from master to fix/mxm-add-destroy October 20, 2020 15:43
@misterdev misterdev merged commit e64d824 into Musixmatchdev:fix/mxm-add-destroy Oct 20, 2020
Comment on lines +193 to +199
function _removeEvent (object, type, callback) {
if (object.removeEventListener) {
object.removeEventListener(type, callback, false);
return;
}
object.detachEvent('on' + type, callback);
}
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the reverse of _addEvent that already exists

Comment on lines +905 to +907
_addEvent(targetElement, 'keypress', self._handleKeyEvent);
_addEvent(targetElement, 'keydown', self._handleKeyEvent);
_addEvent(targetElement, 'keyup', self._handleKeyEvent);
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is inside mousetrap constructor, I useself._handleKeyEvent so that I can remove it on destroy

Comment on lines +963 to +974
Mousetrap.prototype.destroy = function () {
var self = this

self.reset()

_removeEvent(self.target, 'keypress', self._handleKeyEvent);
_removeEvent(self.target, 'keydown', self._handleKeyEvent);
_removeEvent(self.target, 'keyup', self._handleKeyEvent);

self.target = undefined
self._handleKeyEvent = undefined
}
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove the listeners and clear the instance

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant