|
| 1 | +import { Controller } from "../index" |
| 2 | +import { EmptyRequest, String } from "@shared/proto/common" |
| 3 | +import { StreamingResponseHandler, getRequestRegistry } from "../grpc-handler" |
| 4 | +import { getTheme } from "@integrations/theme/getTheme" |
| 5 | + |
| 6 | +// Keep track of active theme subscriptions |
| 7 | +const activeThemeSubscriptions = new Set<StreamingResponseHandler>() |
| 8 | + |
| 9 | +/** |
| 10 | + * Subscribe to theme change events |
| 11 | + * @param controller The controller instance |
| 12 | + * @param request The empty request |
| 13 | + * @param responseStream The streaming response handler |
| 14 | + * @param requestId The ID of the request (passed by the gRPC handler) |
| 15 | + */ |
| 16 | +export async function subscribeToTheme( |
| 17 | + controller: Controller, |
| 18 | + request: EmptyRequest, |
| 19 | + responseStream: StreamingResponseHandler, |
| 20 | + requestId?: string, |
| 21 | +): Promise<void> { |
| 22 | + // Add this subscription to the active subscriptions |
| 23 | + activeThemeSubscriptions.add(responseStream) |
| 24 | + |
| 25 | + // Register cleanup when the connection is closed |
| 26 | + const cleanup = () => { |
| 27 | + activeThemeSubscriptions.delete(responseStream) |
| 28 | + } |
| 29 | + |
| 30 | + // Register the cleanup function with the request registry if we have a requestId |
| 31 | + if (requestId) { |
| 32 | + getRequestRegistry().registerRequest(requestId, cleanup, { type: "theme_subscription" }, responseStream) |
| 33 | + } |
| 34 | + |
| 35 | + // Send the current theme immediately upon subscription |
| 36 | + const theme = await getTheme() |
| 37 | + if (theme) { |
| 38 | + try { |
| 39 | + const themeEvent = String.create({ |
| 40 | + value: JSON.stringify(theme), |
| 41 | + }) |
| 42 | + await responseStream( |
| 43 | + themeEvent, |
| 44 | + false, // Not the last message |
| 45 | + ) |
| 46 | + } catch (error) { |
| 47 | + console.error("Error sending initial theme:", error) |
| 48 | + activeThemeSubscriptions.delete(responseStream) |
| 49 | + } |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +/** |
| 54 | + * Send a theme event to all active subscribers |
| 55 | + * @param themeJson The JSON-stringified theme data |
| 56 | + */ |
| 57 | +export async function sendThemeEvent(themeJson: string): Promise<void> { |
| 58 | + // Send the event to all active subscribers |
| 59 | + const promises = Array.from(activeThemeSubscriptions).map(async (responseStream) => { |
| 60 | + try { |
| 61 | + const event = String.create({ |
| 62 | + value: themeJson, |
| 63 | + }) |
| 64 | + await responseStream( |
| 65 | + event, |
| 66 | + false, // Not the last message |
| 67 | + ) |
| 68 | + } catch (error) { |
| 69 | + console.error("Error sending theme event:", error) |
| 70 | + // Remove the subscription if there was an error |
| 71 | + activeThemeSubscriptions.delete(responseStream) |
| 72 | + } |
| 73 | + }) |
| 74 | + |
| 75 | + await Promise.all(promises) |
| 76 | +} |
0 commit comments