razshare / sveltekit-sse

Server Sent Events with SvelteKit
https://www.npmjs.com/package/sveltekit-sse
MIT License
306 stars 9 forks source link

Server side loop keeps running even if page is closed #12

Closed luccas40 closed 1 year ago

luccas40 commented 1 year ago

Code is having the same behavior commented on the issue #6 , It's maintaining the connection even if I close the page. Reload page and now I have 2 loops. I tried the solution mentioned there but the function onCancel is never called. Here is my API GET function:

export async function GET(){
    let keepAlive = true;
    return event(async emit => {
        const a = Math.random();
        while(keepAlive){
            console.log(a);
            await Delay(1000);
        }        
    }).onCancel(() => {
        console.log("closed");
        keepAlive = false;
    }).toResponse();
}

And this is my output:

0.745325751723089
0.4290714386471284
0.9218169541304038
0.9261938753155319
0.18358792724190764
0.5849592777398782
0.8884102611692648
0.8990577568419466
0.49825546939853327
0.21722120556493907
0.8292935102246708
0.9955188908531237
0.3307339104582989
0.7303807104681799
0.8000301013653071
0.40561914517802555
0.49165112478635176

Again the console log "closed" never shows up. I also have SSR disabled on the page consuming the API. I might be missing something cause I'm new to svelte.

razshare commented 1 year ago

Hey @luccas40 , I ran your code and it works correctly for me.

Peek 2023-10-25 22-36

I ran it with both bun 1.0.7 and node v18.17.1 and it works correctly.

I just published a repo and the code here https://github.com/tncrazvan/sveltekit-sse-issue-12

Can you please clone it, try to run it and let me know if it works on your end?

Also, can you create a repo of your own reproducing your issue and post it here?

luccas40 commented 1 year ago

Just cloned your repo, installed the packages and It is still doing the same behavior

~/sveltekit-sse-issue-12$ bun run --bun dev --host
$ vite dev --host
▲ [WARNING] Cannot find base config file "./.svelte-kit/tsconfig.json" [tsconfig.json]

    jsconfig.json:2:12:
      2 │   "extends": "./.svelte-kit/tsconfig.json",
        ╵              ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Forced re-optimization of dependencies

  VITE v4.5.0  ready in 991 ms

  ➜  Local:   http://localhost:5173/
  ➜  Network: http://192.168.15.100:5173/
  ➜  press h to show help
5:06:24 PM [vite] ✨ new dependencies optimized: sveltekit-sse
5:06:24 PM [vite] ✨ optimized dependencies changed. reloading
0.8399509345285102
0.8112408000210155
0.8399509345285102
0.8112408000210155
0.8399509345285102
0.8112408000210155
0.8399509345285102
0.8112408000210155
0.8399509345285102
0.8112408000210155
0.8399509345285102
0.8112408000210155
0.8399509345285102
0.8112408000210155
0.8399509345285102
luccas40 commented 1 year ago

Okay, running with node apparently fixed the problem. Seems like is some weird interaction with Bun Btw it needs to force to run with bun "--bun" or else it tries to run with node for some reason

razshare commented 1 year ago

Maybe you're using an older version of bun? They do seem to publish a new release almost every week fixing more and more bugs.

Btw it needs to force to run with bun "--bun" or else it tries to run with node for some reason

Bun this is actually weird though.

Can you run bun upgrade and see if it works after the upgrade? Otherwise we should let the bun guys know about this.

luccas40 commented 1 year ago

I'm already on the latest version 1.0.7

razshare commented 1 year ago

hello @luccas40 , I'm closing this issue since it doesn't seem to be related to the library itself. If you have more information that says otherwise I'll reopen it.

Meanwhile, if it really turns out to be a Bun issue, maybe you should open an issue on https://github.com/oven-sh/bun and let them know your hardware specs and os setup.