vite-plugin-node icon indicating copy to clipboard operation
vite-plugin-node copied to clipboard

import.meta.hot.dispose not working?

Open schummar opened this issue 3 years ago • 2 comments

It seems disposing of side effects does not work. If I have some background task running (job scheduler, db watcher, etc.) in my server, I usually gracefully clean it up on shutdown by listening for signals.

In order for this to work in HMR, my understanding is that I need to register a dispose callback in the file with the side effect. E.g.:

const handle = setInterval(() => {
  console.log('ping');
}, 1000);

if (import.meta.hot) {
  import.meta.hot.accept();
  import.meta.hot.dispose(() => {
    console.log('dispose');
    clearInterval(handle);
  });
}

However, the callback is never called, so I'll have two intervals printing pings.

Reproduction: https://stackblitz.com/edit/node-qfyzmw?file=index.ts,fileWithSideEffect.ts Change e.g. 'ping' -> 'ping1' Reload right side

schummar avatar Jul 11 '22 09:07 schummar

Are there any news? Looks like a very major blocker

nick4fake avatar Jun 12 '23 06:06 nick4fake

https://github.com/vitejs/vite/issues/7887

nick4fake avatar Jun 12 '23 07:06 nick4fake