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

Initiate returns undefined when unwrapping after resetApiState #4833

Open
Cambrionix-Matt opened this issue Jan 29, 2025 · 6 comments
Open

Comments

@Cambrionix-Matt
Copy link

I have places in my application where i call

const res = dispatch(api.endpoint.myEndpoint.initiate()).unwrap()

but when I call api.util.resetApiState() these return undefined, despite the type suggesting otherwise.

Either the initiate should reject, return the old response before the api is reset, or the types should be updated to match the behavior.

@markerikson
Copy link
Collaborator

@Cambrionix-Matt : what is the code sequence and timing of resetApiState() vs initiate()? Can you provide an example that repros this behavior?

@Matt-Ord
Copy link
Contributor

I think resetApIState was triggered around the same time the initiate was fulfilled.

I won't be able to repo properly until next week but the general structure was

API 1 has an endpoint getData1

API 2 has an endpoint getData2 which calls api1.getData.initiate(). unwrap () inside the base query 2

There is a listener middleware which listens for Api1.getData1.matchFufilled and dispatched api2.resetAPIState (ie around the same time api2.getData2 is unwrapped)

@markerikson
Copy link
Collaborator

If api2.resetApiState() is dispatched while that second request is still in progress, then yeah, it doesn't surprise me that this broke. That's not intended usage, and it's likely that the resolution logic now has to deal with an empty state that has no cache entry, because you told RTKQ to remove them.

@Matt-Ord
Copy link
Contributor

Yeah I'm not too surprised it went wrong - and the fix our end was just to not reset the API state as it was definitely not the right thing to be doing! I still think this is a bug if the types suggested it should never return undefined

@markerikson
Copy link
Collaborator

I'd disagree on that. The types are accurate for intended usage. adding | undefined here would force users to do additional checks that aren't necessary except in this specific broken scenario.

@Matt-Ord
Copy link
Contributor

I think the ideal fix is to reject all initiate which are pending before the cache is removed. In the intended scenario for resetAPIState would you be able to guarantee that it wouldn't have any queries which happen to be resolved at the same time as calling reset, and what would happen if the initiate has not yet resolved?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants