fix iteration through programs on events. see #157.

This commit is contained in:
Christopher Jeffrey 2015-07-22 05:52:32 -07:00
parent e35811ae97
commit da59a32ac5
1 changed files with 31 additions and 25 deletions

View File

@ -363,15 +363,17 @@ Program.prototype._listenInput = function() {
key.full = name;
Program.list.forEach(function(self) {
self.emit('keypress', ch, key);
self.emit('key ' + name, ch, key);
Program.list.forEach(function(program) {
if (program.input !== self.input) return;
program.emit('keypress', ch, key);
program.emit('key ' + name, ch, key);
});
});
this.input.on('data', function(data) {
Program.list.forEach(function(self) {
self.emit('data', data);
Program.list.forEach(function(program) {
if (program.input !== self.input) return;
program.emit('data', data);
});
});
@ -379,11 +381,12 @@ Program.prototype._listenInput = function() {
this.on('newListener', function fn(type) {
if (type === 'keypress' || type === 'mouse') {
Program.list.forEach(function(self) {
self.removeListener('newListener', fn);
if (self.input.setRawMode && !self.input.isRaw) {
self.input.setRawMode(true);
self.input.resume();
Program.list.forEach(function(program) {
if (program.input !== self.input) return;
program.removeListener('newListener', fn);
if (program.input.setRawMode && !program.input.isRaw) {
program.input.setRawMode(true);
program.input.resume();
}
});
}
@ -391,9 +394,10 @@ Program.prototype._listenInput = function() {
this.on('newListener', function fn(type) {
if (type === 'mouse') {
Program.list.forEach(function(self) {
self.removeListener('newListener', fn);
self.bindMouse();
Program.list.forEach(function(program) {
if (program.input !== self.input) return;
program.removeListener('newListener', fn);
program.bindMouse();
});
}
});
@ -410,26 +414,28 @@ Program.prototype._listenOutput = function() {
// Output
function resize() {
Program.list.forEach(function(self) {
self.cols = self.output.columns;
self.rows = self.output.rows;
self.emit('resize');
Program.list.forEach(function(program) {
if (program.output !== self.output) return;
program.cols = program.output.columns;
program.rows = program.output.rows;
program.emit('resize');
});
}
this.output.on('resize', function() {
Program.list.forEach(function(self) {
if (!self.options.resizeTimeout) {
Program.list.forEach(function(program) {
if (program.output !== self.output) return;
if (!program.options.resizeTimeout) {
return resize();
}
if (self._resizeTimer) {
clearTimeout(self._resizeTimer);
delete self._resizeTimer;
if (program._resizeTimer) {
clearTimeout(program._resizeTimer);
delete program._resizeTimer;
}
var time = typeof self.options.resizeTimeout === 'number'
? self.options.resizeTimeout
var time = typeof program.options.resizeTimeout === 'number'
? program.options.resizeTimeout
: 300;
self._resizeTimer = setTimeout(resize, time);
program._resizeTimer = setTimeout(resize, time);
});
});
};