hacky icon indicating copy to clipboard operation
hacky copied to clipboard

feat: clean up intervals, subscriptions, etc. when unmounting

Open gustavopch opened this issue 2 years ago • 1 comments

I'd like to do something like this:

function* Component() {
  const interval = setInterval(() => {
    // Do something
  }, 1000);

  try {
    while (true) {
      yield html`Hi`;
    }
  } finally {
    // Runs when unmounting
    clearTimeout(interval);
  }
}

Can it be supported? Or is there already some way to do it?

EDIT: @aidenybai From what I understand, Crank.js does that by calling return() on the generator when unmounting, so JS will automatically run the finally blocks. Seems relatively simple. 🤔

gustavopch avatar Jun 16 '22 14:06 gustavopch

Will look into that return, seems simple (feels kinda like useEffect cleanup function)

aidenybai avatar Jun 16 '22 15:06 aidenybai