Skip to content

Commit

Permalink
Implement Duplex destroy
Browse files Browse the repository at this point in the history
  • Loading branch information
rotu committed Dec 26, 2024
1 parent 701ffd8 commit 33bf24c
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions packages/stream/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,10 @@ export class SerialPortStream<T extends BindingInterface = BindingInterface> ext
* @emits open
*/
open(openCallback?: ErrorCallback): void {
if (this.destroyed) {
return this._asyncError(new Error('Port is already destroyed - it cannot be reopened'), openCallback)
}

if (this.isOpen) {
return this._asyncError(new Error('Port is already open'), openCallback)
}
Expand Down Expand Up @@ -473,6 +477,30 @@ export class SerialPortStream<T extends BindingInterface = BindingInterface> ext
},
)
}

/**
* Implementation for Duplex._destroy. Disposes of underlying resources and forbids this port from being reopened
* @param err
* @param callback
*/
_destroy(err: Error | null, callback: ErrorCallback) {
debug('_destroy')
if (this.port) {
debug('_destroy', 'releasing port')
this.port.close().then(
() => {
callback(err)
},
e => {
callback(e)
},
)
this.port = undefined
} else {
debug('_destroy', 'nothing to do; port has not been opened')
callback(err)
}
}
}

/**
Expand Down

0 comments on commit 33bf24c

Please sign in to comment.