use onScreenEvent for element keys to prevent memory leaks.

This commit is contained in:
Christopher Jeffrey 2015-05-01 22:43:31 -07:00
parent d68aa5cf41
commit e6c763a73c
1 changed files with 32 additions and 10 deletions

View File

@ -2520,13 +2520,23 @@ Element.prototype.sattr = function(style, fg, bg) {
}; };
Element.prototype.onScreenEvent = function(type, handler) { Element.prototype.onScreenEvent = function(type, handler) {
this.screen.on(type, handler);
var listeners = this._slisteners = this._slisteners || []; var listeners = this._slisteners = this._slisteners || [];
listeners.push({ type: type, handler: handler }); 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) { Element.prototype.removeScreenEvent = function(type, handler) {
this.screen.removeListener(type, handler);
var listeners = this._slisteners = this._slisteners || []; var listeners = this._slisteners = this._slisteners || [];
for (var i = 0; i < listeners.length; i++) { for (var i = 0; i < listeners.length; i++) {
var listener = listeners[i]; var listener = listeners[i];
@ -2535,9 +2545,10 @@ Element.prototype.removeScreenEvent = function(type, handler) {
if (this._slisteners.length === 0) { if (this._slisteners.length === 0) {
delete this._slisteners; delete this._slisteners;
} }
return; break;
} }
} }
this.screen.removeListener(type, handler);
}; };
Element.prototype.free = function() { Element.prototype.free = function() {
@ -2552,7 +2563,6 @@ Element.prototype.free = function() {
Element.prototype.destroy = function() { Element.prototype.destroy = function() {
this.detach(); this.detach();
this.free(); this.free();
this.screen.render();
}; };
Element.prototype.hide = function() { Element.prototype.hide = function() {
@ -3099,17 +3109,29 @@ Element.prototype.disableDrag = function() {
return this._draggable = false; return this._draggable = false;
}; };
Element.prototype.key = function() { Element.prototype.key = function(key, listener) {
return this.screen.program.key.apply(this, arguments); // 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() { Element.prototype.onceKey = function(key, listener) {
return this.screen.program.onceKey.apply(this, arguments); // 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.unkey =
Element.prototype.removeKey = function() { Element.prototype.removeKey = function(key, listener) {
return this.screen.program.unkey.apply(this, arguments); // 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) { Element.prototype.setIndex = function(index) {