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-
75import 'dart:async' ;
86
97import 'package:async/async.dart' ;
@@ -21,14 +19,17 @@ class RemoteDebuggerExecutionContext extends ExecutionContext {
2119 final RemoteDebugger _remoteDebugger;
2220 final _logger = Logger ('RemoteDebuggerExecutionContext' );
2321
24- // Contexts that may contain a Dart application.
25- StreamQueue <int > _contexts;
22+ /// Contexts that may contain a Dart application.
23+ ///
24+ /// Context can be null if an error has occured and we cannot detect
25+ /// and parse the context ID.
26+ late StreamQueue <int > _contexts;
2627
27- int _id;
28+ int ? _id;
2829
2930 @override
3031 Future <int > get id async {
31- if (_id != null ) return _id;
32+ if (_id != null ) return _id! ;
3233 _logger.fine ('Looking for Dart execution context...' );
3334 const timeoutInMs = 100 ;
3435 while (await _contexts.hasNext
@@ -45,7 +46,7 @@ class RemoteDebuggerExecutionContext extends ExecutionContext {
4546 'expression' : r'window["$dartAppInstanceId"];' ,
4647 'contextId' : context,
4748 });
48- if (result.result['result' ]['value' ] != null ) {
49+ if (result.result? ['result' ]? ['value' ] != null ) {
4950 _logger.fine ('Found valid execution context: $context ' );
5051 _id = context;
5152 break ;
@@ -59,18 +60,30 @@ class RemoteDebuggerExecutionContext extends ExecutionContext {
5960 if (_id == null ) {
6061 throw StateError ('No context with the running Dart application.' );
6162 }
62- return _id;
63+ return _id! ;
6364 }
6465
6566 RemoteDebuggerExecutionContext (this ._id, this ._remoteDebugger) {
6667 final contextController = StreamController <int >();
6768 _remoteDebugger
6869 .eventStream ('Runtime.executionContextsCleared' , (e) => e)
6970 .listen ((_) => _id = null );
70- _remoteDebugger
71- .eventStream ('Runtime.executionContextCreated' ,
72- (e) => int .parse (e.params['context' ]['id' ].toString ()))
73- .listen (contextController.add);
71+ _remoteDebugger.eventStream ('Runtime.executionContextCreated' , (e) {
72+ // Parse and add the context ID to the stream.
73+ // If we cannot detect or parse the context ID, add `null` to the stream
74+ // to indicate an error context - those will be skipped when trying to find
75+ // the dart context, with a warning.
76+ final id = e.params? ['context' ]? ['id' ]? .toString ();
77+ final parsedId = id == null ? null : int .parse (id);
78+ if (id == null ) {
79+ _logger.warning ('Cannot find execution context id: $e ' );
80+ } else if (parsedId == null ) {
81+ _logger.warning ('Cannot parse execution context id: $id ' );
82+ }
83+ return parsedId;
84+ }).listen ((e) {
85+ if (e != null ) contextController.add (e);
86+ });
7487 _contexts = StreamQueue (contextController.stream);
7588 }
7689}
0 commit comments