recordPhase method

void recordPhase(
  1. String phaseName, {
  2. Map<String, dynamic>? parameters,
  3. String? parentPhase,
})

Records a loading phase with optional parameters

Implementation

void recordPhase(String phaseName, {Map<String, dynamic>? parameters, String? parentPhase}) {
  final now = DateTime.now();
  final duration =
      _lastPhaseTime != null ? now.difference(_lastPhaseTime!) : null;

  final phase = LoadingPhase(
    name: phaseName,
    timestamp: now,
    parameters: parameters,
    duration: duration,
    parentPhase: parentPhase,
  );

  // Special handling for LCP candidates
  if (phaseName == phaseLargestContentfulPaint && !_lcpFinalized) {
    _lastLcpCandidate = phase;
    // Check if this is marked as final
    if (parameters?['isFinal'] == true) {
      // Auto-finalize if marked as final
      _lcpFinalized = true;
      _phases[phaseName] = phase;
    }
    // Otherwise, don't add to phases yet, just track as candidate
  } else if (parentPhase != null && _phases.containsKey(parentPhase)) {
    // Add as substep to parent phase
    _phases[parentPhase]!.addSubstep(phase);
  } else {
    // Add as top-level phase
    _phases[phaseName] = phase;
  }

  _lastPhaseTime = now;

  // Dispatch the phase event
  _dispatchPhaseEvent(phase);
}