From 275315a81fca198f1e3cd7a35c2374a14ddffeba Mon Sep 17 00:00:00 2001 From: Francis Gulotta Date: Sun, 22 Sep 2019 15:27:16 -0400 Subject: [PATCH] feat: add optional end event for piping (#1926) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This will allow streams to end. We haven't done this because it allows a stream to be reopened and data to be continued to be read. Adds `endOnClose` to stream options so the stream will emit the end event when the port is closed. This will signal any piped parsers to end as well. If the port is reopened the parsers won’t function. --- packages/stream/stream.js | 4 ++++ packages/stream/stream.test.js | 23 ++++++++++++++++++++++- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/packages/stream/stream.js b/packages/stream/stream.js index d3c8f9734..030d9bb9f 100644 --- a/packages/stream/stream.js +++ b/packages/stream/stream.js @@ -10,6 +10,7 @@ const FLOWCONTROLS = Object.freeze(['xon', 'xoff', 'xany', 'rtscts']) const defaultSettings = Object.freeze({ autoOpen: true, + endOnClose: false, baudRate: 9600, dataBits: 8, hupcl: true, @@ -432,6 +433,9 @@ SerialPort.prototype.close = function(callback, disconnectError) { this.closing = false debug('binding.close', 'finished') this.emit('close', disconnectError) + if (this.settings.endOnClose) { + this.emit('end') + } if (callback) { callback.call(this, disconnectError) } diff --git a/packages/stream/stream.test.js b/packages/stream/stream.test.js index 1ef75f08a..b07f548d5 100644 --- a/packages/stream/stream.test.js +++ b/packages/stream/stream.test.js @@ -439,7 +439,7 @@ describe('SerialPort', () => { }) describe('#close', () => { - it('emits a close event', done => { + it('emits a close event for writing consumers', done => { const port = new SerialPort('/dev/exists', () => { port.on('close', () => { assert.isFalse(port.isOpen) @@ -449,6 +449,27 @@ describe('SerialPort', () => { }) }) + it('emits an "end" event for reading consumers when endOnClose is true', done => { + const port = new SerialPort('/dev/exists', { endOnClose: true }) + port.on('open', () => { + port.on('end', () => { + assert.isFalse(port.isOpen) + done() + }) + port.close() + }) + }) + + it('doesn\'t emit an "end" event for reading consumers when endOnClose is false', done => { + const port = new SerialPort('/dev/exists') + port.on('open', () => { + port.on('end', () => { + done(new Error('Should not have ended')) + }) + port.close(() => done()) + }) + }) + it('has a close callback', done => { const port = new SerialPort('/dev/exists', () => { port.close(() => {