badgateway/react-ketting

how to add support of abortController in react ketting

sazyadav opened this issue · 5 comments

I wanna add abortController support in react ketting.. Is there any way to use abortController in ketting get request?
@evert Please help on this

evert commented

A good place to start with be to add it to the GetOptions type, and from there see where else you would need to add it to drill all the way down to fetch(). I'm definitely open to this!

The trickiest thing to keep in mind is that multiple calls to get() de-duplicate, so if there's 2 separate calls to get(), and the first one aborts, it probably shouldn't cancel because the second one is still waiting. If you can solve that you're well on your way.

Thanks For your reply @evert .
In the current implementation how Can I pass abortController signal instance in react-ketting fetch/get request.
the way in below code snippet, passing the signal
useEffect(() => {
const abortController = new AbortController();

setIsLoading(true);
fetch(https://jsonplaceholder.typicode.com/posts/${postId}, {
signal: abortController.signal,
})
.then((response) => {
if (response.ok) {
return response.json();
}
return Promise.reject();
})
.then((fetchedPost: Post) => {
setPost(fetchedPost);
})
.finally(() => {
setIsLoading(false);
});

return () => {
abortController.abort();
};
}, [postId]);

The primary blocker I see for getting this to work is the private optionsToRequestInit function in resource.ts would not forward the AbortController signal onwards to the fetcher.

evert commented

So the main issue is, how are you going to deal with de-duplicated requests?

For example, it's possible to do 2 GET requests, but Ketting will ensure only 1 goes out. What happens if the first one AbortController and it got aborted. What happens if the second one got an AbortController and it got cancelled?

Maybe both GET requests can be invoked with the same signal? So cancelling any of them will cancel the other as well?