finalizeBridges method

void finalizeBridges()

Runs every extension callback registered via registerExtensions in registration order, then marks the runner as finalized.

execute*/executeBundle* invoke this implicitly on first run if it has not been called already, so embedders don't have to remember to call it. Calling it explicitly first is supported and preferred when an embedder needs deterministic timing (e.g. a constructor of a Flutter helper that touches bridges before the first script runs).

Idempotent per runner: repeat calls on the same runner return without re-running any callback — the contract is "run once, then frozen". After this call returns, registerExtensions throws StateError.

Step 11 — once per package per process. Extension callbacks live on the process-global pooled bundles (_packagePool). A callback is fired only the first time any runner finalizes the package, guarded by _PackageBridgeBundle.extensionFired. A second runner that was granted the same (already-pooled) package — e.g. via the canonical if (providePackage(name) == false) { register…; registerExtensions… } idiom, where the second instance skips both — never re-fires it. Because the callback's effects (relaxers, interface proxies, generic constructors) land in static D4 / BridgedClass registries shared across instances, firing once is correct and sufficient.

Implementation

void finalizeBridges() {
  if (_bridgesFinalized) return;
  _bridgesFinalized = true;
  // Callbacks live on the pooled bundles; fire this instance's registered
  // packages in registration order, but only those not yet fired anywhere
  // in the process (step 11 once-per-package-per-process guard).
  for (final packageName in _extensionPackages) {
    final bundle = _packagePool[packageName];
    final callback = bundle?.extensionCallback;
    if (callback == null) continue;
    if (bundle!.extensionFired) {
      Logger.debugLazy(
        () => '[D4rtRunner.finalizeBridges] Extensions for "$packageName" '
            'already fired in this process — skipping.',
      );
      continue;
    }
    bundle.extensionFired = true;
    Logger.debug(
      '[D4rtRunner.finalizeBridges] Running extensions for "$packageName"',
    );
    callback();
  }
}