-
Notifications
You must be signed in to change notification settings - Fork 26
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
How to clean ressources ? #20
Comments
I too am wondering about this. I asked on Stackoverflow. |
@abernier so is the answer "use an effect"? |
that's how I did yes -- that way if a dep changes (same as your suspend, in my case |
@abernier That's not ideal because it de-couples the effect from it's clean-up. Suspend-react is aware of when the dependencies change, and so is best-positioned to run clean-up effects at the right time. |
I thought like you primarily, I even asked about that on Discord From @drcmda's answer, I guess this decoupling is intentional:
which makes sense to me, it's a cache, nothing more If you want more, like a side-effect when your cache-key changes, then you have |
I see. Sounds like we should be wrapping suspend in our own hook. Something like this. const useSuspense(cb, deps) {
const { value, cleanup } = suspend(cb)
useEffect(() => {
() => cleanup()
}, deps)
return value;
} ...but then we get the pitfall from a lack of reference-counting if two components use the same suspended keys. useEffect(() => {
() => {
if (lastRef) evictFromCache();
cleanup()
}
}, deps) Makes me think suspend-react should be providing this. |
I found an implementation of a "useMemoCleanup" hook from here i've been using it in my own project like so: const texture = useMemoCleanup( () =>
[
suspend(async () => {
// your code here
return texture;
}, []),
// cleanup func
() => {
texture.dispose();
}
], [] ); and it seems to be working so far hope this helps someone |
@nassosyian How does this work? Wouldn't the memorized value be lost when |
Any way to clean resources using this library. I would like to open a socket using suspense but when the Host change, i need to reload a new socket and would like to clean effect the previous.
The text was updated successfully, but these errors were encountered: