install static method

void install()

Idempotent install. Safe to call multiple times within the same isolate lifetime (matches TapirPlugin.install semantics).

Insertion order is load-bearing: form and navigation keep slots 0 and 1, controller state, form errors, gate result, middleware and auth user take slots 2 to 6, controller flags and route params 7 and 8, Echo connection and all gate results 9 and 10, and the three crumbs-bridge enrichers (recentHttp, recentLogs, recentExceptions) 11, 12 and 13, so the order of the lines in a snapshot stays deterministic across versions. After enricher registration, one navigate adapter is wired via TapirPlugin.registerNavigateAdapter so ext.tapir.navigate --route drives MagicRouter.instance.to (path-based) instead of falling back to the SystemNavigator broadcast.

Implementation

static void install() {
  if (_installed) return;
  _installed = true;

  // 1. Form and navigation (slots 0 and 1).
  TapirPlugin.enrichers.add(magicFormEnricher);
  TapirPlugin.enrichers.add(magicNavigationEnricher);

  // 2. Controller, form errors, gate, middleware, auth user (slots 2..6).
  TapirPlugin.enrichers.add(magicControllerEnricher);
  TapirPlugin.enrichers.add(magicFormErrorsEnricher);
  TapirPlugin.enrichers.add(magicGateResultEnricher);
  TapirPlugin.enrichers.add(magicMiddlewareEnricher);
  TapirPlugin.enrichers.add(magicAuthUserEnricher);

  // 3. Controller flags and route params (slots 7..8).
  TapirPlugin.enrichers.add(magicControllerFlagsEnricher);
  TapirPlugin.enrichers.add(magicRouteParamsEnricher);

  // 4. Echo connection and all gate results (slots 9..10).
  TapirPlugin.enrichers.add(magicEchoConnectionEnricher);
  TapirPlugin.enrichers.add(magicGateResultsAllEnricher);

  // 5. The crumbs-bridge enrichers (slots 11..13). Each enricher
  //    wraps its [CrumbsStore] read in try/catch so a missing-crumbs
  //    classpath collapses to a graceful null without bubbling up.
  TapirPlugin.enrichers.add(magicRecentHttpEnricher);
  TapirPlugin.enrichers.add(magicRecentLogsEnricher);
  TapirPlugin.enrichers.add(magicRecentExceptionsEnricher);

  // 6. Subscribe to the Echo connection-state stream when broadcasting is
  //    registered; keep the last emitted value in the module-private cache
  //    so [magicEchoConnectionEnricher] can read it synchronously.
  if (Magic.bound('broadcasting')) {
    _echoSubscription = Echo.manager.connection().connectionState.listen(
      (state) => _lastEchoState = _connectionStateName(state),
    );
  }

  // 7. Register the navigate adapter so `ext.tapir.navigate --route <path>`
  //    drives GoRouter through MagicRouter instead of falling back to the
  //    SystemNavigator broadcast (which GoRouter does not listen to).
  //
  //    The adapter uses MagicRouter.instance.to(route) (path-based navigation,
  //    NOT .toNamed) because the tapir `--route` argument is a URL path. A
  //    null router instance (not yet initialised) throws StateError; we catch
  //    it and return false so tapir can fall back to the platform broadcast.
  TapirPlugin.registerNavigateAdapter((String route) async {
    try {
      MagicRouter.instance.to(route);
      return true;
    } on StateError {
      return false;
    }
  });
}