start static method

Future<Dwds> start({
  1. required AssetReader assetReader,
  2. required Stream<BuildResult> buildResults,
  3. required ConnectionProvider chromeConnection,
  4. required ToolConfiguration toolConfiguration,
})

Implementation

static Future<Dwds> start({
  required AssetReader assetReader,
  required Stream<BuildResult> buildResults,
  required ConnectionProvider chromeConnection,
  required ToolConfiguration toolConfiguration,
}) async {
  globalToolConfiguration = toolConfiguration;
  final debugSettings = toolConfiguration.debugSettings;
  final appMetadata = toolConfiguration.appMetadata;
  DevTools? devTools;
  Future<String>? extensionUri;
  ExtensionBackend? extensionBackend;
  if (debugSettings.enableDebugExtension) {
    final handler = debugSettings.useSseForDebugBackend
        ? SseSocketHandler(
            SseHandler(
              Uri.parse('/\$debug'),
              // Proxy servers may actively kill long standing connections.
              // Allow for clients to reconnect in a short window. Making the
              // window too long may cause issues if the user closes a debug
              // session and initiates a new one during the keepAlive window.
              keepAlive: const Duration(seconds: 5),
            ),
          )
        : WebSocketSocketHandler();

    extensionBackend =
        await ExtensionBackend.start(handler, appMetadata.hostname);
    extensionUri = Future.value(
      Uri(
        scheme: debugSettings.useSseForDebugBackend ? 'http' : 'ws',
        host: extensionBackend.hostname,
        port: extensionBackend.port,
        path: r'$debug',
      ).toString(),
    );
    final urlEncoder = debugSettings.urlEncoder;
    if (urlEncoder != null) {
      extensionUri = urlEncoder(await extensionUri);
    }
  }

  final devToolsLauncher = debugSettings.devToolsLauncher;
  if (devToolsLauncher != null) {
    devTools = await devToolsLauncher(appMetadata.hostname);
    final uri =
        Uri(scheme: 'http', host: devTools.hostname, port: devTools.port);
    _logger.info('Serving DevTools at $uri\n');
  }

  final injected = DwdsInjector(
    extensionUri: extensionUri,
  );

  final devHandler = DevHandler(
    chromeConnection,
    buildResults,
    devTools,
    assetReader,
    appMetadata.hostname,
    extensionBackend,
    debugSettings.urlEncoder,
    debugSettings.useSseForDebugProxy,
    debugSettings.useSseForInjectedClient,
    debugSettings.expressionCompiler,
    injected,
    debugSettings.spawnDds,
    debugSettings.launchDevToolsInNewWindow,
  );

  return Dwds._(
    injected.middleware,
    devTools,
    devHandler,
    assetReader,
    debugSettings.enableDebugging,
  );
}