Skip to content

Commit

Permalink
Cancel pending transactions when calling destroy (#521)
Browse files Browse the repository at this point in the history
* add jsdocs

* allow to pass more opts to socket

* allow to pass AbortSignal to socket

* add abort signal example

* lint the code

* remove vscode/ from gitignore

* drop node v14 (reached EOL April 2023)

* cancel pending transactions on destroy

* remove comments
  • Loading branch information
AndreMaz authored Oct 23, 2023
1 parent 9a74d61 commit 0b6a7d8
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -637,6 +637,20 @@ class ModbusRTU extends EventEmitter {
return false;
}

/**
* Clears the timeout for all pending transactions.
* This essentially cancels all pending requests.
*/
_cancelPendingTransactions() {
if (Object.keys(this._transactions).length > 0) {
Object.values(this._transactions).forEach((transaction) => {
if (transaction._timeoutHandle) {
_cancelTimeout(transaction._timeoutHandle);
}
});
}
}

/**
* Close the serial port
*
Expand All @@ -661,6 +675,9 @@ class ModbusRTU extends EventEmitter {
* or failure.
*/
destroy(callback) {
// cancel all pending requests as we're closing the port
this._cancelPendingTransactions();

// close the serial port if exist and it has a destroy function
if (this._port && this._port.destroy) {
this._port.removeAllListeners("data");
Expand Down

0 comments on commit 0b6a7d8

Please sign in to comment.