From e6c763a73c8a0a5207fd772909817669001c9ff6 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Fri, 1 May 2015 22:43:31 -0700 Subject: [PATCH] use onScreenEvent for element keys to prevent memory leaks. --- lib/widget.js | 42 ++++++++++++++++++++++++++++++++---------- 1 file changed, 32 insertions(+), 10 deletions(-) diff --git a/lib/widget.js b/lib/widget.js index 1634297..38b7605 100644 --- a/lib/widget.js +++ b/lib/widget.js @@ -2520,13 +2520,23 @@ Element.prototype.sattr = function(style, fg, bg) { }; Element.prototype.onScreenEvent = function(type, handler) { - this.screen.on(type, handler); var listeners = this._slisteners = this._slisteners || []; listeners.push({ type: type, handler: handler }); + this.screen.on(type, handler); +}; + +Element.prototype.onceScreenEvent = function(type, handler) { + var listeners = this._slisteners = this._slisteners || []; + var entry = { type: type, handler: handler }; + listeners.push(entry); + this.screen.once(type, function() { + var i = listeners.indexOf(entry); + if (~i) listeners.splice(i, 1); + return handler.apply(this, arguments); + }); }; Element.prototype.removeScreenEvent = function(type, handler) { - this.screen.removeListener(type, handler); var listeners = this._slisteners = this._slisteners || []; for (var i = 0; i < listeners.length; i++) { var listener = listeners[i]; @@ -2535,9 +2545,10 @@ Element.prototype.removeScreenEvent = function(type, handler) { if (this._slisteners.length === 0) { delete this._slisteners; } - return; + break; } } + this.screen.removeListener(type, handler); }; Element.prototype.free = function() { @@ -2552,7 +2563,6 @@ Element.prototype.free = function() { Element.prototype.destroy = function() { this.detach(); this.free(); - this.screen.render(); }; Element.prototype.hide = function() { @@ -3099,17 +3109,29 @@ Element.prototype.disableDrag = function() { return this._draggable = false; }; -Element.prototype.key = function() { - return this.screen.program.key.apply(this, arguments); +Element.prototype.key = function(key, listener) { + // return this.screen.program.key.apply(this, arguments); + if (typeof key === 'string') key = key.split(/\s*,\s*/); + key.forEach(function(key) { + return this.onScreenEvent('key ' + key, listener); + }, this); }; -Element.prototype.onceKey = function() { - return this.screen.program.onceKey.apply(this, arguments); +Element.prototype.onceKey = function(key, listener) { + // return this.screen.program.onceKey.apply(this, arguments); + if (typeof key === 'string') key = key.split(/\s*,\s*/); + key.forEach(function(key) { + return this.onceScreenEvent('key ' + key, listener); + }, this); }; Element.prototype.unkey = -Element.prototype.removeKey = function() { - return this.screen.program.unkey.apply(this, arguments); +Element.prototype.removeKey = function(key, listener) { + // return this.screen.program.unkey.apply(this, arguments); + if (typeof key === 'string') key = key.split(/\s*,\s*/); + key.forEach(function(key) { + return this.removeScreenEvent('key ' + key, listener); + }, this); }; Element.prototype.setIndex = function(index) {