Refactor: Extract resolveWaiting helper to reduce code duplication

- Created resolveWaiting helper function to centralize the logic for resolving and cleaning up waiting entries
- Simplified test error message to avoid redundancy
- This improves code maintainability by avoiding duplication

Co-authored-by: arthurfiorette <47537704+arthurfiorette@users.noreply.github.com>
This commit is contained in:
copilot-swe-agent[bot] 2025-12-08 13:33:27 +00:00
parent bebc2fbb6a
commit d1b571a640
2 changed files with 23 additions and 43 deletions

View File

@ -34,6 +34,25 @@ export function defaultResponseInterceptor(axios: AxiosCacheInstance): ResponseI
}
};
/**
* Resolves the waiting deferred for a response, if present, and removes it from the waiting map.
*/
const resolveWaiting = (responseId: string) => {
const waiting = axios.waiting.get(responseId);
if (waiting) {
waiting.resolve();
axios.waiting.delete(responseId);
if (__ACI_DEV__) {
axios.debug({
id: responseId,
msg: 'Found waiting deferred(s) and resolved them'
});
}
}
};
const onFulfilled: ResponseInterceptor['onFulfilled'] = async (response) => {
// When response.config is not present, the response is indeed a error.
if (!response?.config) {
@ -114,18 +133,7 @@ export function defaultResponseInterceptor(axios: AxiosCacheInstance): ResponseI
}
// Clean up the waiting map if the cache was removed (e.g., due to maxEntries eviction)
const waiting = axios.waiting.get(response.id);
if (waiting) {
waiting.resolve();
axios.waiting.delete(response.id);
if (__ACI_DEV__) {
axios.debug({
id: response.id,
msg: 'Cleaned up waiting entry for non-loading cache state'
});
}
}
resolveWaiting(response.id);
return response;
}
@ -227,19 +235,7 @@ export function defaultResponseInterceptor(axios: AxiosCacheInstance): ResponseI
await axios.storage.set(response.id, newCache, config);
// Resolve all other requests waiting for this response
const waiting = axios.waiting.get(response.id);
if (waiting) {
waiting.resolve();
axios.waiting.delete(response.id);
if (__ACI_DEV__) {
axios.debug({
id: response.id,
msg: 'Found waiting deferred(s) and resolved them'
});
}
}
resolveWaiting(response.id);
if (__ACI_DEV__) {
axios.debug({
@ -362,19 +358,7 @@ export function defaultResponseInterceptor(axios: AxiosCacheInstance): ResponseI
config
);
// Resolve all other requests waiting for this response
const waiting = axios.waiting.get(id);
if (waiting) {
waiting.resolve();
axios.waiting.delete(id);
if (__ACI_DEV__) {
axios.debug({
id,
msg: 'Found waiting deferred(s) and resolved them'
});
}
}
resolveWaiting(id);
if (__ACI_DEV__) {
axios.debug({

View File

@ -39,11 +39,7 @@ describe('Waiting Memory Leak', () => {
assert.ok(result2.data);
// The waiting map should be empty
assert.equal(
axios.waiting.size,
0,
`waiting map should be empty but has ${axios.waiting.size} entries`
);
assert.equal(axios.waiting.size, 0, 'waiting map should be empty but has entries');
});
it('should handle multiple waves of concurrent requests with maxEntries', async () => {