preactjs / signals

Manage state with style in every framework
https://preactjs.com/blog/introducing-signals/
MIT License
3.81k stars 95 forks source link

Garbage collection in useSignal() when component is unmounted? #564

Closed ashah888 closed 6 months ago

ashah888 commented 6 months ago

I'm using useSignal() for the first time and I want to use it to store data from the server.

In this code and example, will the signal data be data collected or should I do something else to properly handle the unmount?

const abc = useSignal({a: "apple"});

useEffect(() => {
  apiRequest().then(() => {
    abc.value.a = "abraham";
  });
}, []);

In this code, if the user navigates away from the page and "unmounts" this component before the API request finishes, will "abc" signal be properly garbage collected? Or will it live in memory until I do something about it?

XantreDev commented 6 months ago

It will live on memory until closure that references it alive. But you should n't handle this case especially, it just will be assigned to a new value (when the promise is resolved), after this - it will be collected in the next gc cycle

rschristian commented 6 months ago

Yeah it should be GC'd w/out issue -- if you find anything to the contrary, then we can take a look.