Skip to content

DebugSession listens to events instead of just sending events #1804

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 1, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 19 additions & 8 deletions dwds/debug_extension_mv3/web/debug_session.dart
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,10 @@ Future<bool> _connectToDwds({
final client = uri.isScheme('ws') || uri.isScheme('wss')
? WebSocketClient(WebSocketChannel.connect(uri))
: SseSocketClient(SseClient(uri.toString()));
final debugSession = _DebugSession(client: client, appTabId: dartAppTabId);
_debugSessions.add(debugSession);
client.stream.listen(
(data) => _routeDwdsEvent(data, client, dartAppTabId),
final debugSession = _DebugSession(
client: client,
appTabId: dartAppTabId,
onIncoming: (data) => _routeDwdsEvent(data, client, dartAppTabId),
onDone: () {
debugLog('Shutting down DWDS communication channel.');
_detachDebuggerForTab(dartAppTabId, type: TabType.dartApp);
Expand All @@ -147,15 +147,15 @@ Future<bool> _connectToDwds({
},
cancelOnError: true,
);
_debugSessions.add(debugSession);
final tabUrl = await _getTabUrl(dartAppTabId);
// Send a DevtoolsRequest to the event stream:
final event = jsonEncode(serializers.serialize(DevToolsRequest((b) => b
debugSession.sendEvent(DevToolsRequest((b) => b
..appId = debugInfo.appId
..instanceId = debugInfo.appInstanceId
..contextId = dartAppContextId
..tabUrl = tabUrl
..uriOnly = true)));
client.sink.add(event);
..uriOnly = true));
return true;
}

Expand Down Expand Up @@ -323,12 +323,23 @@ class _DebugSession {
_DebugSession({
required client,
required this.appTabId,
required void Function(String data) onIncoming,
required void Function() onDone,
required void Function(dynamic error) onError,
required bool cancelOnError,
}) : _socketClient = client {
// Collect extension events and send them periodically to the server.
_batchSubscription = _batchController.stream.listen((events) {
_socketClient.sink.add(jsonEncode(serializers.serialize(BatchedEvents(
(b) => b.events = ListBuilder<ExtensionEvent>(events)))));
});
// Listen for incoming events:
_socketClient.stream.listen(
onIncoming,
onDone: onDone,
onError: onError,
cancelOnError: cancelOnError,
);
}

set socketClient(SocketClient client) {
Expand All @@ -341,7 +352,7 @@ class _DebugSession {
});
}

void sendEvent(ExtensionEvent event) {
void sendEvent<T>(T event) {
_socketClient.sink.add(jsonEncode(serializers.serialize(event)));
}

Expand Down