Skip to content

Commit 275315a

Browse files
authored
feat: add optional end event for piping (#1926)
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.
1 parent eae28d4 commit 275315a

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

packages/stream/stream.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const FLOWCONTROLS = Object.freeze(['xon', 'xoff', 'xany', 'rtscts'])
1010

1111
const defaultSettings = Object.freeze({
1212
autoOpen: true,
13+
endOnClose: false,
1314
baudRate: 9600,
1415
dataBits: 8,
1516
hupcl: true,
@@ -432,6 +433,9 @@ SerialPort.prototype.close = function(callback, disconnectError) {
432433
this.closing = false
433434
debug('binding.close', 'finished')
434435
this.emit('close', disconnectError)
436+
if (this.settings.endOnClose) {
437+
this.emit('end')
438+
}
435439
if (callback) {
436440
callback.call(this, disconnectError)
437441
}

packages/stream/stream.test.js

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@ describe('SerialPort', () => {
439439
})
440440

441441
describe('#close', () => {
442-
it('emits a close event', done => {
442+
it('emits a close event for writing consumers', done => {
443443
const port = new SerialPort('/dev/exists', () => {
444444
port.on('close', () => {
445445
assert.isFalse(port.isOpen)
@@ -449,6 +449,27 @@ describe('SerialPort', () => {
449449
})
450450
})
451451

452+
it('emits an "end" event for reading consumers when endOnClose is true', done => {
453+
const port = new SerialPort('/dev/exists', { endOnClose: true })
454+
port.on('open', () => {
455+
port.on('end', () => {
456+
assert.isFalse(port.isOpen)
457+
done()
458+
})
459+
port.close()
460+
})
461+
})
462+
463+
it('doesn\'t emit an "end" event for reading consumers when endOnClose is false', done => {
464+
const port = new SerialPort('/dev/exists')
465+
port.on('open', () => {
466+
port.on('end', () => {
467+
done(new Error('Should not have ended'))
468+
})
469+
port.close(() => done())
470+
})
471+
})
472+
452473
it('has a close callback', done => {
453474
const port = new SerialPort('/dev/exists', () => {
454475
port.close(() => {

0 commit comments

Comments
 (0)