From 1f1205b153110b2ce9edc47a4b7001359785c190 Mon Sep 17 00:00:00 2001 From: dmgnr Date: Wed, 30 Sep 2026 17:19:08 +0700 Subject: [PATCH] fix: attempt to fix sse --- lib/db/redis.ts | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/lib/db/redis.ts b/lib/db/redis.ts index 896b313..7d6d76e 100755 --- a/lib/db/redis.ts +++ b/lib/db/redis.ts @@ -55,15 +55,19 @@ export class PubSubManager { function ping() { clearTimeout(heartbeatTimeout); heartbeatTimeout = setTimeout(() => { - void writer.write(":)\n\n").catch(() => {}); + try { + void writer.write(":)\n\n").catch(() => { }); + } catch { } ping(); }, 90000); // cloudflare timeout = 100s } const write = (payload: PubPayload) => { - void writer - .write(this.constructMessage(payload.data, payload.event)) - .catch(() => {}); + try { + void writer + .write(this.constructMessage(payload.data, payload.event)) + .catch(() => { }); + } catch { } ping(); }; @@ -83,6 +87,7 @@ export class PubSubManager { console.log(` DSC ${topic}`); sub.unsubscribe(`${this.prefix}${topic}`, handler); + void writer.close().catch(() => { }); clearInterval(heartbeatTimeout); clearTimeout(timeout); }; @@ -122,7 +127,7 @@ export class EventSourceEndpoint { constructor( private endpoint: string, private eventMap: T, - ) {} + ) { } pub(event: K, data: z.infer) { if (!this.manager) @@ -139,8 +144,8 @@ export class EventSourceEndpoint { events: Partial<{ [K in keyof T]: (data: z.infer) => void }>, { endpoint = this.defaultEndpointUrl, - onerror = () => {}, - onopen = () => {}, + onerror = () => { }, + onopen = () => { }, }: SubOption = {}, ) { const es = new ReconnectingEventSource(endpoint); @@ -159,8 +164,8 @@ export class EventSourceEndpoint { callback: (data: z.infer) => void, { endpoint = this.defaultEndpointUrl, - onerror = () => {}, - onopen = () => {}, + onerror = () => { }, + onopen = () => { }, }: SubOption = {}, ) { const listener = (e: MessageEvent) => callback(JSON.parse(e.data));