Skip to content

Commit

Permalink
Clarify write promise flow and handle Socket.write throwing
Browse files Browse the repository at this point in the history
  • Loading branch information
Abestanis committed Sep 4, 2024
1 parent 1b6479c commit a9959d9
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions ports/tcpport.js
Original file line number Diff line number Diff line change
Expand Up @@ -242,15 +242,26 @@ class TcpPort extends EventEmitter {
});

// send buffer to slave
this._writeCompleted = new Promise((resolve) => {
this._writeCompleted.finally(() => {
if (this._client.write(buffer)) {
resolve();
} else {
this._client.once("drain", resolve);
let previousWritePromise = this._writeCompleted;
let newWritePromise = new Promise((resolveNewWrite, rejectNewWrite) => {

Check failure on line 246 in ports/tcpport.js

View workflow job for this annotation

GitHub Actions / test (18.x)

'newWritePromise' is never reassigned. Use 'const' instead.

Check failure on line 246 in ports/tcpport.js

View workflow job for this annotation

GitHub Actions / test (20.x)

'newWritePromise' is never reassigned. Use 'const' instead.
// Wait for the completion of any write that happened before.
previousWritePromise.finally(() => {
try {
// The previous write succeeded, write the new buffer.
if (this._client.write(buffer)) {
// Mark this write as complete.
resolveNewWrite();
} else {
// Wait for one `drain` event to mark this write as complete.
this._client.once("drain", resolveNewWrite);
}
} catch (error) {
rejectNewWrite(error);
}
});
});
// Overwrite `_writeCompleted` so that the next call to `TcpPort.write` will have to wait on our write to complete.
this._writeCompleted = newWritePromise;

// set next transaction id
this._transactionIdWrite = (this._transactionIdWrite + 1) % MAX_TRANSACTIONS;
Expand Down

0 comments on commit a9959d9

Please sign in to comment.