From 36be232bb1baf2527e30d1e5c341e82e3bf80e28 Mon Sep 17 00:00:00 2001 From: Francis Gulotta Date: Thu, 16 Jun 2016 00:21:42 -0400 Subject: [PATCH] Cleanup disconnect path (#855) - make the disconnect path more consistent - call callback in the context of the port --- lib/serialport.js | 32 +++++++++++++------------------- test/serialport.js | 26 +++++++------------------- 2 files changed, 20 insertions(+), 38 deletions(-) diff --git a/lib/serialport.js b/lib/serialport.js index d549da3..90ba557 100644 --- a/lib/serialport.js +++ b/lib/serialport.js @@ -2,10 +2,6 @@ // Copyright 2011 Chris Williams -// 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); diff --git a/test/serialport.js b/test/serialport.js index 7b82809..746e172 100644 --- a/test/serialport.js +++ b/test/serialport.js @@ -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(); - // }); - // }); }); });