* Safely write data to a client's response. If the write fails (e.g., client already disconnected), * the client is automatically removed to prevent further writes to a dead connection. * Also fans out to any registered observers of `chatId`.
(chatId: string, data: string)
| 64 | * Also fans out to any registered observers of `chatId`. |
| 65 | */ |
| 66 | private safeWrite(chatId: string, data: string): boolean { |
| 67 | const client = this.clients.get(chatId) |
| 68 | let ok = false |
| 69 | if (client) { |
| 70 | try { |
| 71 | client.response.write(data) |
| 72 | ok = true |
| 73 | } catch { |
| 74 | this.clients.delete(chatId) |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | const observerSet = this.observers.get(chatId) |
| 79 | if (observerSet && observerSet.size > 0) { |
| 80 | for (const observerId of Array.from(observerSet)) { |
| 81 | const observer = this.clients.get(observerId) |
| 82 | if (!observer) { |
| 83 | observerSet.delete(observerId) |
| 84 | continue |
| 85 | } |
| 86 | try { |
| 87 | observer.response.write(data) |
| 88 | } catch { |
| 89 | this.clients.delete(observerId) |
| 90 | observerSet.delete(observerId) |
| 91 | } |
| 92 | } |
| 93 | if (observerSet.size === 0) this.observers.delete(chatId) |
| 94 | } |
| 95 | |
| 96 | return ok |
| 97 | } |
| 98 | |
| 99 | removeClient(chatId: string) { |
| 100 | const client = this.clients.get(chatId) |
no test coverage detected