Resource with async cache

Whats the pattern for using resources with caching that may be async? What I mean by that is that my cache_get function returns a promise so I cannot set it to the initialValue option of resources. I want to display the cached value while I fetch the actual, (the cached value is considered stale) so I cannot simply add it to the fetcher. I thought that using mutate with res.latest would give the desired behaviour, where Suspense is only triggered when initially, and not-triggered when cached value is set. Here are my two attempts: one with transitions: https://playground.solidjs.com/anonymous/6b0c7b0f-d2ec-458a-8536-883da3ae55a6 and one with additional signals: https://playground.solidjs.com/anonymous/ef897a99-c8da-4a17-84a4-68772a0cb934 Although they do kinda work, they feel incorrect. They both have the same problem that the "user" cannot easily control whether he wants to show the cached value or not. (As they could with res.latest and initialValue) It seems that maybe calling mutate should set resuorce into "refreshing" state.
4 Replies
peerreynders
peerreynders3mo ago
Based on what I know now about cache/action/createAsync I'd be inclined to not mess with the resource but instead use its state property to determine whether to show the “fake” value—i.e. refrain from injecting the fake value into the data flow entirely but instead simply use the fake value in the UI when the resource value is in an “unavailable” state. https://playground.solidjs.com/anonymous/dbec00e1-ec70-4f36-a9c9-8b490612d5a0
Solid Playground
Quickly discover what the solid compiler will generate from your JSX template
thetarnav
thetarnav3mo ago
thanks I like the explicitness here just not sure why it has 4 stages?
loading -> one -> cached -> two?
loading -> one -> cached -> two?
but I get the main idea
peerreynders
peerreynders3mo ago
Here I wrapped the async cacher in its own resource https://playground.solidjs.com/anonymous/d2cf1f4e-5488-4638-9cb3-6851f445f698
Solid Playground
Quickly discover what the solid compiler will generate from your JSX template
peerreynders
peerreynders3mo ago
Lets say you cache your values in IndexedDB. Before the initial fetch you should be able to get the value from the last session from IndexedDB (that you want to show) before you're first fetch comes back.