allow multiple programs to use the same streams correctly. see #157.

This commit is contained in:
Christopher Jeffrey 2015-07-22 05:43:50 -07:00
parent 5e31eb1370
commit e35811ae97
1 changed files with 36 additions and 24 deletions

View File

@ -363,30 +363,38 @@ Program.prototype._listenInput = function() {
key.full = name;
self.emit('keypress', ch, key);
self.emit('key ' + name, ch, key);
Program.list.forEach(function(self) {
self.emit('keypress', ch, key);
self.emit('key ' + name, ch, key);
});
});
this.input.on('data', function(data) {
self.emit('data', data);
Program.list.forEach(function(self) {
self.emit('data', data);
});
});
keys.emitKeypressEvents(this.input);
this.on('newListener', function fn(type) {
if (type === 'keypress' || type === 'mouse') {
self.removeListener('newListener', fn);
if (self.input.setRawMode && !self.input.isRaw) {
self.input.setRawMode(true);
self.input.resume();
}
Program.list.forEach(function(self) {
self.removeListener('newListener', fn);
if (self.input.setRawMode && !self.input.isRaw) {
self.input.setRawMode(true);
self.input.resume();
}
});
}
});
this.on('newListener', function fn(type) {
if (type === 'mouse') {
self.removeListener('newListener', fn);
self.bindMouse();
Program.list.forEach(function(self) {
self.removeListener('newListener', fn);
self.bindMouse();
});
}
});
};
@ -402,23 +410,27 @@ Program.prototype._listenOutput = function() {
// Output
function resize() {
self.cols = self.output.columns;
self.rows = self.output.rows;
self.emit('resize');
Program.list.forEach(function(self) {
self.cols = self.output.columns;
self.rows = self.output.rows;
self.emit('resize');
});
}
this.output.on('resize', function() {
if (!self.options.resizeTimeout) {
return resize();
}
if (self._resizeTimer) {
clearTimeout(self._resizeTimer);
delete self._resizeTimer;
}
var time = typeof self.options.resizeTimeout === 'number'
? self.options.resizeTimeout
: 300;
self._resizeTimer = setTimeout(resize, time);
Program.list.forEach(function(self) {
if (!self.options.resizeTimeout) {
return resize();
}
if (self._resizeTimer) {
clearTimeout(self._resizeTimer);
delete self._resizeTimer;
}
var time = typeof self.options.resizeTimeout === 'number'
? self.options.resizeTimeout
: 300;
self._resizeTimer = setTimeout(resize, time);
});
});
};