Closed bbohlender closed 2 years ago
could you make a small pseudo code example what you need to accomplish? i don't understand what you mean when you say: persist working with suspense.
with preload and clear you have access to the cache, and you can store items that are then immediately accessible. is that unrelated?
Yeah, you are right, thanks!
preload
and peek should do the trick 👍
For completeness here the persist function I came up with 😄 (not tested yet)
function persist<T, Keys extends Array<unknown>, Fn extends (...keys: Keys) => T>(
fn: Fn,
keys: Keys,
config: Partial<{
lifespan?: number
equal?: (a: any, b: any) => boolean
}> = {}
): T {
let result = peek(keys) as T
if (result == null) {
result = fn(...keys)
preload(() => Promise.resolve(result), keys, config)
}
return result
}
I have a case where I need to persist a value through a suspension. So a value should be "persistent", similar to useMemo, but when working with suspense. I could use a suspend with a promise that resolves directly, but that is not ideal.
I like the cache implementation in this library pretty much, so I thought of adapting it to a "persist" library, but since both would have so much in common, maybe the addition of the persist function is a good fit here.
Additionally, I extracted the insert, delete and get functionality in a separate class so there can be two different caches for asynchronous cache entries and synchronous cache entries (that is how I called them, at least).
If my changes are too disruptive or the persist idea does not fit this library, I would love to hear some feedback on different solutions for my problem or different options to publish my solution.
Thanks 😄