MCPcopy Create free account
hub / github.com/colbymchenry/codegraph / EventHub

Class EventHub

src/ui-server/api/events.ts:160–480  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

158 * only while somebody is listening.
159 */
160export class EventHub {
161 private readonly projectRoot: string;
162 private readonly session: GraphSession;
163 private readonly clients = new Set<Client>();
164
165 private sourceWatcher: FileWatcher | null = null;
166 private indexWatcher: fs.FSWatcher | null = null;
167 private indexTimer: ReturnType<typeof setTimeout> | null = null;
168 /** When the current settle window started, for the {@link INDEX_SETTLE_MAX_MS} cap. */
169 private indexPendingSince = 0;
170 private revision: WireIndexRevision | null = null;
171 private sourceUp = false;
172 private indexUp = false;
173 private degraded: string | null = null;
174 private closed = false;
175
176 constructor(projectRoot: string, session: GraphSession) {
177 this.projectRoot = projectRoot;
178 this.session = session;
179 }
180
181 /**
182 * Attach one browser to the stream.
183 *
184 * Returns `true` in every case — the response is answered here, streaming or
185 * not — so it slots into the API's `switch` like any other endpoint.
186 */
187 subscribe(req: IncomingMessage, res: ServerResponse, method: string): true {
188 if (this.closed) {
189 // The server is shutting down. Answer, do not attach: a client that got a
190 // stream here would hold the socket open against `close()`.
191 res.writeHead(503, {
192 'Content-Type': 'application/json; charset=utf-8',
193 'Cache-Control': 'no-store',
194 });
195 res.end(method === 'HEAD' ? undefined : JSON.stringify({ error: 'Shutting down.', code: 'internal' }));
196 return true;
197 }
198
199 res.writeHead(200, {
200 'Content-Type': 'text/event-stream; charset=utf-8',
201 'Cache-Control': 'no-store',
202 // Node would otherwise chunk small writes; an event that sits in a buffer
203 // is an event that did not happen.
204 Connection: 'keep-alive',
205 'X-Accel-Buffering': 'no',
206 });
207
208 if (method === 'HEAD') {
209 res.end();
210 return true;
211 }
212
213 // No keep-alive timeout on this socket: the server sets one globally so
214 // Ctrl-C does not wait on browser connections, and it would close a healthy
215 // stream between heartbeats.
216 res.socket?.setTimeout(0);
217 res.socket?.setNoDelay(true);

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected