vercel / swr

React Hooks for Data Fetching
https://swr.vercel.app
MIT License
30.52k stars 1.22k forks source link

`populateCache` is being deferred when `mutate` is called multiple times #2186

Open henriqemalheiros opened 2 years ago

henriqemalheiros commented 2 years ago

Bug report

Description / Observed Behavior

I'm using the mutate function to partially update the cached data (like this second example). When starting a second mutation before the first mutation is finished, the populateCache of the first one is ignored. Because of this, when adding things to a list, the final result will be incomplete, since the first mutation couldn't update the cache.

Expected Behavior

populateCache is always called, in order in which the mutations were called.

Repro Steps / Code Example

Additional Context

SWR version: 2.0.0-rc.0

koba04 commented 2 years ago

Thank you for reporting the issue👍
Currently, swr ignores populateCache intentionally to avoid race conditions. So the result of the first mutation is ignored in this case. In order to fix this avoiding the race condition problem, we might have to enqueue populateCache and apply it in the right order.

alexnsorensen commented 1 year ago

Is there an update on getting this fixed as it's a bit of a deal breaker for me using this in my project.