Open majanojoel opened 2 months ago
Also come up with the same issue. It seems hooks.server.ts is never called with import.meta.hot
, therefore one could not do:
let interval = undefined;
if (dev) {
interval = setInterval(()=>{
console.log('pong');
},1000);
}
if (import.meta.hot) {
import.meta.hot.dispose(()=> {
if ( interval !== undefined ) {
clearInterval(interval);
interval = undefined;
}
}
Which seems from svelte-hmr
the way to go for disposing resource on HMR events.
Definitely related to #11932
Agreed it looks related. That issue has been open for some time now, so is there any ETA when it will be looked at?
Describe the bug
I have a function I would like to call periodically so I initialize a setInterval loop in
interval.ts
by importinghandle
inhooks.server.ts
. The return value from setInterval is exported ashandle
. The bug is that whenever theconsole.log
is updated, the output in the console shows there are two setInterval loops running. This output keeps stacking as more changes are made and more HMR updates are triggered. For example, if you update therunAsync(1)
call torunAsync(2)
you will see the following console output:I tried using
import.meta.hot
ininterval.ts
with adispose
followingaccept()
, as well as others likeinvalidate
andon('vite:beforeUpdate', ...)
but this behavior persisted or at best the first loop was the only one that kept running after HMR updates.This is all running using
npm run dev
. Am I doing something wrong withimport.meta.hot
or is there another way to cleanup the previous loop?Reproduction
https://stackblitz.com/edit/sveltejs-kit-template-default-2xzaqm?file=src%2Flib%2Fserver%2Finterval.ts
Logs
System Info
Severity
serious, but I can work around it
Additional Information
Reproduced using the SvelteKit template on StackBlitz.