Skip to content

Commit b06b9db

Browse files
authored
Faster DevTools launch (#747)
- No longer wait for `CreateIsolate` to complete or the debugger to be enabled before creating the `ChromeProxyService` - Get rid of the `correctTab` evals as they are no longer necessary - No longer return a `RunResponse` as the run state is now properly stored in the `AppConnection`
1 parent 12fa2fb commit b06b9db

File tree

13 files changed

+84
-247
lines changed

13 files changed

+84
-247
lines changed

dwds/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
- The injected client's connection is now based off the request URI.
44
- Fix an issue where resuming while paused at the start would cause an error.
55
- Expose the `ChromeDebugException` class for error handling purposes.
6+
- DevTools will now launch immediately and lazily sets up necessary state.
67

78
## 0.7.4
89

dwds/lib/data/run_request.dart

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,3 @@ abstract class RunRequest implements Built<RunRequest, RunRequestBuilder> {
1414

1515
RunRequest._();
1616
}
17-
18-
abstract class RunResponse implements Built<RunResponse, RunResponseBuilder> {
19-
static Serializer<RunResponse> get serializer => _$runResponseSerializer;
20-
21-
factory RunResponse([Function(RunResponseBuilder) updates]) = _$RunResponse;
22-
23-
RunResponse._();
24-
}

dwds/lib/data/run_request.g.dart

Lines changed: 0 additions & 76 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dwds/lib/data/serializers.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ part 'serializers.g.dart';
2020
DevToolsResponse,
2121
ConnectRequest,
2222
RunRequest,
23-
RunResponse,
2423
DefaultBuildResult,
2524
IsolateExit,
2625
IsolateStart,

dwds/lib/data/serializers.g.dart

Lines changed: 1 addition & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dwds/lib/dwds.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ class Dwds {
5050
Future<DebugConnection> debugConnection(AppConnection appConnection) async {
5151
if (!_enableDebugging) throw StateError('Debugging is not enabled.');
5252
var appDebugServices = await _devHandler.loadAppServices(appConnection);
53+
await appDebugServices.chromeProxyService.isInitialized;
5354
return DebugConnection(appDebugServices);
5455
}
5556

dwds/lib/src/connections/app_connection.dart

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5+
import 'dart:async';
56
import 'dart:convert';
67

78
import 'package:sse/server/sse_handler.dart';
@@ -14,17 +15,19 @@ import '../../data/serializers.dart';
1415
class AppConnection {
1516
/// The initial connection request sent from the application in the browser.
1617
final ConnectRequest request;
17-
18+
final _startedCompleter = Completer<void>();
1819
final SseConnection _connection;
19-
var _isStarted = false;
2020

2121
AppConnection(this.request, this._connection);
2222

23-
bool get isStarted => _isStarted;
23+
bool get isStarted => _startedCompleter.isCompleted;
24+
Future<void> get onStart => _startedCompleter.future;
2425

2526
void runMain() {
26-
if (_isStarted) throw StateError('Main has already started.');
27+
if (_startedCompleter.isCompleted) {
28+
throw StateError('Main has already started.');
29+
}
2730
_connection.sink.add(jsonEncode(serializers.serialize(RunRequest())));
28-
_isStarted = true;
31+
_startedCompleter.complete();
2932
}
3033
}

dwds/lib/src/handlers/dev_handler.dart

Lines changed: 3 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import 'package:build_daemon/data/serializers.dart' as build_daemon;
1010
import 'package:dwds/data/error_response.dart';
1111
import 'package:dwds/data/run_request.dart';
1212
import 'package:dwds/src/connections/debug_connection.dart';
13-
import 'package:dwds/src/debugging/remote_debugger.dart';
1413
import 'package:dwds/src/debugging/webkit_debugger.dart';
1514
import 'package:dwds/src/servers/extension_backend.dart';
1615
import 'package:logging/logging.dart';
@@ -148,9 +147,6 @@ class DevHandler {
148147
await startDebugService(await _chromeConnection(), appConnection);
149148
var appServices = await _createAppDebugServices(
150149
appConnection.request.appId, debugService);
151-
if (appConnection.isStarted) {
152-
await appServices.chromeProxyService.resumeFromStart();
153-
}
154150
unawaited(appServices.chromeProxyService.remoteDebugger.onClose.first
155151
.whenComplete(() {
156152
appServices.close();
@@ -187,8 +183,6 @@ class DevHandler {
187183
await _handleIsolateExit(appConnection);
188184
} else if (message is IsolateStart) {
189185
await _handleIsolateStart(appConnection, injectedConnection);
190-
} else if (message is RunResponse) {
191-
await _handleRunResponse(appConnection);
192186
}
193187
}
194188
} catch (e, s) {
@@ -262,15 +256,6 @@ class DevHandler {
262256
return;
263257
}
264258

265-
// If you load the same app in a different tab then we need to throw
266-
// away our old services and start new ones.
267-
if (!(await _isCorrectTab(appConnection.request.instanceId,
268-
appServices.chromeProxyService.remoteDebugger))) {
269-
unawaited(appServices.close());
270-
unawaited(_servicesByAppId.remove(appConnection.request.appId));
271-
appServices = await loadAppServices(appConnection);
272-
}
273-
274259
sseConnection.sink.add(jsonEncode(
275260
serializers.serialize(DevToolsResponse((b) => b..success = true))));
276261

@@ -293,13 +278,9 @@ class DevHandler {
293278
var services = await _servicesByAppId[message.appId];
294279
var connection = AppConnection(message, sseConnection);
295280
if (services != null && services.connectedInstanceId == null) {
296-
// Re-connect to the previous instance if its in the same tab,
297-
// otherwise do nothing for now.
298-
if (await _isCorrectTab(
299-
message.instanceId, services.chromeProxyService.remoteDebugger)) {
300-
services.connectedInstanceId = message.instanceId;
301-
await services.chromeProxyService.createIsolate(connection);
302-
}
281+
// Reconnect to existing service.
282+
services.connectedInstanceId = message.instanceId;
283+
await services.chromeProxyService.createIsolate(connection);
303284
}
304285
_appConnectionByAppId[message.appId] = connection;
305286
_connectedApps.add(connection);
@@ -322,12 +303,6 @@ class DevHandler {
322303
sseConnection.sink.add(jsonEncode(serializers.serialize(RunRequest())));
323304
}
324305

325-
Future<void> _handleRunResponse(AppConnection appConnection) async {
326-
await (await _servicesByAppId[appConnection.request.appId])
327-
?.chromeProxyService
328-
?.resumeFromStart();
329-
}
330-
331306
void _listen() async {
332307
var injectedConnections = _sseHandler.connections;
333308
while (await injectedConnections.hasNext) {
@@ -406,10 +381,3 @@ class DevHandler {
406381
});
407382
}
408383
}
409-
410-
/// Checks if connection of [remoteDebugger] is running the app with [instanceId].
411-
Future<bool> _isCorrectTab(
412-
String instanceId, RemoteDebugger remoteDebugger) async {
413-
var result = await remoteDebugger.evaluate(r'window["$dartAppInstanceId"];');
414-
return result.value == instanceId;
415-
}

0 commit comments

Comments
 (0)