-
Notifications
You must be signed in to change notification settings - Fork 79
/
Copy pathbatched_expression_evaluator.dart
171 lines (152 loc) · 5.24 KB
/
batched_expression_evaluator.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'dart:async';
import 'package:collection/collection.dart';
import 'package:dwds/shared/batched_stream.dart';
import 'package:dwds/src/debugging/debugger.dart';
import 'package:dwds/src/debugging/location.dart';
import 'package:dwds/src/debugging/modules.dart';
import 'package:dwds/src/services/expression_compiler.dart';
import 'package:dwds/src/services/expression_evaluator.dart';
import 'package:dwds/src/utilities/domain.dart';
import 'package:dwds/src/utilities/shared.dart';
import 'package:logging/logging.dart';
import 'package:webkit_inspection_protocol/webkit_inspection_protocol.dart';
class EvaluateRequest {
final String isolateId;
final String? libraryUri;
final String expression;
final Map<String, String>? scope;
final completer = Completer<RemoteObject>();
EvaluateRequest(this.isolateId, this.libraryUri, this.expression, this.scope);
}
class BatchedExpressionEvaluator extends ExpressionEvaluator {
final _logger = Logger('BatchedExpressionEvaluator');
final AppInspectorInterface _inspector;
final _requestController =
BatchedStreamController<EvaluateRequest>(delay: 200);
bool _closed = false;
BatchedExpressionEvaluator(
String entrypoint,
this._inspector,
Debugger debugger,
Locations locations,
Modules modules,
ExpressionCompiler compiler,
) : super(entrypoint, _inspector, debugger, locations, modules, compiler) {
_requestController.stream.listen(_processRequest);
}
@override
void close() {
if (_closed) return;
_logger.fine('Closed');
_requestController.close();
_closed = true;
}
@override
Future<RemoteObject> evaluateExpression(
String isolateId,
String? libraryUri,
String expression,
Map<String, String>? scope,
) async {
if (_closed) {
return createError(
EvaluationErrorKind.internal,
'Batched expression evaluator closed',
);
}
final request = EvaluateRequest(isolateId, libraryUri, expression, scope);
_requestController.sink.add(request);
return request.completer.future;
}
void _processRequest(List<EvaluateRequest> requests) {
String? libraryUri;
String? isolateId;
Map<String, String>? scope;
var currentRequests = <EvaluateRequest>[];
for (final request in requests) {
libraryUri ??= request.libraryUri;
isolateId ??= request.isolateId;
scope ??= request.scope;
if (libraryUri != request.libraryUri ||
isolateId != request.isolateId ||
!MapEquality().equals(scope, request.scope)) {
_logger.fine('New batch due to');
if (libraryUri != request.libraryUri) {
_logger.fine(' - library uri: $libraryUri != ${request.libraryUri}');
}
if (isolateId != request.isolateId) {
_logger.fine(' - isolateId: $isolateId != ${request.isolateId}');
}
if (!MapEquality().equals(scope, request.scope)) {
_logger.fine(' - scope: $scope != ${request.scope}');
}
safeUnawaited(_evaluateBatch(currentRequests));
currentRequests = [];
libraryUri = request.libraryUri;
isolateId = request.isolateId;
scope = request.scope;
}
currentRequests.add(request);
}
safeUnawaited(_evaluateBatch(currentRequests));
}
Future<void> _evaluateBatch(List<EvaluateRequest> requests) async {
if (requests.isEmpty) return;
final first = requests.first;
if (requests.length == 1) {
if (first.completer.isCompleted) return;
return super
.evaluateExpression(
first.isolateId,
first.libraryUri,
first.expression,
first.scope,
)
.then(requests.first.completer.complete);
}
final expressions = requests.map((r) => r.expression).join(', ');
final batchedExpression = '[ $expressions ]';
_logger.fine('Evaluating batch of expressions $batchedExpression');
final list = await super.evaluateExpression(
first.isolateId,
first.libraryUri,
batchedExpression,
first.scope,
);
for (var i = 0; i < requests.length; i++) {
final request = requests[i];
if (request.completer.isCompleted) continue;
_logger.fine('Getting result out of a batch for ${request.expression}');
final listId = list.objectId;
if (listId == null) {
final error = createError(
EvaluationErrorKind.internal,
'No batch result object ID.',
);
request.completer.complete(error);
} else {
safeUnawaited(
_inspector
.getProperties(
listId,
offset: i,
count: 1,
length: requests.length,
)
.then((v) {
final result = v.first.value!;
_logger.fine(
'Got result out of a batch for ${request.expression}: $result',
);
request.completer.complete(result);
}),
onError: (error, stackTrace) =>
request.completer.completeError(error, stackTrace),
);
}
}
}
}