razshare / sveltekit-sse

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

[BUG] onCancel callbacks not firing #15

Closed Mautriz closed 1 year ago

Mautriz commented 1 year ago

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

Mautriz commented 1 year ago

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