thread-open-profiler.ts1.4 KBView on GitHub
import { perfLogger, perfSessionLog } from '@/lib/performance-logger';

/**
 * Start profiling a thread open operation
 * Call this before opening a thread (keyboard or mouse)
 */
export function startThreadOpenProfiling(threadId: string, source: 'mouse' | 'keyboard') {
  perfLogger.startSession(`thread-open:${threadId}`);
  perfSessionLog(
    `[THREAD OPEN] ${source} - opening thread ${threadId}`,
    'color: #ff00ff; font-weight: bold',
  );

  // Add native Performance mark for browser DevTools
  performance.mark('thread-open-start');
}

/**
 * Set up automatic profiling end after browser paint
 * Call this inside startTransition after state updates
 */
export function setupThreadOpenProfilingEnd(threadId: string) {
  // Mark when React finishes rendering and browser finishes painting
  requestAnimationFrame(() => {
    perfLogger.mark('[THREAD OPEN] React commit done');
    performance.mark('thread-open-react-done');

    requestAnimationFrame(() => {
      perfLogger.mark('[THREAD OPEN] Browser paint done');
      performance.mark('thread-open-paint-done');

      // Create performance measures for browser DevTools
      try {
        performance.measure('Thread Open (Full)', 'thread-open-start', 'thread-open-paint-done');
      } catch {
        // Ignore if marks don't exist
      }

      // End session
      perfLogger.endSession(`thread-open:${threadId}`);
    });
  });
}