Skip to content

Commit

Permalink
fix: updated for closing stream more safety (for pass tests)
Browse files Browse the repository at this point in the history
  • Loading branch information
JS-AK committed Nov 8, 2024
1 parent 20839e9 commit 9b0181a
Showing 1 changed file with 4 additions and 41 deletions.
45 changes: 4 additions & 41 deletions src/transit.js
Original file line number Diff line number Diff line change
Expand Up @@ -1016,8 +1016,8 @@ class Transit {
removePendingRequest(id) {
this.pendingRequests.delete(id);

this._softDeletePendingReqStream(id);
this._softDeletePendingResStream(id);
this.pendingReqStreams.delete(id);
this.pendingResStreams.delete(id);
}

/**
Expand Down Expand Up @@ -1056,22 +1056,6 @@ class Transit {
});
}

/**
* Internal method to delete a pending response stream from `pendingResStreams`
* and soft-end it (if not already ended) without error.
*
* @param {String} id ID of the stream in `pendingResStreams`
* @memberof Transit
*/
_softDeletePendingResStream(id) {
const stream = this.pendingResStreams.get(id);
this.pendingResStreams.delete(id);

if (stream) {
this._destroyStreamIfPossible(stream);
}
}

/**
* Internal method to delete a pending response stream from `pendingResStreams`
* and destroy it (if not already destroyed) with error.
Expand All @@ -1089,23 +1073,6 @@ class Transit {
}
}

/**
* Internal method to delete a pending request stream from `pendingReqStreams`
* and soft-end it (if not already ended) without error.
*
* @param {String} id ID of the stream in `pendingReqStreams`
* @memberof Transit
*/
_softDeletePendingReqStream(id) {
const reqStream = this.pendingReqStreams.get(id);
this.pendingReqStreams.delete(id);
const pass = reqStream ? reqStream.stream : undefined;

if (pass) {
this._destroyStreamIfPossible(pass);
}
}

/**
* Internal method to delete a pending request stream from `pendingReqStreams`
* and destroy it (if not already ended) with error.
Expand All @@ -1116,8 +1083,8 @@ class Transit {
*/
_deletePendingReqStream(id, origin) {
const reqStream = this.pendingReqStreams.get(id);
this.pendingReqStreams.delete(id);
const pass = reqStream ? reqStream.stream : undefined;
this.pendingReqStreams.delete(id);

if (pass) {
this._destroyStreamIfPossible(pass, `Stream closed by ${origin}`);
Expand All @@ -1134,11 +1101,7 @@ class Transit {
*/
_destroyStreamIfPossible(stream, errorMessage) {
if (!stream.destroyed && stream.destroy) {
if (errorMessage) {
stream.destroy(new Error(errorMessage));
} else {
stream.destroy();
}
stream.destroy(new Error(errorMessage));
}
}

Expand Down

0 comments on commit 9b0181a

Please sign in to comment.