Skip to content

Commit 8bb873d

Browse files
committed
Adds abort signal to requests
Enables request abortion using AbortSignal in all transports. The change ensures requests can be cancelled, prevents memory leaks and improves user experience by rejecting requests that are no longer relevant. Implements AbortError class to differentiate RPC errors from aborts. Ran `npm i baseline-browser-mapping@latest -D` to update browser mappings, and `npm audit fix` to fix 3 vulnerabilities (1 low, 1 moderate, 1 high).
1 parent 747c639 commit 8bb873d

17 files changed

+2646
-8549
lines changed

README.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,40 @@ main().then(() => {
120120

121121
</details>
122122

123+
<details>
124+
<summary>AbortController</summary>
125+
126+
```javascript
127+
import { RequestManager, Client, HTTPTransport, AbortError } from "@open-rpc/client-js";
128+
129+
const transport = new HTTPTransport("http://localhost:3333");
130+
const requestManager = new RequestManager([transport]);
131+
const client = new Client(requestManager);
132+
133+
const main = async () => {
134+
const controller = new AbortController();
135+
const signal = controller.signal;
136+
137+
setTimeout(() => {
138+
controller.abort();
139+
}, 100);
140+
141+
try {
142+
await client.request({ method: "long_running_method", params: [], timeout: 5000, signal });
143+
} catch (e) {
144+
if (e instanceof AbortError) {
145+
console.log("Request was aborted");
146+
} else {
147+
console.error(e);
148+
}
149+
}
150+
};
151+
152+
main();
153+
```
154+
155+
</details>
156+
123157
### Building
124158

125159
```sh

0 commit comments

Comments
 (0)