Skip to content

Commit 2045d85

Browse files
committed
Remove the disconnect event
1 parent 1888efc commit 2045d85

File tree

4 files changed

+18
-48
lines changed

4 files changed

+18
-48
lines changed

README.md

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,6 @@ In addition to reading the [article mentioned above](http://www.voodootikigod.co
8888
* [`Event: "error"`](#module_serialport--SerialPort+event_error)
8989
* [`Event: "open"`](#module_serialport--SerialPort+event_open)
9090
* [`Event: "data"`](#module_serialport--SerialPort+event_data)
91-
* [`Event: "disconnect"`](#module_serialport--SerialPort+event_disconnect)
9291
* [`Event: "close"`](#module_serialport--SerialPort+event_close)
9392
* _static_
9493
* [`.Binding`](#module_serialport--SerialPort.Binding) : [<code>BaseBinding</code>](#module_serialport--SerialPort..BaseBinding)
@@ -421,7 +420,7 @@ You should never have to wrap a Node-Serialport object in a try/catch statement
421420

422421
### SerialPort ⏏
423422
**Kind**: Exported class
424-
**Emits**: [<code>open</code>](#module_serialport--SerialPort+event_open), [<code>data</code>](#module_serialport--SerialPort+event_data), [<code>close</code>](#module_serialport--SerialPort+event_close), [<code>error</code>](#module_serialport--SerialPort+event_error), [<code>disconnect</code>](#module_serialport--SerialPort+event_disconnect)
423+
**Emits**: [<code>open</code>](#module_serialport--SerialPort+event_open), [<code>data</code>](#module_serialport--SerialPort+event_data), [<code>close</code>](#module_serialport--SerialPort+event_close), [<code>error</code>](#module_serialport--SerialPort+event_error)
425424
**Properties**
426425

427426
| Name | Type | Description |
@@ -493,7 +492,7 @@ The write operation is non-blocking. When it returns, data might still not have
493492

494493
Some devices, like the Arduino, reset when you open a connection to them. In such cases, immediately writing to the device will cause lost data as they wont be ready to receive the data. This is often worked around by having the Arduino send a "ready" byte that your Node program waits for before writing. You can also often get away with waiting around 400ms.
495494

496-
If a port is disconnected during a write, the write will error in addition to the disconnect event.
495+
If a port is disconnected during a write, the write will error in addition to the `close` event.
497496

498497
Even though serialport is a stream, when writing it can accept arrays of bytes in addition to strings and buffers. This extra functionality is pretty sweet.
499498

@@ -668,19 +667,10 @@ The `data` event puts the port in flowing mode. Data is emitted as soon as it's
668667

669668
* * *
670669

671-
<a name="module_serialport--SerialPort+event_disconnect"></a>
672-
673-
#### `Event: "disconnect"`
674-
The `disconnect` event's callback is called with an error object. This will always happen before a `close` event if a disconnection is detected. If a disconnect happens and there is no event handler for the disconnect event an error event will be emitted instead. The error will have the `disconnected` property set to `true`.
675-
676-
**Kind**: event emitted by [<code>SerialPort</code>](#exp_module_serialport--SerialPort)
677-
678-
* * *
679-
680670
<a name="module_serialport--SerialPort+event_close"></a>
681671

682672
#### `Event: "close"`
683-
The `close` event's callback is called with no arguments when the port is closed. In the event of an error, an error event is triggered.
673+
The `close` event's callback is called with no arguments when the port is closed. In the case of a disconnect it will be called with a Disconnect Error object (`err.disconnected == true`). In the event of a close error (unlikely), an error event is triggered.
684674

685675
**Kind**: event emitted by [<code>SerialPort</code>](#exp_module_serialport--SerialPort)
686676

UPGRADE_GUIDE.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ Upgrading from 4.x to 5.x
22
-------------
33
5.x is a major rewrite to make node serialport a NodeJS stream. While the api surface is similar there have been a number of changes to ensure more consistent error handling and operation of a serial port.
44

5+
- Removed the `disconnect` event. The `close` event now fires with a disconnect error object in the event of a disconnection.
6+
57
The exact changes will go here see #1046
68

79
Upgrading from 3.x to 4.x

lib/serialport.js

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,6 @@ function allocNewReadPool(poolSize) {
9595
* @emits module:serialport#data
9696
* @emits module:serialport#close
9797
* @emits module:serialport#error
98-
* @emits module:serialport#disconnect
9998
* @alias module:serialport
10099
*/
101100
function SerialPort(path, options, callback) {
@@ -278,7 +277,7 @@ The write operation is non-blocking. When it returns, data might still not have
278277
279278
Some devices, like the Arduino, reset when you open a connection to them. In such cases, immediately writing to the device will cause lost data as they wont be ready to receive the data. This is often worked around by having the Arduino send a "ready" byte that your Node program waits for before writing. You can also often get away with waiting around 400ms.
280279
281-
If a port is disconnected during a write, the write will error in addition to the disconnect event.
280+
If a port is disconnected during a write, the write will error in addition to the `close` event.
282281
283282
Even though serialport is a stream, when writing it can accept arrays of bytes in addition to strings and buffers. This extra functionality is pretty sweet.
284283
* @method module:serialport#write
@@ -379,30 +378,18 @@ SerialPort.prototype._read = function(bytesToRead) {
379378
});
380379
};
381380

382-
/**
383-
* The `disconnect` event's callback is called with an error object. This will always happen before a `close` event if a disconnection is detected. If a disconnect happens and there is no event handler for the disconnect event an error event will be emitted instead. The error will have the `disconnected` property set to `true`.
384-
* @event module:serialport#disconnect
385-
*/
386-
387381
SerialPort.prototype._disconnected = function(err) {
388382
if (!this.isOpen) {
389383
debug('disconnected aborted because already closed', err);
390384
return;
391385
}
392386
debug('disconnected', err);
393387
err.disconnected = true;
394-
395-
if (this.listeners('disconnect').length > 0) {
396-
this.emit('disconnect', err);
397-
} else {
398-
this.emit('error', err);
399-
}
400-
401-
this.close();
388+
this.close(null, err);
402389
};
403390

404391
/**
405-
* The `close` event's callback is called with no arguments when the port is closed. In the event of an error, an error event is triggered.
392+
* The `close` event's callback is called with no arguments when the port is closed. In the case of a disconnect it will be called with a Disconnect Error object (`err.disconnected == true`). In the event of a close error (unlikely), an error event is triggered.
406393
* @event module:serialport#close
407394
*/
408395

@@ -413,7 +400,9 @@ SerialPort.prototype._disconnected = function(err) {
413400
* @param {errorCallback} callback Called once a connection is closed.
414401
* @emits module:serialport#close
415402
*/
416-
SerialPort.prototype.close = function(callback) {
403+
SerialPort.prototype.close = function(callback, disconnectError) {
404+
disconnectError = disconnectError || null;
405+
417406
if (!this.isOpen) {
418407
debug('close attempted, but port is not open');
419408
return this._asyncError(new Error('Port is not open'), callback);
@@ -422,8 +411,8 @@ SerialPort.prototype.close = function(callback) {
422411
this.closing = true;
423412
this.binding.close().then(() => {
424413
this.closing = false;
425-
this.emit('close');
426-
if (callback) { callback.call(this, null) }
414+
this.emit('close', disconnectError);
415+
if (callback) { callback.call(this, disconnectError) }
427416
}, (err) => {
428417
this.closing = false;
429418
debug('Binding #close had an error', err);

test/serialport.js

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -871,29 +871,18 @@ describe('SerialPort', () => {
871871
});
872872
});
873873

874-
describe('disconnect errors', () => {
875-
it('emits as a disconnect event on a bad read', (done) => {
874+
describe('disconnect close errors', () => {
875+
it('emits as a disconnected close event on a bad read', (done) => {
876876
const port = new SerialPort('/dev/exists');
877877
sinon.stub(port.binding, 'read').callsFake(() => {
878878
return Promise.reject(new Error('EBAD_ERR'));
879879
});
880-
port.on('disconnect', (err) => {
881-
assert.instanceOf(err, Error);
882-
done();
883-
});
884-
port.on('error', done);
885-
port.read();
886-
});
887-
888-
it('emits as an error event if there are no listeners', (done) => {
889-
const port = new SerialPort('/dev/exists');
890-
sinon.stub(port.binding, 'read').callsFake(() => {
891-
return Promise.reject(new Error('attack ships on fire off the shoulder of Orion'));
892-
});
893-
port.on('error', (err) => {
880+
port.on('close', (err) => {
894881
assert.instanceOf(err, Error);
882+
assert.isTrue(err.disconnected);
895883
done();
896884
});
885+
port.on('error', done); // this shouldn't be called
897886
port.read();
898887
});
899888
});

0 commit comments

Comments
 (0)