From 33bf24ccf95bbe4eb6b6db5aaf176984b6248034 Mon Sep 17 00:00:00 2001 From: Dan Rose Date: Fri, 21 Jun 2024 16:32:10 -0500 Subject: [PATCH] Implement Duplex destroy --- packages/stream/lib/index.ts | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/packages/stream/lib/index.ts b/packages/stream/lib/index.ts index 6272499c2..9262beff0 100644 --- a/packages/stream/lib/index.ts +++ b/packages/stream/lib/index.ts @@ -148,6 +148,10 @@ export class SerialPortStream 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) } @@ -473,6 +477,30 @@ export class SerialPortStream 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) + } + } } /**