Cleanup disconnect path (#855)

- make the disconnect path more consistent
 - call callback in the context of the port
This commit is contained in:
Francis Gulotta 2016-06-16 00:21:42 -04:00 • committed by GitHub
parent c84c9a22c9
commit 36be232bb1
2 changed files with 20 additions and 38 deletions

View file

@ -2,10 +2,6 @@
// Copyright 2011 Chris Williams <chris@iterativedesigns.com>
// Require serialport binding from pre-compiled binaries using
// node-pre-gyp, if something fails or package not available fallback
// to regular build from source.
// 3rd Party Dependencies
var debug = require('debug')('serialport');
@ -123,17 +119,7 @@ function SerialPort(path, options, callback) {
}
});
// TODO remove this option
settings.disconnectedCallback = function(err) {
if (this.closing) {
return;
}
if (!err) {
err = new Error('Disconnected');
}
this.emit('disconnect', err);
}.bind(this);
settings.disconnectedCallback = this._disconnected.bind(this);
settings.dataCallback = settings.parser.bind(this, this);
this.fd = null;
@ -347,14 +333,22 @@ if (process.platform !== 'win32') {
} // if !'win32'
SerialPort.prototype._disconnected = function(err) {
this.emit('disconnect', err);
this.paused = true;
this.closing = true;
this.emit('disconnect', err);
if (this.closing) {
return;
}
if (this.fd === null) {
return;
}
this.closing = true;
if (process.platform !== 'win32') {
this.readable = false;
this.serialPoller.close();
}
SerialPortBinding.close(this.fd, function(err) {
this.closing = false;
if (err) {
@ -366,7 +360,7 @@ SerialPort.prototype._disconnected = function(err) {
};
SerialPort.prototype.close = function(callback) {
var fd = this.fd;
this.paused = true;
if (this.closing) {
debug('close attempted, but port is already closing');
@ -385,7 +379,7 @@ SerialPort.prototype.close = function(callback) {
this.readable = false;
this.serialPoller.close();
}
SerialPortBinding.close(fd, function(err) {
SerialPortBinding.close(this.fd, function(err) {
this.closing = false;
if (err) {
debug('SerialPortBinding.close had an error', err);

View file

@ -494,30 +494,18 @@ describe('SerialPort', function() {
});
describe('disconnections', function() {
it('emits a disconnect event', function(done) {
it('emits a disconnect event and closes the port', function(done) {
var port = new SerialPort('/dev/exists', function() {
assert.isTrue(port.isOpen());
hardware.disconnect('/dev/exists');
});
port.on('disconnect', function(err) {
assert.instanceOf(err, Error);
var spy = sandbox.spy();
port.on('disconnect', spy);
port.on('close', function() {
assert.isFalse(port.isOpen());
assert(spy.calledOnce);
done();
});
});
// Disconnects are inconsistent bananas
// it('closes the port', function(done) {
// var port = new SerialPort('/dev/exists', function () {
// expect(port.fd).to.equal(0);
// hardware.disconnect('/dev/exists');
// });
// var spy = sandbox.spy();
// port.on('disconnect', spy);
// port.on('close', function(err) {
// assert.instanceOf(err, Error);
// assert.isFalse(port.isOpen());
// assert(spy.calledOnce);
// done();
// });
// });
});
});