Skip to content

Commit be31caa

Browse files
committed
consolidate handleChromeMessages and handleWebSocketMessages created default createIsolate and destroyIsolate in ProxyService
1 parent 5340ab0 commit be31caa

File tree

4 files changed

+69
-55
lines changed

4 files changed

+69
-55
lines changed

dwds/lib/src/handlers/dev_handler.dart

Lines changed: 22 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ class DevHandler {
141141
/// Sends the provided [request] to all connected injected clients.
142142
/// Returns the number of clients the request was successfully sent to.
143143
int _sendRequestToClients(Object request) {
144+
_logger.finest('Sending request to injected clients: $request');
144145
var successfulSends = 0;
145146
for (final injectedConnection in _injectedConnections) {
146147
try {
@@ -358,12 +359,9 @@ class DevHandler {
358359
}
359360
if (message is DevToolsRequest) {
360361
await _handleDebugRequest(connection, injectedConnection);
361-
} else if (useWebSocketConnection) {
362-
// Handle WebSocket-specific messages
363-
await _handleWebSocketMessage(connection, message);
364362
} else {
365-
// Handle Chrome-specific messages
366-
await _handleChromeMessage(connection, message);
363+
// Handle messages for both WebSocket and Chrome proxy services
364+
await _handleMessage(connection, message);
367365
}
368366
}
369367
} catch (e, s) {
@@ -413,66 +411,40 @@ class DevHandler {
413411
);
414412
}
415413

416-
/// Handles WebSocket-specific messages.
417-
Future<void> _handleWebSocketMessage(
418-
AppConnection connection,
419-
Object? message,
420-
) async {
414+
/// Handles messages for both WebSocket and Chrome proxy services.
415+
Future<void> _handleMessage(AppConnection connection, Object? message) async {
421416
if (message == null) return;
422417

423418
final appId = connection.request.appId;
424419
final proxyService = _servicesByAppId[appId]?.proxyService;
425-
final wsService =
426-
proxyService is WebSocketProxyService ? proxyService : null;
427420

428-
if (wsService == null) {
421+
if (proxyService == null) {
429422
_logger.warning(
430-
'No WebSocketProxyService found for appId: $appId to process $message',
423+
'No proxy service found for appId: $appId to process $message',
431424
);
432425
return;
433426
}
427+
428+
// Handle messages that are specific to certain proxy service types
434429
if (message is HotReloadResponse) {
435-
wsService.completeHotReload(message);
430+
proxyService.completeHotReload(message);
436431
} else if (message is ServiceExtensionResponse) {
437-
wsService.completeServiceExtension(message);
438-
} else if (message is RegisterEvent) {
439-
wsService.parseRegisterEvent(message);
440-
} else if (message is BatchedDebugEvents) {
441-
wsService.parseBatchedDebugEvents(message);
442-
} else if (message is DebugEvent) {
443-
wsService.parseDebugEvent(message);
444-
} else {
445-
throw UnsupportedError(
446-
'Message type ${message.runtimeType} is not supported in WebSocket mode',
447-
);
448-
}
449-
}
450-
451-
/// Handles Chrome-specific messages.
452-
Future<void> _handleChromeMessage(
453-
AppConnection connection,
454-
Object? message,
455-
) async {
456-
if (message == null) return;
457-
458-
final appId = connection.request.appId;
459-
final proxyService = _servicesByAppId[appId]?.proxyService;
460-
final chromeService =
461-
proxyService is ChromeProxyService ? proxyService : null;
462-
463-
if (message is IsolateExit) {
432+
proxyService.completeServiceExtension(message);
433+
} else if (message is IsolateExit) {
464434
_handleIsolateExit(connection);
465435
} else if (message is IsolateStart) {
466436
await _handleIsolateStart(connection);
467437
} else if (message is BatchedDebugEvents) {
468-
chromeService?.parseBatchedDebugEvents(message);
438+
proxyService.parseBatchedDebugEvents(message);
469439
} else if (message is DebugEvent) {
470-
chromeService?.parseDebugEvent(message);
440+
proxyService.parseDebugEvent(message);
471441
} else if (message is RegisterEvent) {
472-
chromeService?.parseRegisterEvent(message);
442+
proxyService.parseRegisterEvent(message);
473443
} else {
444+
final serviceType =
445+
proxyService is WebSocketProxyService ? 'WebSocket' : 'Chrome';
474446
throw UnsupportedError(
475-
'Message type ${message.runtimeType} is not supported in Chrome mode',
447+
'Message type ${message.runtimeType} is not supported in $serviceType mode',
476448
);
477449
}
478450
}
@@ -649,12 +621,12 @@ class DevHandler {
649621
// AppConnection is in the KeepAlive state (this means it disconnected but
650622
// is still waiting for a possible reconnect - this happens during a page
651623
// reload).
652-
final canReuseConnection =
624+
final canReconnect =
653625
services != null &&
654626
(services.connectedInstanceId == null ||
655627
existingConnection?.isInKeepAlivePeriod == true);
656628

657-
if (canReuseConnection) {
629+
if (canReconnect) {
658630
// Disconnect any old connection (eg. those in the keep-alive waiting
659631
// state when reloading the page).
660632
existingConnection?.shutDown();
@@ -719,13 +691,13 @@ class DevHandler {
719691
final hasNoActiveConnection = services?.connectedInstanceId == null;
720692
final noExistingConnection = existingConnection == null;
721693

722-
final canReuseConnection =
694+
final canReconnect =
723695
services != null &&
724696
(isSameInstance ||
725697
(isKeepAliveReconnect && hasNoActiveConnection) ||
726698
(noExistingConnection && hasNoActiveConnection));
727699

728-
if (canReuseConnection) {
700+
if (canReconnect) {
729701
// Reconnect to existing service.
730702
await _reconnectToService(
731703
services,
@@ -739,15 +711,13 @@ class DevHandler {
739711
readyToRunMainCompleter.complete();
740712

741713
// For WebSocket mode, we need to proactively create and emit a debug connection
742-
// since Flutter tools won't call debugConnection() for WebServerDevice
743714
try {
744715
// Initialize the WebSocket service and create debug connection
745716
final debugConnection = await createDebugConnectionForWebSocket(
746717
connection,
747718
);
748719

749720
// Emit the debug connection through the extension stream
750-
// This should trigger Flutter tools to pick it up as if it was an extension connection
751721
extensionDebugConnections.add(debugConnection);
752722
} catch (e, s) {
753723
_logger.warning('Failed to create WebSocket debug connection: $e\n$s');

dwds/lib/src/services/chrome_proxy_service.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,7 @@ class ChromeProxyService extends ProxyService {
262262
///
263263
/// If [newConnection] is true, this method does not recompute metadata
264264
/// information as the metadata couldn't have changed.
265+
@override
265266
Future<void> createIsolate(
266267
AppConnection appConnection, {
267268
bool newConnection = false,
@@ -381,6 +382,7 @@ class ChromeProxyService extends ProxyService {
381382
/// Should be called when there is a hot restart or full page refresh.
382383
///
383384
/// Clears out the [_inspector] and all related cached information.
385+
@override
384386
void destroyIsolate() {
385387
_logger.fine('Destroying isolate');
386388
if (!_isIsolateRunning) return;

dwds/lib/src/services/proxy_service.dart

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@
55
import 'dart:async';
66

77
import 'package:dwds/data/debug_event.dart';
8+
import 'package:dwds/data/hot_reload_response.dart';
89
import 'package:dwds/data/register_event.dart';
10+
import 'package:dwds/data/service_extension_response.dart';
11+
import 'package:dwds/src/connections/app_connection.dart';
912
import 'package:dwds/src/events.dart';
1013
import 'package:dwds/src/utilities/shared.dart';
1114
import 'package:pub_semver/pub_semver.dart' as semver;
@@ -182,6 +185,22 @@ abstract class ProxyService implements VmServiceInterface {
182185
/// protocol [Event].
183186
void parseRegisterEvent(RegisterEvent registerEvent);
184187

188+
/// Completes hot reload with response from client.
189+
///
190+
/// Default implementation throws UnimplementedError.
191+
/// Override in subclasses that support hot reload completion.
192+
void completeHotReload(HotReloadResponse response) {
193+
throw UnimplementedError('completeHotReload not supported');
194+
}
195+
196+
/// Completes service extension with response from client.
197+
///
198+
/// Default implementation throws UnimplementedError.
199+
/// Override in subclasses that support service extension completion.
200+
void completeServiceExtension(ServiceExtensionResponse response) {
201+
throw UnimplementedError('completeServiceExtension not supported');
202+
}
203+
185204
/// Standard RPC error for unsupported methods.
186205
static vm_service.RPCError _rpcNotSupported(String method) {
187206
return vm_service.RPCError(
@@ -347,6 +366,21 @@ abstract class ProxyService implements VmServiceInterface {
347366
return _rpcNotSupportedFuture('clearCpuSamples');
348367
}
349368

369+
/// Creates a new isolate for debugging.
370+
///
371+
/// Implementations should handle isolate lifecycle management according to
372+
/// their specific debugging mode (Chrome vs WebSocket).
373+
Future<void> createIsolate(
374+
AppConnection appConnection, {
375+
bool newConnection = false,
376+
});
377+
378+
/// Destroys the isolate and cleans up debugging state.
379+
///
380+
/// Implementations should handle cleanup according to their specific
381+
/// debugging mode and connection management strategy.
382+
void destroyIsolate();
383+
350384
/// Prevent DWDS from blocking Dart SDK rolls if changes in package:vm_service
351385
/// are unimplemented in DWDS.
352386
@override

dwds/lib/src/services/web_socket_proxy_service.dart

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,15 +122,20 @@ class WebSocketProxyService extends ProxyService {
122122
bool _mainHasStarted = false;
123123

124124
/// Creates a new isolate for WebSocket debugging.
125-
Future<void> createIsolate([AppConnection? appConnectionOverride]) async {
126-
// Update app connection if override provided
127-
appConnection = appConnectionOverride ?? appConnection;
125+
@override
126+
Future<void> createIsolate(
127+
AppConnection appConnection, {
128+
bool newConnection = false,
129+
}) async {
130+
// Update app connection
131+
this.appConnection = appConnection;
128132

129133
// Track this connection
130134
final connectionId = appConnection.request.instanceId;
131135

132136
// Check if this connection is already being tracked
133137
final isNewConnection =
138+
newConnection ||
134139
!_appConnectionDoneSubscriptions.containsKey(connectionId);
135140

136141
if (isNewConnection) {
@@ -265,6 +270,7 @@ class WebSocketProxyService extends ProxyService {
265270
}
266271

267272
/// Destroys the isolate and cleans up state.
273+
@override
268274
void destroyIsolate() {
269275
_logger.fine('Destroying isolate');
270276

@@ -452,6 +458,7 @@ class WebSocketProxyService extends ProxyService {
452458
}
453459

454460
/// Completes hot reload with response from client.
461+
@override
455462
void completeHotReload(HotReloadResponse response) {
456463
final tracker = _pendingHotReloads[response.id];
457464

@@ -630,6 +637,7 @@ class WebSocketProxyService extends ProxyService {
630637
}
631638

632639
/// Completes service extension with response.
640+
@override
633641
void completeServiceExtension(ServiceExtensionResponse response) {
634642
final id = response.id;
635643

0 commit comments

Comments
 (0)