@@ -133,10 +133,10 @@ Future<bool> _connectToDwds({
133
133
final client = uri.isScheme ('ws' ) || uri.isScheme ('wss' )
134
134
? WebSocketClient (WebSocketChannel .connect (uri))
135
135
: 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),
140
140
onDone: () {
141
141
debugLog ('Shutting down DWDS communication channel.' );
142
142
_detachDebuggerForTab (dartAppTabId, type: TabType .dartApp);
@@ -147,15 +147,15 @@ Future<bool> _connectToDwds({
147
147
},
148
148
cancelOnError: true ,
149
149
);
150
+ _debugSessions.add (debugSession);
150
151
final tabUrl = await _getTabUrl (dartAppTabId);
151
152
// Send a DevtoolsRequest to the event stream:
152
- final event = jsonEncode (serializers. serialize (DevToolsRequest ((b) => b
153
+ debugSession. sendEvent (DevToolsRequest ((b) => b
153
154
..appId = debugInfo.appId
154
155
..instanceId = debugInfo.appInstanceId
155
156
..contextId = dartAppContextId
156
157
..tabUrl = tabUrl
157
- ..uriOnly = true )));
158
- client.sink.add (event);
158
+ ..uriOnly = true ));
159
159
return true ;
160
160
}
161
161
@@ -323,12 +323,23 @@ class _DebugSession {
323
323
_DebugSession ({
324
324
required client,
325
325
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,
326
330
}) : _socketClient = client {
327
331
// Collect extension events and send them periodically to the server.
328
332
_batchSubscription = _batchController.stream.listen ((events) {
329
333
_socketClient.sink.add (jsonEncode (serializers.serialize (BatchedEvents (
330
334
(b) => b.events = ListBuilder <ExtensionEvent >(events)))));
331
335
});
336
+ // Listen for incoming events:
337
+ _socketClient.stream.listen (
338
+ onIncoming,
339
+ onDone: onDone,
340
+ onError: onError,
341
+ cancelOnError: cancelOnError,
342
+ );
332
343
}
333
344
334
345
set socketClient (SocketClient client) {
@@ -341,7 +352,7 @@ class _DebugSession {
341
352
});
342
353
}
343
354
344
- void sendEvent ( ExtensionEvent event) {
355
+ void sendEvent < T >( T event) {
345
356
_socketClient.sink.add (jsonEncode (serializers.serialize (event)));
346
357
}
347
358
0 commit comments