Skip to content

Commit 01bd136

Browse files
committed
fix: clear inflightRequests after each batch RPC call
1 parent 485dcc6 commit 01bd136

File tree

2 files changed

+23
-33
lines changed

2 files changed

+23
-33
lines changed

.changeset/late-bears-call.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"thirdweb": patch
3+
---
4+
5+
fix: Clear inflightRequests after each batch RPC call

packages/thirdweb/src/rpc/rpc.ts

Lines changed: 18 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,6 @@ export function getRpcClient(
9090
DEFAULT_BATCH_TIMEOUT_MS;
9191

9292
// inflight requests
93-
// biome-ignore lint/suspicious/noExplicitAny: TODO: fix any
9493
const inflightRequests = new Map<string, Promise<any>>();
9594

9695
let pendingBatch: Array<{
@@ -100,9 +99,7 @@ export function getRpcClient(
10099
id: number;
101100
jsonrpc: "2.0";
102101
};
103-
// biome-ignore lint/suspicious/noExplicitAny: TODO: fix any
104102
resolve: (value: any) => void;
105-
// biome-ignore lint/suspicious/noExplicitAny: TODO: fix any
106103
reject: (reason?: any) => void;
107104
requestKey: string;
108105
}> = [];
@@ -138,46 +135,41 @@ export function getRpcClient(
138135
requestTimeoutMs: options.config?.requestTimeoutMs,
139136
})
140137
.then((responses) => {
141-
// for each response, resolve the inflight request
142138
activeBatch.forEach((inflight, index) => {
139+
// Handle the inflight request promise for each response.
143140
const response = responses[index];
144-
// if we didn't get a response at all, reject the inflight request
141+
142+
// No response.
145143
if (!response) {
146144
inflight.reject(new Error("No response"));
147-
return;
148145
}
149-
// handle errors in the response
150-
if (response instanceof Error) {
146+
// Response is an error or error string.
147+
else if (response instanceof Error) {
151148
inflight.reject(response);
152-
return;
153-
}
154-
155-
// handle strings as responses??
156-
if (typeof response === "string") {
149+
} else if ("error" in response) {
150+
inflight.reject(response.error);
151+
} else if (typeof response === "string") {
157152
inflight.reject(new Error(response));
158-
return;
159153
}
160-
161-
if ("error" in response) {
162-
inflight.reject(response.error);
163-
// otherwise, resolve the inflight request
164-
} else if (response.method === "eth_subscription") {
165-
// TODO: handle subscription responses
166-
throw new Error("Subscriptions not supported yet");
167-
} else {
154+
// eth_subscription is not supported yet.
155+
else if (response.method === "eth_subscription") {
156+
inflight.reject("Subscriptions not supported yet");
157+
}
158+
// Else return the successful response for the inflight request.
159+
else {
168160
inflight.resolve(response.result);
169161
}
170-
// remove the inflight request from the inflightRequests map
171-
inflightRequests.delete(inflight.requestKey);
172162
});
173163
})
174164
.catch((err) => {
175165
// http call failed, reject all inflight requests
176166
for (const inflight of activeBatch) {
177167
inflight.reject(err);
178-
// remove the inflight request from the inflightRequests map
179-
inflightRequests.delete(inflight.requestKey);
180168
}
169+
})
170+
.finally(() => {
171+
// Clear the inflight requests map so any new requests are re-fetched.
172+
inflightRequests.clear();
181173
});
182174
}
183175

@@ -186,9 +178,7 @@ export function getRpcClient(
186178
return async (request) => {
187179
// we can hard-code the id and jsonrpc version
188180
// we also mutate the request object here to avoid copying it
189-
// biome-ignore lint/suspicious/noExplicitAny: TODO: fix any
190181
(request as any).id = 1;
191-
// biome-ignore lint/suspicious/noExplicitAny: TODO: fix any
192182
(request as any).jsonrpc = "2.0";
193183
const rpcResponse = await fetchSingleRpc(rpcUrl, options.client, {
194184
request: request,
@@ -210,20 +200,15 @@ export function getRpcClient(
210200

211201
// if the request for this key is already inflight, return the promise directly
212202
if (inflightRequests.has(requestKey)) {
213-
// biome-ignore lint/style/noNonNullAssertion: the `has` check ensures this is defined
214203
return inflightRequests.get(requestKey)!;
215204
}
216-
// biome-ignore lint/suspicious/noExplicitAny: TODO: fix any
217205
let resolve: (value: any) => void;
218-
// biome-ignore lint/suspicious/noExplicitAny: TODO: fix any
219206
let reject: (reason?: any) => void;
220-
// biome-ignore lint/suspicious/noExplicitAny: TODO: fix any
221207
const promise = new Promise<any>((resolve_, reject_) => {
222208
resolve = resolve_;
223209
reject = reject_;
224210
});
225211
inflightRequests.set(requestKey, promise);
226-
// @ts-expect-error - they *are* definitely assgined within the promise constructor
227212
pendingBatch.push({ request, resolve, reject, requestKey });
228213
if (batchSize > 1) {
229214
// if there is no timeout, set one

0 commit comments

Comments
 (0)