Skip to content

Commit a61bde1

Browse files
committed
consolidate webSocketAppDebugService and AppDebugServices
1 parent af9763f commit a61bde1

File tree

4 files changed

+163
-138
lines changed

4 files changed

+163
-138
lines changed

dwds/lib/src/connections/debug_connection.dart

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,11 @@ class DebugConnection {
2222
Future<void>? _closed;
2323

2424
DebugConnection(this._appDebugServices) {
25-
_appDebugServices.chromeProxyService?.remoteDebugger.onClose.first.then(
26-
(_) => close(),
27-
);
25+
// Only setup Chrome-specific close handling if we have a ChromeProxyService
26+
final proxyService = _appDebugServices.proxyService;
27+
if (proxyService is ChromeProxyService) {
28+
proxyService.remoteDebugger.onClose.first.then((_) => close());
29+
}
2830
}
2931

3032
/// The port of the host Dart VM Service.
@@ -41,7 +43,10 @@ class DebugConnection {
4143

4244
Future<void> close() =>
4345
_closed ??= () async {
44-
await _appDebugServices.chromeProxyService?.remoteDebugger.close();
46+
final proxyService = _appDebugServices.proxyService;
47+
if (proxyService is ChromeProxyService) {
48+
await proxyService.remoteDebugger.close();
49+
}
4550
await _appDebugServices.close();
4651
_onDoneCompleter.complete();
4752
}();
@@ -50,5 +55,10 @@ class DebugConnection {
5055
}
5156

5257
/// [ChromeProxyService] of a [DebugConnection] for internal use only.
53-
ChromeProxyService fetchChromeProxyService(DebugConnection debugConnection) =>
54-
debugConnection._appDebugServices.chromeProxyService;
58+
ChromeProxyService fetchChromeProxyService(DebugConnection debugConnection) {
59+
final service = debugConnection._appDebugServices.proxyService;
60+
if (service is ChromeProxyService) {
61+
return service;
62+
}
63+
throw StateError('ChromeProxyService not available in this debug connection');
64+
}

dwds/lib/src/handlers/dev_handler.dart

Lines changed: 104 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,10 @@ import 'package:dwds/src/servers/devtools.dart';
3131
import 'package:dwds/src/servers/extension_backend.dart';
3232
import 'package:dwds/src/servers/extension_debugger.dart';
3333
import 'package:dwds/src/services/app_debug_services.dart';
34+
import 'package:dwds/src/services/chrome_proxy_service.dart';
3435
import 'package:dwds/src/services/debug_service.dart';
3536
import 'package:dwds/src/services/expression_compiler.dart';
36-
import 'package:dwds/src/services/web_socket_app_debug_services.dart';
37+
import 'package:dwds/src/services/web_socket_proxy_service.dart';
3738
import 'package:dwds/src/utilities/shared.dart';
3839
import 'package:dwds/src/web_socket_dwds_vm_client.dart';
3940
import 'package:logging/logging.dart';
@@ -315,18 +316,19 @@ class DevHandler {
315316
IAppDebugServices appServices,
316317
AppConnection appConnection,
317318
) {
318-
safeUnawaited(
319-
appServices.chromeProxyService.remoteDebugger.onClose.first.whenComplete(
320-
() async {
319+
final chromeProxy = appServices.proxyService;
320+
if (chromeProxy is ChromeProxyService) {
321+
safeUnawaited(
322+
chromeProxy.remoteDebugger.onClose.first.whenComplete(() async {
321323
await appServices.close();
322324
_servicesByAppId.remove(appConnection.request.appId);
323325
_logger.info(
324326
'Stopped debug service on '
325327
'ws://${appServices.debugService.hostname}:${appServices.debugService.port}\n',
326328
);
327-
},
328-
),
329-
);
329+
}),
330+
);
331+
}
330332
}
331333

332334
void _handleConnection(SocketConnection injectedConnection) {
@@ -420,7 +422,9 @@ class DevHandler {
420422
if (message == null) return;
421423

422424
final appId = connection.request.appId;
423-
final wsService = _servicesByAppId[appId]?.webSocketProxyService;
425+
final proxyService = _servicesByAppId[appId]?.proxyService;
426+
final wsService =
427+
proxyService is WebSocketProxyService ? proxyService : null;
424428

425429
if (wsService == null) {
426430
_logger.warning(
@@ -452,22 +456,21 @@ class DevHandler {
452456
) async {
453457
if (message == null) return;
454458

455-
if (message is HotReloadResponse) {
456-
_servicesByAppId[connection.request.appId]?.chromeProxyService
457-
.completeHotReload(message);
458-
} else if (message is IsolateExit) {
459+
final appId = connection.request.appId;
460+
final proxyService = _servicesByAppId[appId]?.proxyService;
461+
final chromeService =
462+
proxyService is ChromeProxyService ? proxyService : null;
463+
464+
if (message is IsolateExit) {
459465
_handleIsolateExit(connection);
460466
} else if (message is IsolateStart) {
461467
await _handleIsolateStart(connection);
462468
} else if (message is BatchedDebugEvents) {
463-
_servicesByAppId[connection.request.appId]?.chromeProxyService
464-
.parseBatchedDebugEvents(message);
469+
chromeService?.parseBatchedDebugEvents(message);
465470
} else if (message is DebugEvent) {
466-
_servicesByAppId[connection.request.appId]?.chromeProxyService
467-
.parseDebugEvent(message);
471+
chromeService?.parseDebugEvent(message);
468472
} else if (message is RegisterEvent) {
469-
_servicesByAppId[connection.request.appId]?.chromeProxyService
470-
.parseRegisterEvent(message);
473+
chromeService?.parseRegisterEvent(message);
471474
} else {
472475
throw UnsupportedError(
473476
'Message type ${message.runtimeType} is not supported in Chrome mode',
@@ -571,13 +574,16 @@ class DevHandler {
571574
debuggerStart: debuggerStart,
572575
devToolsStart: DateTime.now(),
573576
);
574-
await _launchDevTools(
575-
appServices.chromeProxyService.remoteDebugger,
576-
_constructDevToolsUri(
577-
appServices.debugService.uri,
578-
ideQueryParam: 'Dwds',
579-
),
580-
);
577+
final chromeProxy = appServices.proxyService;
578+
if (chromeProxy is ChromeProxyService) {
579+
await _launchDevTools(
580+
chromeProxy.remoteDebugger,
581+
_constructDevToolsUri(
582+
appServices.debugService.uri,
583+
ideQueryParam: 'Dwds',
584+
),
585+
);
586+
}
581587
}
582588

583589
/// Creates a debug connection for WebSocket mode.
@@ -587,9 +593,9 @@ class DevHandler {
587593
final appDebugServices = await loadAppServices(appConnection);
588594

589595
// Initialize WebSocket proxy service
590-
final webSocketProxyService = appDebugServices.webSocketProxyService;
591-
if (webSocketProxyService != null) {
592-
await webSocketProxyService.isInitialized;
596+
final proxyService = appDebugServices.proxyService;
597+
if (proxyService is WebSocketProxyService) {
598+
await proxyService.isInitialized;
593599
_logger.fine('WebSocket proxy service initialized successfully');
594600
} else {
595601
_logger.warning('WebSocket proxy service is null');
@@ -606,9 +612,9 @@ class DevHandler {
606612

607613
// Initialize Chrome proxy service
608614
try {
609-
final chromeProxyService = appDebugServices.chromeProxyService;
610-
if (chromeProxyService != null) {
611-
await chromeProxyService.isInitialized;
615+
final proxyService = appDebugServices.proxyService;
616+
if (proxyService is ChromeProxyService) {
617+
await proxyService.isInitialized;
612618
_logger.fine('Chrome proxy service initialized successfully');
613619
} else {
614620
_logger.warning('Chrome proxy service is null');
@@ -653,24 +659,30 @@ class DevHandler {
653659
// Disconnect any old connection (eg. those in the keep-alive waiting
654660
// state when reloading the page).
655661
existingConnection?.shutDown();
656-
services.chromeProxyService.destroyIsolate();
662+
final chromeProxy = services.proxyService;
663+
if (chromeProxy is ChromeProxyService) {
664+
chromeProxy.destroyIsolate();
665+
}
657666

658667
// Reconnect to existing service.
659668
services.connectedInstanceId = message.instanceId;
660669

661-
if (services.chromeProxyService.pauseIsolatesOnStart) {
662-
// If the pause-isolates-on-start flag is set, we need to wait for
663-
// the resume event to run the app's main() method.
664-
_waitForResumeEventToRunMain(
665-
services.chromeProxyService.resumeAfterRestartEventsStream,
666-
readyToRunMainCompleter,
667-
);
668-
} else {
669-
// Otherwise, we can run the app's main() method immediately.
670-
readyToRunMainCompleter.complete();
671-
}
670+
final chromeService = services.proxyService;
671+
if (chromeService is ChromeProxyService) {
672+
if (chromeService.pauseIsolatesOnStart) {
673+
// If the pause-isolates-on-start flag is set, we need to wait for
674+
// the resume event to run the app's main() method.
675+
_waitForResumeEventToRunMain(
676+
chromeService.resumeAfterRestartEventsStream,
677+
readyToRunMainCompleter,
678+
);
679+
} else {
680+
// Otherwise, we can run the app's main() method immediately.
681+
readyToRunMainCompleter.complete();
682+
}
672683

673-
await services.chromeProxyService.createIsolate(connection);
684+
await chromeService.createIsolate(connection);
685+
}
674686
} else {
675687
// If this is the initial app connection, we can run the app's main()
676688
// method immediately.
@@ -762,11 +774,16 @@ class DevHandler {
762774

763775
_logger.finest('WebSocket service reconnected for app: ${message.appId}');
764776

765-
_setupMainExecution(
766-
services.webSocketProxyService?.pauseIsolatesOnStart == true,
767-
services.webSocketProxyService?.resumeAfterRestartEventsStream,
768-
readyToRunMainCompleter,
769-
);
777+
final wsService = services.proxyService;
778+
if (wsService is WebSocketProxyService) {
779+
_setupMainExecution(
780+
wsService.pauseIsolatesOnStart,
781+
wsService.resumeAfterRestartEventsStream,
782+
readyToRunMainCompleter,
783+
);
784+
} else {
785+
readyToRunMainCompleter.complete();
786+
}
770787

771788
await _handleIsolateStart(newConnection);
772789
}
@@ -809,7 +826,10 @@ class DevHandler {
809826
'Isolate exit handled by WebSocket proxy service for app: $appId',
810827
);
811828
} else {
812-
_servicesByAppId[appId]?.chromeProxyService.destroyIsolate();
829+
final proxyService = _servicesByAppId[appId]?.proxyService;
830+
if (proxyService is ChromeProxyService) {
831+
proxyService.destroyIsolate();
832+
}
813833
}
814834
}
815835

@@ -818,13 +838,15 @@ class DevHandler {
818838
final appId = appConnection.request.appId;
819839

820840
if (useWebSocketConnection) {
821-
await _servicesByAppId[appId]?.webSocketProxyService?.createIsolate(
822-
appConnection,
823-
);
841+
final proxyService = _servicesByAppId[appId]?.proxyService;
842+
if (proxyService is WebSocketProxyService) {
843+
await proxyService.createIsolate(appConnection);
844+
}
824845
} else {
825-
await _servicesByAppId[appId]?.chromeProxyService.createIsolate(
826-
appConnection,
827-
);
846+
final proxyService = _servicesByAppId[appId]?.proxyService;
847+
if (proxyService is ChromeProxyService) {
848+
await proxyService.createIsolate(appConnection);
849+
}
828850
}
829851
}
830852

@@ -871,15 +893,18 @@ class DevHandler {
871893
);
872894
final encodedUri = await debugService.encodedUri;
873895
_logger.info('Debug service listening on $encodedUri\n');
874-
await appDebugService.chromeProxyService.remoteDebugger.sendCommand(
875-
'Runtime.evaluate',
876-
params: {
877-
'expression':
878-
'console.log('
879-
'"This app is linked to the debug service: $encodedUri"'
880-
');',
881-
},
882-
);
896+
final chromeProxy = appDebugService.proxyService;
897+
if (chromeProxy is ChromeProxyService) {
898+
await chromeProxy.remoteDebugger.sendCommand(
899+
'Runtime.evaluate',
900+
params: {
901+
'expression':
902+
'console.log('
903+
'"This app is linked to the debug service: $encodedUri"'
904+
');',
905+
},
906+
);
907+
}
883908

884909
// Notify that DWDS has been launched and a debug connection has been made:
885910
_maybeEmitDwdsLaunchEvent();
@@ -989,18 +1014,20 @@ class DevHandler {
9891014
extensionDebugger.sendEvent('dwds.debugUri', debugService.uri);
9901015
final encodedUri = await debugService.encodedUri;
9911016
extensionDebugger.sendEvent('dwds.encodedUri', encodedUri);
992-
safeUnawaited(
993-
appServices.chromeProxyService.remoteDebugger.onClose.first
994-
.whenComplete(() async {
995-
appServices?.chromeProxyService.destroyIsolate();
996-
await appServices?.close();
997-
_servicesByAppId.remove(devToolsRequest.appId);
998-
_logger.info(
999-
'Stopped debug service on '
1000-
'${await appServices?.debugService.encodedUri}\n',
1001-
);
1002-
}),
1003-
);
1017+
final chromeProxy = appServices.proxyService;
1018+
if (chromeProxy is ChromeProxyService) {
1019+
safeUnawaited(
1020+
chromeProxy.remoteDebugger.onClose.first.whenComplete(() async {
1021+
chromeProxy.destroyIsolate();
1022+
await appServices?.close();
1023+
_servicesByAppId.remove(devToolsRequest.appId);
1024+
_logger.info(
1025+
'Stopped debug service on '
1026+
'${await appServices?.debugService.encodedUri}\n',
1027+
);
1028+
}),
1029+
);
1030+
}
10041031
extensionDebugConnections.add(DebugConnection(appServices));
10051032
_servicesByAppId[appId] = appServices;
10061033
}

0 commit comments

Comments
 (0)