(event: string, payload: unknown)
| 894 | let latestEventTimestamp: number | null = null; |
| 895 | |
| 896 | const send = (event: string, payload: unknown) => { |
| 897 | if (!active) { |
| 898 | return; |
| 899 | } |
| 900 | try { |
| 901 | res.write(`event: ${event}\n`); |
| 902 | res.write(`data: ${JSON.stringify(payload)}\n\n`); |
| 903 | // Flush headers if available (helps with immediate delivery) |
| 904 | if (typeof (res as any).flush === 'function') { |
| 905 | (res as any).flush(); |
| 906 | } |
| 907 | } catch (error) { |
| 908 | // Connection closed or error writing |
| 909 | console.warn(`Failed to send SSE event ${event}:`, error); |
| 910 | if (!active) { |
| 911 | return; |
| 912 | } |
| 913 | active = false; |
| 914 | void cleanup(); |
| 915 | } |
| 916 | }; |
| 917 | |
| 918 | const cleanup = async () => { |
| 919 | if (!active) { |
no test coverage detected