diff --git a/src/interceptors/response.ts b/src/interceptors/response.ts index 4a9f61a..618635c 100644 --- a/src/interceptors/response.ts +++ b/src/interceptors/response.ts @@ -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({ diff --git a/test/interceptors/waiting-memory-leak.test.ts b/test/interceptors/waiting-memory-leak.test.ts index aa58140..739312a 100644 --- a/test/interceptors/waiting-memory-leak.test.ts +++ b/test/interceptors/waiting-memory-leak.test.ts @@ -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 () => {