File tree Expand file tree Collapse file tree 1 file changed +28
-0
lines changed Expand file tree Collapse file tree 1 file changed +28
-0
lines changed Original file line number Diff line number Diff line change @@ -148,6 +148,10 @@ export class SerialPortStream<T extends BindingInterface = BindingInterface> ext
148148 * @emits open
149149 */
150150 open ( openCallback ?: ErrorCallback ) : void {
151+ if ( this . destroyed ) {
152+ return this . _asyncError ( new Error ( 'Port is already destroyed - it cannot be reopened' ) , openCallback )
153+ }
154+
151155 if ( this . isOpen ) {
152156 return this . _asyncError ( new Error ( 'Port is already open' ) , openCallback )
153157 }
@@ -473,6 +477,30 @@ export class SerialPortStream<T extends BindingInterface = BindingInterface> ext
473477 } ,
474478 )
475479 }
480+
481+ /**
482+ * Implementation for Duplex._destroy. Disposes of underlying resources and forbids this port from being reopened
483+ * @param err
484+ * @param callback
485+ */
486+ _destroy ( err : Error | null , callback : ErrorCallback ) {
487+ debug ( '_destroy' )
488+ if ( this . port ) {
489+ debug ( '_destroy' , 'releasing port' )
490+ this . port . close ( ) . then (
491+ ( ) => {
492+ callback ( err )
493+ } ,
494+ e => {
495+ callback ( e )
496+ } ,
497+ )
498+ this . port = undefined
499+ } else {
500+ debug ( '_destroy' , 'nothing to do; port has not been opened' )
501+ callback ( err )
502+ }
503+ }
476504}
477505
478506/**
You can’t perform that action at this time.
0 commit comments