From a6d837f953caca25fb4220a94ee2fc7bed248fc1 Mon Sep 17 00:00:00 2001 From: Darius Date: Thu, 5 Feb 2026 22:05:35 +0100 Subject: [PATCH] make clients of sse service observable --- dist/homepage.d.ts | 4 ++-- dist/homepage.d.ts.map | 2 +- dist/sse.d.ts | 9 +++++++++ dist/sse.d.ts.map | 2 +- dist/sse.js | 22 ++++++++++++++++++++++ src/homepage.ts | 6 +++--- src/sse.ts | 42 ++++++++++++++++++++++++++++++++++++++---- 7 files changed, 76 insertions(+), 11 deletions(-) diff --git a/dist/homepage.d.ts b/dist/homepage.d.ts index cc8f4d6..cd50940 100644 --- a/dist/homepage.d.ts +++ b/dist/homepage.d.ts @@ -1,5 +1,5 @@ -import { API_HA_DeskPosition } from "./homeassistant"; -import { TidalGetCurrent } from "./tidal"; +import { type API_HA_DeskPosition } from "./homeassistant"; +import { type TidalGetCurrent } from "./tidal"; export type FullInformation = { ha_desk_position: API_HA_DeskPosition | null; ha_temp: string | null; diff --git a/dist/homepage.d.ts.map b/dist/homepage.d.ts.map index be0b16e..072180e 100644 --- a/dist/homepage.d.ts.map +++ b/dist/homepage.d.ts.map @@ -1 +1 @@ -{"version":3,"file":"homepage.d.ts","sourceRoot":"","sources":["../src/homepage.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,mBAAmB,EAAE,MAAM,iBAAiB,CAAC;AACtD,OAAO,EAAE,eAAe,EAAE,MAAM,SAAS,CAAC;AAE1C,MAAM,MAAM,eAAe,GAAG;IAC7B,gBAAgB,EAAE,mBAAmB,GAAG,IAAI,CAAC;IAC7C,OAAO,EAAE,MAAM,GAAG,IAAI,CAAC;IACvB,aAAa,EAAE,eAAe,GAAG,IAAI,CAAC;CACtC,CAAA"} \ No newline at end of file +{"version":3,"file":"homepage.d.ts","sourceRoot":"","sources":["../src/homepage.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,mBAAmB,EAAE,MAAM,iBAAiB,CAAC;AAC3D,OAAO,EAAE,KAAK,eAAe,EAAE,MAAM,SAAS,CAAC;AAE/C,MAAM,MAAM,eAAe,GAAG;IAC7B,gBAAgB,EAAE,mBAAmB,GAAG,IAAI,CAAC;IAC7C,OAAO,EAAE,MAAM,GAAG,IAAI,CAAC;IACvB,aAAa,EAAE,eAAe,GAAG,IAAI,CAAC;CACtC,CAAC"} \ No newline at end of file diff --git a/dist/sse.d.ts b/dist/sse.d.ts index c69f4de..63e5749 100644 --- a/dist/sse.d.ts +++ b/dist/sse.d.ts @@ -8,8 +8,17 @@ export type SseEvent = { data?: unknown; message?: string; }; +export type SseClientChangeEvent = { + type: "add" | "remove"; + clientId: string; + clientCount: number; +}; +export type SseClientChangeCallback = (event: SseClientChangeEvent) => void; export declare class SseService { private clients; + private clientChangeCallbacks; + onClientChange(callback: SseClientChangeCallback): () => void; + private emitClientChange; addClient(client: SseClient): void; removeClient(clientId: string): void; notifyClients(event: SseEvent): void; diff --git a/dist/sse.d.ts.map b/dist/sse.d.ts.map index b2c66e0..ed313e1 100644 --- a/dist/sse.d.ts.map +++ b/dist/sse.d.ts.map @@ -1 +1 @@ -{"version":3,"file":"sse.d.ts","sourceRoot":"","sources":["../src/sse.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,QAAQ,CAAC;AAG9B,MAAM,MAAM,SAAS,GAAG;IACvB,EAAE,EAAE,IAAI,CAAC;IACT,IAAI,EAAE,CAAC,IAAI,EAAE,QAAQ,KAAK,IAAI,CAAC;CAC/B,CAAA;AAED,MAAM,MAAM,QAAQ,GAAG;IACtB,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,CAAC,EAAE,OAAO,CAAC;IACf,OAAO,CAAC,EAAE,MAAM,CAAC;CACjB,CAAA;AAED,qBAAa,UAAU;IACtB,OAAO,CAAC,OAAO,CAAqC;IAEpD,SAAS,CAAC,MAAM,EAAE,SAAS,GAAG,IAAI;IAOlC,YAAY,CAAC,QAAQ,EAAE,MAAM,GAAG,IAAI;IAOpC,aAAa,CAAC,KAAK,EAAE,QAAQ,GAAG,IAAI;IAMpC,cAAc,IAAI,MAAM;CAGxB"} \ No newline at end of file +{"version":3,"file":"sse.d.ts","sourceRoot":"","sources":["../src/sse.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,QAAQ,CAAC;AAG9B,MAAM,MAAM,SAAS,GAAG;IACvB,EAAE,EAAE,IAAI,CAAC;IACT,IAAI,EAAE,CAAC,IAAI,EAAE,QAAQ,KAAK,IAAI,CAAC;CAC/B,CAAC;AAEF,MAAM,MAAM,QAAQ,GAAG;IACtB,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,CAAC,EAAE,OAAO,CAAC;IACf,OAAO,CAAC,EAAE,MAAM,CAAC;CACjB,CAAC;AAEF,MAAM,MAAM,oBAAoB,GAAG;IAClC,IAAI,EAAE,KAAK,GAAG,QAAQ,CAAC;IACvB,QAAQ,EAAE,MAAM,CAAC;IACjB,WAAW,EAAE,MAAM,CAAC;CACpB,CAAC;AAEF,MAAM,MAAM,uBAAuB,GAAG,CAAC,KAAK,EAAE,oBAAoB,KAAK,IAAI,CAAC;AAE5E,qBAAa,UAAU;IACtB,OAAO,CAAC,OAAO,CAAqC;IACpD,OAAO,CAAC,qBAAqB,CAAiC;IAE9D,cAAc,CAAC,QAAQ,EAAE,uBAAuB,GAAG,MAAM,IAAI;IAS7D,OAAO,CAAC,gBAAgB;IAMxB,SAAS,CAAC,MAAM,EAAE,SAAS,GAAG,IAAI;IAYlC,YAAY,CAAC,QAAQ,EAAE,MAAM,GAAG,IAAI;IAYpC,aAAa,CAAC,KAAK,EAAE,QAAQ,GAAG,IAAI;IAMpC,cAAc,IAAI,MAAM;CAGxB"} \ No newline at end of file diff --git a/dist/sse.js b/dist/sse.js index 2cffd06..7ce15ec 100644 --- a/dist/sse.js +++ b/dist/sse.js @@ -1,13 +1,35 @@ import { logInfo } from "./logger.js"; export class SseService { clients = new Map(); + clientChangeCallbacks = []; + onClientChange(callback) { + this.clientChangeCallbacks.push(callback); + return () => { + this.clientChangeCallbacks = this.clientChangeCallbacks.filter((cb) => cb !== callback); + }; + } + emitClientChange(event) { + for (const callback of this.clientChangeCallbacks) { + callback(event); + } + } addClient(client) { this.clients.set(client.id, client); logInfo(`SSE client connected: ${client.id}. Total clients: ${this.clients.size}`); + this.emitClientChange({ + type: "add", + clientId: client.id, + clientCount: this.clients.size, + }); } removeClient(clientId) { this.clients.delete(clientId); logInfo(`SSE client disconnected: ${clientId}. Total clients: ${this.clients.size}`); + this.emitClientChange({ + type: "remove", + clientId, + clientCount: this.clients.size, + }); } notifyClients(event) { for (const client of this.clients.values()) { diff --git a/src/homepage.ts b/src/homepage.ts index 3ccb7b2..de5b778 100644 --- a/src/homepage.ts +++ b/src/homepage.ts @@ -1,8 +1,8 @@ -import { API_HA_DeskPosition } from "./homeassistant"; -import { TidalGetCurrent } from "./tidal"; +import { type API_HA_DeskPosition } from "./homeassistant"; +import { type TidalGetCurrent } from "./tidal"; export type FullInformation = { ha_desk_position: API_HA_DeskPosition | null; ha_temp: string | null; tidal_current: TidalGetCurrent | null; -} +}; diff --git a/src/sse.ts b/src/sse.ts index c0bd92f..da2dfec 100644 --- a/src/sse.ts +++ b/src/sse.ts @@ -4,22 +4,51 @@ import { logInfo } from "./logger.js"; export type SseClient = { id: UUID; send: (data: SseEvent) => void; -} +}; export type SseEvent = { type: string; data?: unknown; message?: string; -} +}; + +export type SseClientChangeEvent = { + type: "add" | "remove"; + clientId: string; + clientCount: number; +}; + +export type SseClientChangeCallback = (event: SseClientChangeEvent) => void; export class SseService { private clients: Map = new Map(); + private clientChangeCallbacks: SseClientChangeCallback[] = []; + + onClientChange(callback: SseClientChangeCallback): () => void { + this.clientChangeCallbacks.push(callback); + return () => { + this.clientChangeCallbacks = this.clientChangeCallbacks.filter( + (cb) => cb !== callback, + ); + }; + } + + private emitClientChange(event: SseClientChangeEvent): void { + for (const callback of this.clientChangeCallbacks) { + callback(event); + } + } addClient(client: SseClient): void { this.clients.set(client.id, client); logInfo( `SSE client connected: ${client.id}. Total clients: ${this.clients.size}`, ); + this.emitClientChange({ + type: "add", + clientId: client.id, + clientCount: this.clients.size, + }); } removeClient(clientId: string): void { @@ -27,12 +56,17 @@ export class SseService { logInfo( `SSE client disconnected: ${clientId}. Total clients: ${this.clients.size}`, ); + this.emitClientChange({ + type: "remove", + clientId, + clientCount: this.clients.size, + }); } notifyClients(event: SseEvent): void { for (const client of this.clients.values()) { - client.send(event); - } + client.send(event); + } } getClientCount(): number {