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

Fix memory leak (add destroy method) #98

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 30 additions & 18 deletions src/formatter.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,16 +64,18 @@ function Formatter(el, opts) {
self.hldrs = {};
self.focus = 0;

// Store references to event handlers to be able to remove them
self._handlers = {
keyDown: self._keyDown.bind(self),
keyPress: self._keyPress.bind(self),
paste: self._paste.bind(self),
focus: self._focus.bind(self),
};

// Add Listeners
utils.addListener(self.el, 'keydown', function (evt) {
self._keyDown(evt);
});
utils.addListener(self.el, 'keypress', function (evt) {
self._keyPress(evt);
});
utils.addListener(self.el, 'paste', function (evt) {
self._paste(evt);
});
utils.addListener(self.el, 'keydown', self._handlers.keyDown);
utils.addListener(self.el, 'keypress', self._handlers.keyPress);
utils.addListener(self.el, 'paste', self._handlers.paste);

// Persistence
if (self.opts.persistent) {
Expand All @@ -82,18 +84,28 @@ function Formatter(el, opts) {
self.el.blur();

// Add Listeners
utils.addListener(self.el, 'focus', function (evt) {
self._focus(evt);
});
utils.addListener(self.el, 'click', function (evt) {
self._focus(evt);
});
utils.addListener(self.el, 'touchstart', function (evt) {
self._focus(evt);
});
utils.addListener(self.el, 'focus', self._handlers.focus);
utils.addListener(self.el, 'click', self._handlers.focus);
utils.addListener(self.el, 'touchstart', self._handlers.focus);
}
}

//
// @public
// Remove event handlers
//
Formatter.prototype.destroy = function () {
utils.removeListener(this.el, 'keydown', this._handlers.keyDown);
utils.removeListener(this.el, 'keypress', this._handlers.keyPress);
utils.removeListener(this.el, 'paste', this._handlers.paste);

if (this.opts.persistent) {
utils.removeListener(this.el, 'focus', this._handlers.focus);
utils.removeListener(this.el, 'click', this._handlers.focus);
utils.removeListener(this.el, 'touchstart', this._handlers.focus);
}
};

//
// @public
// Add new char
Expand Down
10 changes: 10 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,16 @@ utils.addListener = function (el, evt, handler) {
: el.attachEvent('on' + evt, handler);
};

//
// Helper method for cross browser implementation for removing
// event listeners
//
utils.removeListener = function (el, evt, handler) {
return (typeof el.removeEventListener !== 'undefined')
? el.removeEventListener(evt, handler, false)
: el.detachEvent('on' + evt, handler);
};

//
// Helper method for cross browser implementation of preventDefault
//
Expand Down