Skip to content

Commit 8c8edc1

Browse files
author
Anna Gringauze
authored
Migrate rest of the services and dwds_vm_client.dart to null safety (#1688)
* Migrate debugging/inspector.dart to null safety * Migrate expression evaluator to null safety * format * Fix failing tests * Migrate services/chrome_proxu_service.dart to null safety * Migrate rest of the services and dwds_vm_client to null safety * Made Inspector.getObject return non-null value * Allow null _ddsUri in debug_service.dart * Address CR comments
1 parent 4534124 commit 8c8edc1

File tree

6 files changed

+61
-68
lines changed

6 files changed

+61
-68
lines changed

dwds/lib/src/connections/debug_connection.dart

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
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-
// @dart = 2.9
6-
75
import 'dart:async';
86

97
import 'package:vm_service/vm_service.dart';
@@ -22,7 +20,7 @@ class DebugConnection {
2220
/// Null until [close] is called.
2321
///
2422
/// All subsequent calls to [close] will return this future.
25-
Future<void> _closed;
23+
Future<void>? _closed;
2624

2725
DebugConnection(this._appDebugServices) {
2826
_appDebugServices.chromeProxyService.remoteDebugger.onClose.first.then((_) {

dwds/lib/src/debugging/inspector.dart

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -403,8 +403,7 @@ class AppInspector implements AppInspectorInterface {
403403
}
404404
final libraryId = _scriptIdToLibraryId[scriptId];
405405
if (libraryId == null) {
406-
throwInvalidParam('getObject',
407-
'No library for script $scriptRef with libraryId: $libraryId');
406+
throwInvalidParam('getObject', 'No library for script $scriptRef');
408407
}
409408
return Script(
410409
uri: scriptRef.uri,

dwds/lib/src/dwds_vm_client.dart

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
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-
// @dart = 2.9
6-
75
import 'dart:async';
86
import 'dart:convert';
97

@@ -32,7 +30,7 @@ class DwdsVmClient {
3230
/// Null until [close] is called.
3331
///
3432
/// All subsequent calls to [close] will return this future.
35-
Future<void> _closed;
33+
Future<void>? _closed;
3634

3735
DwdsVmClient(this.client, this._requestController, this._responseController);
3836

@@ -57,7 +55,7 @@ class DwdsVmClient {
5755
'$request');
5856
return;
5957
}
60-
requestController.sink.add(jsonDecode(request) as Map<String, dynamic>);
58+
requestController.sink.add(jsonDecode(request) as Map<String, Object>);
6159
});
6260
final chromeProxyService =
6361
debugService.chromeProxyService as ChromeProxyService;
@@ -73,7 +71,7 @@ class DwdsVmClient {
7371
return <String, dynamic>{
7472
'result': <String, Object>{
7573
'views': <Object>[
76-
for (var isolate in isolates)
74+
for (var isolate in isolates ?? [])
7775
<String, Object>{
7876
'id': isolate.id,
7977
'isolate': isolate.toJson(),
@@ -119,7 +117,7 @@ class DwdsVmClient {
119117
await client.registerService('ext.dwds.emitEvent', 'DWDS');
120118

121119
client.registerServiceCallback('_yieldControlToDDS', (request) async {
122-
final ddsUri = request['uri'] as String;
120+
final ddsUri = request['uri'] as String?;
123121
if (ddsUri == null) {
124122
return RPCError(
125123
request['method'] as String,
@@ -146,30 +144,26 @@ class DwdsVmClient {
146144

147145
void _processSendEvent(Map<String, dynamic> event,
148146
ChromeProxyService chromeProxyService, DwdsStats dwdsStats) {
149-
final type = event['type'] as String;
150-
final payload = event['payload'] as Map<String, dynamic>;
147+
final type = event['type'] as String?;
148+
final payload = event['payload'] as Map<String, dynamic>?;
151149
switch (type) {
152150
case 'DevtoolsEvent':
153151
{
154152
_logger.finest('Received DevTools event: $event');
155-
final action = payload == null ? null : payload['action'] as String;
156-
final screen = payload == null ? null : payload['screen'] as String;
157-
if (action == 'pageReady') {
153+
final action = payload?['action'] as String?;
154+
final screen = payload?['screen'] as String?;
155+
if (screen != null && action == 'pageReady') {
158156
if (dwdsStats.isFirstDebuggerReady) {
159-
if (dwdsStats.devToolsStart != null) {
160-
final time = DateTime.now()
161-
.difference(dwdsStats.devToolsStart)
162-
.inMilliseconds;
163-
emitEvent(DwdsEvent.devToolsLoad(time, screen));
164-
_logger.fine('DevTools load time: $time ms');
165-
}
166-
if (dwdsStats.debuggerStart != null) {
167-
final time = DateTime.now()
168-
.difference(dwdsStats.debuggerStart)
169-
.inMilliseconds;
170-
emitEvent(DwdsEvent.debuggerReady(time, screen));
171-
_logger.fine('Debugger ready time: $time ms');
172-
}
157+
final debuggerReadyTime = DateTime.now()
158+
.difference(dwdsStats.devToolsStart)
159+
.inMilliseconds;
160+
emitEvent(DwdsEvent.devToolsLoad(debuggerReadyTime, screen));
161+
_logger.fine('DevTools load time: $debuggerReadyTime ms');
162+
final debuggerStartTime = DateTime.now()
163+
.difference(dwdsStats.debuggerStart)
164+
.inMilliseconds;
165+
emitEvent(DwdsEvent.debuggerReady(debuggerStartTime, screen));
166+
_logger.fine('Debugger ready time: $debuggerStartTime ms');
173167
} else {
174168
_logger
175169
.finest('Debugger and DevTools startup times alredy recorded.'
@@ -213,14 +207,15 @@ Future<Map<String, dynamic>> _hotRestart(
213207
.jsEvaluate('\$dartHotRestartDwds(\'$runId\');', awaitPromise: true);
214208
_logger.info('\$dartHotRestartDwds request complete.');
215209
} on WipError catch (exception) {
216-
final code = exception.error['code'];
210+
final code = exception.error?['code'];
211+
final message = exception.error?['message'];
217212
// This corresponds to `Execution context was destroyed` which can
218213
// occur during a hot restart that must fall back to a full reload.
219214
if (code != RPCError.kServerError) {
220215
return {
221216
'error': {
222-
'code': exception.error['code'],
223-
'message': exception.error['message'],
217+
'code': code,
218+
'message': message,
224219
'data': exception,
225220
}
226221
};
@@ -255,9 +250,14 @@ Future<void> _disableBreakpointsAndResume(
255250
VmService client, ChromeProxyService chromeProxyService) async {
256251
_logger.info('Attempting to disable breakpoints and resume the isolate');
257252
final vm = await client.getVM();
258-
if (vm.isolates.isEmpty) throw StateError('No active isolate to resume.');
259-
final isolateRef = vm.isolates.first;
260-
253+
final isolates = vm.isolates;
254+
if (isolates == null || isolates.isEmpty) {
255+
throw StateError('No active isolate to resume.');
256+
}
257+
final isolateId = isolates.first.id;
258+
if (isolateId == null) {
259+
throw StateError('No active isolate to resume.');
260+
}
261261
await chromeProxyService.disableBreakpoints();
262262
try {
263263
// Any checks for paused status result in race conditions or hangs
@@ -274,7 +274,7 @@ Future<void> _disableBreakpointsAndResume(
274274
// ignore failures indicating that the app is already running:
275275
//
276276
// WipError -32000 Can only perform operation while paused.
277-
await client.resume(isolateRef.id);
277+
await client.resume(isolateId);
278278
} on RPCError catch (e, s) {
279279
if (!e.message.contains('Can only perform operation while paused')) {
280280
_logger.severe('Hot restart failed to resume exiting isolate', e, s);

dwds/lib/src/services/app_debug_services.dart

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
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-
// @dart = 2.9
6-
75
import 'dart:async';
86

97
import '../dwds_vm_client.dart';
@@ -23,12 +21,12 @@ class AppDebugServices {
2321
/// Null until [close] is called.
2422
///
2523
/// All subsequent calls to [close] will return this future.
26-
Future<void> _closed;
24+
Future<void>? _closed;
2725

2826
/// The instance ID for the currently connected application, if there is one.
2927
///
3028
/// We only allow a given app to be debugged in a single tab at a time.
31-
String connectedInstanceId;
29+
String? connectedInstanceId;
3230

3331
AppDebugServices(this.debugService, this.dwdsVmClient, this.dwdsStats);
3432

dwds/lib/src/services/chrome_proxy_service.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ class ChromeProxyService implements VmServiceInterface {
128128
LoadStrategy loadStrategy,
129129
AppConnection appConnection,
130130
ExecutionContext executionContext,
131-
ExpressionCompiler expressionCompiler,
131+
ExpressionCompiler? expressionCompiler,
132132
SdkConfigurationProvider sdkConfigurationProvider,
133133
) async {
134134
final vm = VM(

dwds/lib/src/services/debug_service.dart

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
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-
// @dart = 2.9
6-
75
import 'dart:async';
86
import 'dart:convert';
97
import 'dart:io';
@@ -38,8 +36,8 @@ Logger _logger = Logger('DebugService');
3836
void Function(WebSocketChannel, String) _createNewConnectionHandler(
3937
ChromeProxyService chromeProxyService,
4038
ServiceExtensionRegistry serviceExtensionRegistry, {
41-
void Function(Map<String, dynamic>) onRequest,
42-
void Function(Map<String, dynamic>) onResponse,
39+
void Function(Map<String, dynamic>)? onRequest,
40+
void Function(Map<String, dynamic>)? onResponse,
4341
}) {
4442
return (webSocket, protocol) {
4543
final responseController = StreamController<Map<String, Object>>();
@@ -49,7 +47,7 @@ void Function(WebSocketChannel, String) _createNewConnectionHandler(
4947
}));
5048
final inputStream = webSocket.stream.map((value) {
5149
if (value is List<int>) {
52-
value = utf8.decode(value as List<int>);
50+
value = utf8.decode(value);
5351
} else if (value is! String) {
5452
throw StateError(
5553
'Got value with unexpected type ${value.runtimeType} from web '
@@ -78,8 +76,8 @@ Future<void> _handleSseConnections(
7876
SseHandler handler,
7977
ChromeProxyService chromeProxyService,
8078
ServiceExtensionRegistry serviceExtensionRegistry, {
81-
void Function(Map<String, dynamic>) onRequest,
82-
void Function(Map<String, dynamic>) onResponse,
79+
void Function(Map<String, dynamic>)? onRequest,
80+
void Function(Map<String, dynamic>)? onResponse,
8381
}) async {
8482
while (await handler.connections.hasNext) {
8583
final connection = await handler.connections.next;
@@ -116,7 +114,7 @@ Future<void> _handleSseConnections(
116114
///
117115
/// Creates a [ChromeProxyService] from an existing Chrome instance.
118116
class DebugService {
119-
static String _ddsUri;
117+
static String? _ddsUri;
120118

121119
final VmServiceInterface chromeProxyService;
122120
final String hostname;
@@ -126,13 +124,13 @@ class DebugService {
126124
final HttpServer _server;
127125
final bool _useSse;
128126
final bool _spawnDds;
129-
final UrlEncoder _urlEncoder;
130-
DartDevelopmentService _dds;
127+
final UrlEncoder? _urlEncoder;
128+
DartDevelopmentService? _dds;
131129

132130
/// Null until [close] is called.
133131
///
134132
/// All subsequent calls to [close] will return this future.
135-
Future<void> _closed;
133+
Future<void>? _closed;
136134

137135
DebugService._(
138136
this.chromeProxyService,
@@ -147,7 +145,7 @@ class DebugService {
147145

148146
Future<void> close() => _closed ??= Future.wait([
149147
_server.close(),
150-
if (_dds != null) _dds.shutdown(),
148+
if (_dds != null) _dds!.shutdown(),
151149
]);
152150

153151
Future<void> startDartDevelopmentService() async {
@@ -170,8 +168,9 @@ class DebugService {
170168
}
171169

172170
String get uri {
173-
if (_spawnDds && _dds != null) {
174-
return (_useSse ? _dds.sseUri : _dds.wsUri).toString();
171+
final dds = _dds;
172+
if (_spawnDds && dds != null) {
173+
return (_useSse ? dds.sseUri : dds.wsUri).toString();
175174
}
176175
return (_useSse
177176
? Uri(
@@ -189,12 +188,12 @@ class DebugService {
189188
.toString();
190189
}
191190

192-
String _encodedUri;
191+
String? _encodedUri;
193192
Future<String> get encodedUri async {
194-
if (_encodedUri != null) return _encodedUri;
195-
var encodedUri = uri;
196-
if (_urlEncoder != null) encodedUri = await _urlEncoder(encodedUri);
197-
return _encodedUri = encodedUri;
193+
if (_encodedUri != null) return _encodedUri!;
194+
var encoded = uri;
195+
if (_urlEncoder != null) encoded = await _urlEncoder!(encoded);
196+
return _encodedUri = encoded;
198197
}
199198

200199
static bool yieldControlToDDS(String uri) {
@@ -214,15 +213,14 @@ class DebugService {
214213
AssetReader assetReader,
215214
LoadStrategy loadStrategy,
216215
AppConnection appConnection,
217-
UrlEncoder urlEncoder, {
218-
void Function(Map<String, dynamic>) onRequest,
219-
void Function(Map<String, dynamic>) onResponse,
216+
UrlEncoder? urlEncoder, {
217+
void Function(Map<String, dynamic>)? onRequest,
218+
void Function(Map<String, dynamic>)? onResponse,
220219
bool spawnDds = true,
221-
bool useSse,
222-
ExpressionCompiler expressionCompiler,
223-
SdkConfigurationProvider sdkConfigurationProvider,
220+
bool useSse = false,
221+
ExpressionCompiler? expressionCompiler,
222+
required SdkConfigurationProvider sdkConfigurationProvider,
224223
}) async {
225-
useSse ??= false;
226224
final chromeProxyService = await ChromeProxyService.create(
227225
remoteDebugger,
228226
root,

0 commit comments

Comments
 (0)