Skip to content

Commit

Permalink
corrected error handling on connect, remove existing data listeners o…
Browse files Browse the repository at this point in the history
…n parse if connect is called multiple times
  • Loading branch information
mwittig committed Apr 9, 2019
1 parent 8a34a59 commit 6f8fea5
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
5 changes: 4 additions & 1 deletion lib/driver/serialport.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ SerialPortDriver = (function(superClass) {
this.serialPort.removeAllListeners('error');
this.serialPort.removeAllListeners('data');
this.serialPort.removeAllListeners('close');
if (this.parser != null) {
this.parser.removeAllListeners('data');
}
this.serialPort.on('error', (function(_this) {
return function(error) {
return _this.emit('error', error);
Expand Down Expand Up @@ -85,7 +88,7 @@ SerialPortDriver = (function(superClass) {
_this.serialPort.writeAsync("PING\n")["catch"](reject);
return _this.once("ready", resolver);
});
});
})["catch"](reject);
};
})(this)).timeout(timeout)["catch"]((function(_this) {
return function(err) {
Expand Down
3 changes: 2 additions & 1 deletion src/driver/serialport.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class SerialPortDriver extends events.EventEmitter
@serialPort.removeAllListeners('error')
@serialPort.removeAllListeners('data')
@serialPort.removeAllListeners('close')
@parser.removeAllListeners('data') if @parser?

@serialPort.on('error', (error) => @emit('error', error) )
@serialPort.on('close', =>
Expand Down Expand Up @@ -62,7 +63,7 @@ class SerialPortDriver extends events.EventEmitter
@serialPort.writeAsync("PING\n").catch(reject)
@once("ready", resolver)
)
)
).catch(reject)
).timeout(timeout).catch( (err) =>
@removeListener("ready", resolver)
@serialPort.removeAllListeners('data')
Expand Down

0 comments on commit 6f8fea5

Please sign in to comment.