load method

Future<WebFController?> load(
  1. WebFBundle bundle
)

Loads content from the provided WebFBundle.

Unloads any existing content, adds the bundle to history, and executes the new content. This is the main method for loading new content into WebF.

Implementation

Future<WebFController?> load(WebFBundle bundle) async {
  assert(!_view!.disposed, 'WebF have already disposed');

  String? currentPageId = WebFControllerManager.instance.getControllerName(this);

  if (currentPageId == null) return null;

  // Record load start phase
  _loadingState.recordPhase(LoadingState.phaseLoadStart, parameters: {
    'bundle': bundle.url,
    'currentPageId': currentPageId,
  });

  // Reset performance metrics for the current route
  if (_currentRouteMetrics != null) {
    final metrics = _currentRouteMetrics!;
    // Reset LCP tracking for new page load
    metrics.lcpReported = false;
    metrics.largestContentfulPaintSize = 0;
    metrics.lastReportedLCPTime = 0;
    metrics.currentLCPElement = null;
    metrics.navigationStartTime = DateTime.now();

    // Reset FCP tracking for new page load
    metrics.fcpReported = false;
    metrics.fcpTime = 0;

    // Reset FP tracking for new page load
    metrics.fpReported = false;
    metrics.fpTime = 0;

    // Capture the initial evaluated state for new page load
    metrics.initialEvaluatedState = evaluated;
  }

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

  // Set up new auto-finalization timer
  if (_currentRouteMetrics != null) {
    _currentRouteMetrics!.lcpAutoFinalizeTimer = Timer(Duration(seconds: 5), () {
      if (!_currentRouteMetrics!.lcpReported) {
        finalizeLCP();
      }
    });
  }

  return WebFControllerManager.instance
      .addOrUpdateControllerWithLoading(name: currentPageId, bundle: bundle, forceReplace: true, mode: mode);
}