From e35811ae970d671d4824915d217183355247773c Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Wed, 22 Jul 2015 05:43:50 -0700 Subject: [PATCH] allow multiple programs to use the same streams correctly. see #157. --- lib/program.js | 60 ++++++++++++++++++++++++++++++-------------------- 1 file changed, 36 insertions(+), 24 deletions(-) diff --git a/lib/program.js b/lib/program.js index 51046b0..9fc3648 100644 --- a/lib/program.js +++ b/lib/program.js @@ -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); + }); }); };