Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 38 additions & 1 deletion packages/requests/src/lib/requests-result.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
} from '@ngneat/elf-entities';
import { createTodo, Todo } from '@ngneat/elf-mocks';
import { expectTypeOf } from 'expect-type';
import { map, tap, timer } from 'rxjs';
import { map, Observable, tap, timer } from 'rxjs';
import {
clearRequestsResult,
filterSuccess,
Expand Down Expand Up @@ -768,4 +768,41 @@ describe('requests result', () => {
expect(reqSpy).toHaveBeenCalledTimes(1);
expect(spy).toHaveBeenCalledTimes(2);
});

it('should not cache aborted requests', () => {
jest.useFakeTimers();

const reqSpy = jest.fn();

function get() {
return new Observable((observer) => {
const id = setTimeout(() => {
observer.next();
observer.complete();
}, 1000);

return () => {
clearTimeout(id);
};
}).pipe(
trackRequestResult(['todos']),
tap(() => reqSpy()),
);
}

const subscription = get().subscribe();
subscription.unsubscribe();

jest.runAllTimers();

expect(reqSpy).toHaveBeenCalledTimes(0);

// Next request should not be cached since the first one was aborted

get().subscribe();

jest.runAllTimers();

expect(reqSpy).toHaveBeenCalledTimes(1);
});
});
14 changes: 14 additions & 0 deletions packages/requests/src/lib/requests-result.ts
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,20 @@ export function trackRequestResult<TData>(
}
}
},
finalize() {
const mainKey = resolveKey(key);
const currentResult = emitters.get(mainKey)?.getValue();
if (currentResult?.fetchStatus === 'fetching') {
updateRequestResult(key, {
isLoading: false,
isSuccess: false,
isError: false,
status: 'idle',
fetchStatus: 'idle',
staleTime: Date.now(),
});
}
},
}),
);
}),
Expand Down