Skip to content

Commit

Permalink
Batch Debugger.ScriptParsed events sent from the debug extension (#1628)
Browse files Browse the repository at this point in the history
* Batch Debugger.ScriptParsed events sent from the debug extension to extension backend

* Disable debug extension tests on windows again

* Addressed CR comments

* Update CHANGELOG.md

Update change log to reflect moving `batched_stream.dart` into shared utilities.
  • Loading branch information
Anna Gringauze authored Jun 1, 2022
1 parent 205f8a4 commit 0e77cae
Show file tree
Hide file tree
Showing 11 changed files with 6,136 additions and 5,662 deletions.
3 changes: 3 additions & 0 deletions dwds/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
- Convert JavaScript stack traces in uncaught exceptions to Dart stack traces.
- Fix failure to set breakpoints on windows with a base change in index.html.
- Add the setIsolatePauseMode method to Chrome Proxy Service.
- Batch extension `Debugger.scriptParsed` events and send batches every 1000ms
to the server.
- Move `batched_stream.dart` into shared utilities.

## 14.0.2
- Update the min SDK constraint to 2.17.0.
Expand Down
5 changes: 5 additions & 0 deletions dwds/debug_extension/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@

## 1.30
- Batch extension `Debugger.scriptParsed` events and send batches every 1000ms
to the server.

## 1.29

- Notify the debugger and inspector panels when the debug session is disconnected.
Expand Down
4 changes: 2 additions & 2 deletions dwds/debug_extension/CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
- With dart2js:

```
pub run build_runner build web -o build -r
dart run build_runner build web -o build -r
```

This will build to the `/web` directory.

- With DDC:

```
pub run build_runner build web -o build
dart run build_runner build web -o build
```

This will build to the `/build/web` directory.
Expand Down
2 changes: 1 addition & 1 deletion dwds/debug_extension/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: extension
publish_to: none
version: 1.29.0
version: 1.30.0
homepage: https://github.com/dart-lang/webdev
description: >-
A chrome extension for Dart debugging.
Expand Down
50 changes: 42 additions & 8 deletions dwds/debug_extension/web/background.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import 'package:dwds/data/devtools_request.dart';
import 'package:dwds/data/extension_request.dart';
import 'package:dwds/data/serializers.dart';
import 'package:dwds/src/sockets.dart';
import 'package:dwds/src/utilities/batched_stream.dart';
import 'package:js/js.dart';
import 'package:js/js_util.dart' as js_util;
import 'package:pub_semver/pub_semver.dart';
Expand Down Expand Up @@ -61,8 +62,6 @@ Tab _mostRecentDartTab;
DebuggerTrigger _debuggerTrigger;

class DebugSession {
final SocketClient socketClient;

// The tab ID that contains the running Dart application.
final int appTabId;

Expand All @@ -72,7 +71,38 @@ class DebugSession {
// The tab ID that contains the corresponding Dart DevTools.
int devtoolsTabId;

DebugSession(this.socketClient, this.appTabId, this.appId);
// Socket client for communication with dwds extension backend.
final SocketClient _socketClient;

// How often to send batched events.
static const int _batchDelayMilliseconds = 1000;

// Collect events into batches to be send periodically to the server.
final _batchController =
BatchedStreamController<ExtensionEvent>(delay: _batchDelayMilliseconds);
StreamSubscription<List<ExtensionEvent>> _batchSubscription;

DebugSession(this._socketClient, this.appTabId, this.appId) {
// 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)))));
});
}

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

void sendBatchedEvent(ExtensionEvent event) {
_batchController.sink.add(event);
}

void close() {
_socketClient.close();
_batchSubscription.cancel();
_batchController.close();
}
}

class DevToolsPanel {
Expand Down Expand Up @@ -101,7 +131,7 @@ void main() {
onMessageAddListener(allowInterop(_handleMessageFromContentScripts));

// Attaches a debug session to the app when the extension receives a
// Runtime.executionContextCreated event from DWDS:
// Runtime.executionContextCreated event from Chrome:
addDebuggerListener(allowInterop(_maybeAttachDebugSession));

// When a Dart application tab is closed, detach the corresponding debug
Expand Down Expand Up @@ -264,7 +294,7 @@ void _maybeAttachDebugSession(
Object params,
) async {
// Return early if it's not a Runtime.executionContextCreated event (sent from
// DWDS):
// Chrome):
if (method != 'Runtime.executionContextCreated') return;

var context = json.decode(stringify(params))['context'];
Expand Down Expand Up @@ -303,8 +333,8 @@ int _removeDebugSessionForTab(int tabId) {
// https://github.com/dart-lang/webdev/pull/1595#issuecomment-1116773378
final event =
_extensionEventFor('DebugExtension.detached', js_util.jsify({}));
session.socketClient.sink.add(jsonEncode(serializers.serialize(event)));
session.socketClient.close();
session.sendEvent(event);
session.close();
_debugSessions.remove(session);

// Notify the Dart DevTools panel that the session has been detached by
Expand Down Expand Up @@ -590,7 +620,11 @@ void _filterAndForwardToBackend(Debuggee source, String method, Object params) {

var event = _extensionEventFor(method, params);

debugSession.socketClient.sink.add(jsonEncode(serializers.serialize(event)));
if (method == 'Debugger.scriptParsed') {
debugSession.sendBatchedEvent(event);
} else {
debugSession.sendEvent(event);
}
}

class Notifier<T> {
Expand Down
Loading

0 comments on commit 0e77cae

Please sign in to comment.