razshare / sveltekit-sse

Server Sent Events with SvelteKit

Home Page:https://www.npmjs.com/package/sveltekit-sse

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[BUG] onCancel callbacks not firing

Mautriz opened this issue · comments

EDIT: I just realized that it fires only when effectively CANCELLED, meaning that the stream should not return before clearing resources or stay open forever, my bad :D

Hi, I'm trying to work with eventlisteners which I must close when the request is closed in any way

The onCancel callback doesn't seem to do anything
Even if I close the window/browser nothing happens

Is there any way to accomplish this ?

import { events } from 'sveltekit-sse'

export const GET = async (request) => {
    const evts = events(async (emit) => {
        let x = 0;
        while (true) {
            await new Promise((resolve) => setTimeout(resolve, 1000))
            emit("message", 'message')
            if (x++ === 2) break
        }
    }).onCancel(() => console.log("FIRE"))

    return evts.toResponse()
}

I'm running on

  • nodejs20.10.0
  • sveltekit-sse 0.4.14
  • sveltekit 1.27.4
  • svelte 4.2.7
  • ubuntu

I just realized that it fires only when effectively CANCELLED, meaning that the stream should not return before clearing resources or stay open forever, my bad :D