diff --git a/modules/win-terminal.js b/modules/win-terminal.js index 65f82f9..81ce24d 100644 --- a/modules/win-terminal.js +++ b/modules/win-terminal.js @@ -146,6 +146,10 @@ function windows_terminal() { this.StartEx = function Start(CONSOLE_SCREEN_WIDTH, CONSOLE_SCREEN_HEIGHT, terminalTarget) { + if (this._stream != null) + { + throw ('Concurrent Terminal sessions are not supported on Windows'); + } this.stopping = null; if (this._kernel32.GetConsoleWindow().Val == 0) { if (this._kernel32.AllocConsole().Val == 0) { @@ -180,45 +184,59 @@ function windows_terminal() { // Hook Ready this.terminal.StartCommand(this.userArgs[0]); }, console.log); - this._stream = new duplex({ - 'write': function (chunk, flush) { - if (!this.terminal.connected) { - //console.log('_write: ' + chunk); - if (!this._promise.chunk) { - this._promise.chunk = []; - } - if (typeof (chunk) == 'string') { - this._promise.chunk.push(chunk); - } else { - this._promise.chunk.push(Buffer.alloc(chunk.length)); - chunk.copy(this._promise.chunk.peek()); - } - this._promise.chunk.peek().flush = flush; - this._promise.then(function () { - var buf; - while (this.chunk.length > 0) { - buf = this.chunk.shift(); - this.terminal._WriteBuffer(buf); - buf.flush(); + this._stream = new duplex( + { + 'write': function (chunk, flush) + { + if (!this.terminal.connected) + { + //console.log('_write: ' + chunk); + if (!this._promise.chunk) + { + this._promise.chunk = []; } - }); + if (typeof (chunk) == 'string') + { + this._promise.chunk.push(chunk); + } else + { + this._promise.chunk.push(Buffer.alloc(chunk.length)); + chunk.copy(this._promise.chunk.peek()); + } + this._promise.chunk.peek().flush = flush; + this._promise.then(function () + { + var buf; + while (this.chunk.length > 0) + { + buf = this.chunk.shift(); + this.terminal._WriteBuffer(buf); + buf.flush(); + } + }); + } + else + { + //console.log('writeNOW: ' + chunk); + this.terminal._WriteBuffer(chunk); + flush(); + } + return (true); + }, + 'final': function (flush) + { + var p = this.terminal._stop(); + p.__flush = flush; + p.then(function () { this.__flush(); }); } - else { - //console.log('writeNOW: ' + chunk); - this.terminal._WriteBuffer(chunk); - flush(); - } - return (true); - }, - 'final': function (flush) { - var p = this.terminal._stop(); - p.__flush = flush; - p.then(function () { this.__flush(); }); - } - }); + }); this._stream.terminal = this; this._stream._promise = new promise(function (res, rej) { this._res = res; this._rej = rej; }); this._stream._promise.terminal = this; + this._stream.prependOnceListener('end', function () + { + this.terminal._stream = null; + }); return (this._stream); }; this.Start = function Start(CONSOLE_SCREEN_WIDTH, CONSOLE_SCREEN_HEIGHT)