From b5abab0b94ad72998cbae9543d48c12ed0810763 Mon Sep 17 00:00:00 2001 From: Simon Date: Mon, 18 Sep 2023 04:16:05 +0200 Subject: [PATCH] fix(requests): preventConcurrentRequest along with staleTime (#486) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit trackRequestResult no longer considers staleTime when skipping a request with status: fetching. Previously it was possible to get concurrent requests even if preventConcurrentRequest was set because all requests that were 'fetching' were also stale. Co-authored-by: Simon Hemåker --- .../requests/src/lib/requests-result.spec.ts | 31 +++++++++++++++++++ packages/requests/src/lib/requests-result.ts | 3 +- 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/packages/requests/src/lib/requests-result.spec.ts b/packages/requests/src/lib/requests-result.spec.ts index c371b31b..07bc26b8 100644 --- a/packages/requests/src/lib/requests-result.spec.ts +++ b/packages/requests/src/lib/requests-result.spec.ts @@ -605,6 +605,37 @@ describe('requests result', () => { expect(reqSpy).toHaveBeenCalledTimes(2); }); + it('should prevent concurrent requests when stale time is set', () => { + jest.useFakeTimers(); + + const { state, config } = createState(withEntities()); + + const store = new Store({ state, config, name: 'todos' }); + const reqSpy = jest.fn(); + + function getTodos() { + return timer(1000).pipe( + map(() => [createTodo(1)]), + tap(() => reqSpy()), + tap((todos) => store.update(setEntities(todos))), + trackRequestResult(['todos'], { staleTime: 900_000, preventConcurrentRequest: true }) + ); + } + + getTodos().subscribe(); + getTodos().subscribe(); + + jest.runAllTimers(); + + expect(reqSpy).toHaveBeenCalledTimes(1); + + getTodos().subscribe(); + + jest.runAllTimers(); + + expect(reqSpy).toHaveBeenCalledTimes(1); + }); + it('should request when staleTime pass', () => { jest.useFakeTimers(); diff --git a/packages/requests/src/lib/requests-result.ts b/packages/requests/src/lib/requests-result.ts index 9f3fb4c8..1d29addf 100644 --- a/packages/requests/src/lib/requests-result.ts +++ b/packages/requests/src/lib/requests-result.ts @@ -214,8 +214,7 @@ export function trackRequestResult( if ( result.fetchStatus === 'fetching' && preventConcurrentRequest && - !options?.skipCache && - !stale + !options?.skipCache ) { return getRequestResult(key).pipe( filter((requestResult) => requestResult.fetchStatus === 'idle'),