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

How to clean ressources ? #20

Open
ScreamZ opened this issue Mar 27, 2023 · 9 comments
Open

How to clean ressources ? #20

ScreamZ opened this issue Mar 27, 2023 · 9 comments

Comments

@ScreamZ
Copy link

ScreamZ commented Mar 27, 2023

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.

@vezaynk
Copy link

vezaynk commented Oct 26, 2023

I too am wondering about this. I asked on Stackoverflow.

@abernier
Copy link
Member

@vezaynk
Copy link

vezaynk commented Oct 26, 2023

@abernier so is the answer "use an effect"?

@abernier
Copy link
Member

abernier commented Oct 26, 2023

that's how I did yes -- that way if a dep changes (same as your suspend, in my case videoTextureSrc), it will be cleaned up and cleared

@vezaynk
Copy link

vezaynk commented Oct 27, 2023

@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.

@abernier
Copy link
Member

I thought like you primarily, I even asked about that on Discord

From @drcmda's answer, I guess this decoupling is intentional:

suspense is a cache, unmount means nothing to a cached entry, it will continue to be available

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 useEffect

@vezaynk
Copy link

vezaynk commented Oct 27, 2023

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.

@nassosyian
Copy link

nassosyian commented Jul 17, 2024

I found an implementation of a "useMemoCleanup" hook from here
that performs cleanup.

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
(even though i haven't heavily tested it)

hope this helps someone

@vezaynk
Copy link

vezaynk commented Jul 18, 2024

@nassosyian How does this work? Wouldn't the memorized value be lost when suspense throws to the boundary?

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

4 participants