Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle infinite loading states when a restart occurs in the middle of a request. #612

Closed
arthurfiorette opened this issue Jul 19, 2023 · 0 comments
Assignees
Labels
enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed

Comments

@arthurfiorette
Copy link
Owner

When using any kind of persisted storage, the app may restart while a request is in loading waiting for it to finish. In this case, the request will arrive here:

/* istanbul ignore if 'really hard to test' */
if (!deferred) {
await axios.storage.remove(id, config);
// Hydrates any UI temporarily, if cache is available
if (cache.data) {
await config.cache.hydrate?.(cache);
}
return config;
}

Which is when we have a loading state but no deferred is registered (as it is a in memory promise hash map). Previously we just deleted the cache and continued with a empty response to be handled by the response interceptor.

If the response of this request comes successful, it would ignore caching and return here:

if (
// If the request interceptor had a problem or it wasn't cached
cache.state !== 'loading'
) {
if (__ACI_DEV__) {
axios.debug?.({
id,
msg: "Response not cached and storage isn't loading",
data: { cache, response }

Or, if this response failed, it would re-throw the error thinking the interceptor broke:

if (
// This will only not be loading if the interceptor broke
cache.state !== 'loading' ||
cache.previous !== 'stale'
) {
await rejectResponse(id, config);
if (__ACI_DEV__) {
axios.debug?.({
id,
msg: 'Caught an error in the request interceptor',

The responses interceptors are right to expect a loading state, otherwise non loading requests should've been resolved from the request interceptor already.

To solve this problem, the check below must be done in the ignoreAndRequest code block to fix the cache to loading before returning the response.

if (cache.state === 'loading') {
const deferred = axios.waiting[id];

@arthurfiorette arthurfiorette added enhancement New feature or request help wanted Extra attention is needed good first issue Good for newcomers labels Jul 19, 2023
@arthurfiorette arthurfiorette self-assigned this Jul 19, 2023
@arthurfiorette arthurfiorette changed the title Handle infinite loading states when a restart in the middle of a request happens. Handle infinite loading states when a restart occurs in the middle of a request happens. Jul 20, 2023
@arthurfiorette arthurfiorette changed the title Handle infinite loading states when a restart occurs in the middle of a request happens. Handle infinite loading states when a restart occurs in the middle of a request. Jul 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

1 participant