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) {
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) {