Skip to content

Commit

Permalink
fix(requests): preventConcurrentRequest along with staleTime (#486)
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
citruspress and Simon Hemåker authored Sep 18, 2023
1 parent 8d30ccb commit b5abab0
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
31 changes: 31 additions & 0 deletions packages/requests/src/lib/requests-result.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Todo>());

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();

Expand Down
3 changes: 1 addition & 2 deletions packages/requests/src/lib/requests-result.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,8 +214,7 @@ export function trackRequestResult<TData>(
if (
result.fetchStatus === 'fetching' &&
preventConcurrentRequest &&
!options?.skipCache &&
!stale
!options?.skipCache
) {
return getRequestResult(key).pipe(
filter((requestResult) => requestResult.fetchStatus === 'idle'),
Expand Down

0 comments on commit b5abab0

Please sign in to comment.