@devries-cameron20 / t3code Mirror · read-only / commits / 296eb82c31
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 25, 08:45 UTC

perf: throttle domain event processing with 100ms batch window

During active sessions, every domain event triggered a full syncSnapshot (IPC fetch + state rebuild + React re-render cascade) and sometimes a provider query invalidation. Events fire in rapid bursts during AI turns. Replace per-event processing with a throttle-first pattern: schedule a flush on the first event, absorb subsequent events within a 100ms window, then sync once. Provider query invalidation is batched via a flag. Since syncSnapshot fetches the complete snapshot, no events are lost by skipping intermediate syncs.

Chukwudi Nwobodo committed Mar 8, 2026 at 06:22 UTC 296eb82c312b3c36237b51870b9a87249f5c6d83
1 file changed +24 -2
apps/web/src/routes/__root.tsx
+24 -2
@@ -187,15 +187,34 @@ function EventRouter() {
187
188 void syncSnapshot().catch(() => undefined);
189
190 + // Batch domain events: collect over a short window then sync once.
191 + const DOMAIN_EVENT_BATCH_MS = 100;
192 + let batchTimer: ReturnType<typeof setTimeout> | null = null;
193 + let needsProviderInvalidation = false;
194 +
195 + const flushBatch = () => {
196 + batchTimer = null;
197 + if (needsProviderInvalidation) {
198 + needsProviderInvalidation = false;
199 + void queryClient.invalidateQueries({ queryKey: providerQueryKeys.all });
200 + }
201 + void syncSnapshot();
202 + };
203 +
204 const unsubDomainEvent = api.orchestration.onDomainEvent((event) => {
205 if (event.sequence <= latestSequence) {
206 return;
207 }
208 latestSequence = event.sequence;
209 if (event.type === "thread.turn-diff-completed" || event.type === "thread.reverted") {
196 - void queryClient.invalidateQueries({ queryKey: providerQueryKeys.all });
210 + needsProviderInvalidation = true;
211 + }
212 + // Throttle (not debounce): schedule a flush if none is pending.
213 + // This guarantees at most one syncSnapshot per batch window
214 + // without starving the UI during sustained event bursts.
215 + if (batchTimer === null) {
216 + batchTimer = setTimeout(flushBatch, DOMAIN_EVENT_BATCH_MS);
217 }
198 - void syncSnapshot();
218 });
219 const unsubTerminalEvent = api.terminal.onEvent((event) => {
220 const hasRunningSubprocess = terminalRunningSubprocessFromEvent(event);
@@ -280,6 +299,9 @@ function EventRouter() {
299 });
300 return () => {
301 disposed = true;
302 + if (batchTimer !== null) {
303 + clearTimeout(batchTimer);
304 + }
305 unsubDomainEvent();
306 unsubTerminalEvent();
307 unsubWelcome();