Skip to content

Commit ba5e3ec

Browse files
authored
DebugSession listens to events instead of just sending events (#1804)
1 parent 91b8a19 commit ba5e3ec

File tree

1 file changed

+19
-8
lines changed

1 file changed

+19
-8
lines changed

dwds/debug_extension_mv3/web/debug_session.dart

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -133,10 +133,10 @@ Future<bool> _connectToDwds({
133133
final client = uri.isScheme('ws') || uri.isScheme('wss')
134134
? WebSocketClient(WebSocketChannel.connect(uri))
135135
: SseSocketClient(SseClient(uri.toString()));
136-
final debugSession = _DebugSession(client: client, appTabId: dartAppTabId);
137-
_debugSessions.add(debugSession);
138-
client.stream.listen(
139-
(data) => _routeDwdsEvent(data, client, dartAppTabId),
136+
final debugSession = _DebugSession(
137+
client: client,
138+
appTabId: dartAppTabId,
139+
onIncoming: (data) => _routeDwdsEvent(data, client, dartAppTabId),
140140
onDone: () {
141141
debugLog('Shutting down DWDS communication channel.');
142142
_detachDebuggerForTab(dartAppTabId, type: TabType.dartApp);
@@ -147,15 +147,15 @@ Future<bool> _connectToDwds({
147147
},
148148
cancelOnError: true,
149149
);
150+
_debugSessions.add(debugSession);
150151
final tabUrl = await _getTabUrl(dartAppTabId);
151152
// Send a DevtoolsRequest to the event stream:
152-
final event = jsonEncode(serializers.serialize(DevToolsRequest((b) => b
153+
debugSession.sendEvent(DevToolsRequest((b) => b
153154
..appId = debugInfo.appId
154155
..instanceId = debugInfo.appInstanceId
155156
..contextId = dartAppContextId
156157
..tabUrl = tabUrl
157-
..uriOnly = true)));
158-
client.sink.add(event);
158+
..uriOnly = true));
159159
return true;
160160
}
161161

@@ -323,12 +323,23 @@ class _DebugSession {
323323
_DebugSession({
324324
required client,
325325
required this.appTabId,
326+
required void Function(String data) onIncoming,
327+
required void Function() onDone,
328+
required void Function(dynamic error) onError,
329+
required bool cancelOnError,
326330
}) : _socketClient = client {
327331
// Collect extension events and send them periodically to the server.
328332
_batchSubscription = _batchController.stream.listen((events) {
329333
_socketClient.sink.add(jsonEncode(serializers.serialize(BatchedEvents(
330334
(b) => b.events = ListBuilder<ExtensionEvent>(events)))));
331335
});
336+
// Listen for incoming events:
337+
_socketClient.stream.listen(
338+
onIncoming,
339+
onDone: onDone,
340+
onError: onError,
341+
cancelOnError: cancelOnError,
342+
);
332343
}
333344

334345
set socketClient(SocketClient client) {
@@ -341,7 +352,7 @@ class _DebugSession {
341352
});
342353
}
343354

344-
void sendEvent(ExtensionEvent event) {
355+
void sendEvent<T>(T event) {
345356
_socketClient.sink.add(jsonEncode(serializers.serialize(event)));
346357
}
347358

0 commit comments

Comments
 (0)