initializePerformanceTracking method

void initializePerformanceTracking(
  1. DateTime startTime
)

Initializes performance tracking by recording the navigation start time. Called when the viewport is first laid out.

Implementation

void initializePerformanceTracking(DateTime startTime) {
  // If no route is attached yet, initialize metrics for the initial route
  if (_buildContextStack.isEmpty) {
    final defaultRoute = initialRoute ?? '/';
    if (!_routeMetrics.containsKey(defaultRoute)) {
      _routeMetrics[defaultRoute] = RoutePerformanceMetrics(defaultRoute);
    }
  }

  if (_currentRouteMetrics == null || _currentRouteMetrics!.lcpReported) return;

  final metrics = _currentRouteMetrics!;
  metrics.navigationStartTime = startTime;
  metrics.lcpReported = false;
  metrics.largestContentfulPaintSize = 0;
  metrics.lastReportedLCPTime = 0;
  metrics.currentLCPElement = null;

  metrics.fpReported = false;
  metrics.fpTime = 0;

  // Reset FCP tracking as well
  metrics.fcpReported = false;
  metrics.fcpTime = 0;

  // Capture the initial evaluated state when performance tracking starts
  metrics.initialEvaluatedState = evaluated;

  // Cancel any existing timer
  metrics.lcpAutoFinalizeTimer?.cancel();

  // Set up auto-finalization timer with a 5 second delay
  // This ensures we wait long enough for all LCP candidates to be reported
  metrics.lcpAutoFinalizeTimer = Timer(Duration(seconds: 10), () {
    // Auto-finalize LCP if no user interaction has occurred
    if (!metrics.lcpReported) {
      finalizeLCP();
    }
  });
}